first working example

This commit is contained in:
Tim Bendt
2024-04-19 14:42:42 -06:00
parent cf50f37ede
commit efb5de13cc
3 changed files with 91 additions and 160 deletions

View File

@@ -1,10 +1,10 @@
import { Button, Flex } from "wretched"; import { Button, Flex } from "wretched";
import { getAllProjects } from "../services/api-client.js"; import { getAllProjects } from "../services/api-client.js";
const projects = await getAllProjects({ limit: 100 }); const projectsResp = await getAllProjects({ limit: 100, sort_by: "date_updated", sort_dir: "desc" });
export const projectView = Flex.down({ export const projectView = Flex.down({
children: projects.map(x => new Button({ children: projectsResp.projects.map(x => new Button({
text: x.name text: x.name
}) })
) )

View File

@@ -1,4 +1,4 @@
import "dotenv" import 'dotenv/config'
import { Screen, Box, Flow, Text, Button, interceptConsoleLog, ConsoleLog, iTerm2, Window, Flex } from 'wretched' import { Screen, Box, Flow, Text, Button, interceptConsoleLog, ConsoleLog, iTerm2, Window, Flex } from 'wretched'
import * as utility from "wretched/dist/components/utility"; import * as utility from "wretched/dist/components/utility";
import { projectView } from "./components/listProjects.js"; import { projectView } from "./components/listProjects.js";

View File

@@ -4,72 +4,67 @@ import ky from 'ky';
const API_KEY = process.env.PANCAKE_API_KEY; const API_KEY = process.env.PANCAKE_API_KEY;
const API_URL = process.env.PANCAKE_API_URL; const API_URL = process.env.PANCAKE_API_URL;
console.log("🚀 ~ API_URL:", API_URL)
const api = ky.create({ prefixUrl: API_URL, headers: { "Authorization": `Bearer ${API_KEY}` } }); const api = ky.create({ prefixUrl: API_URL, headers: { "x-api-key": `${API_KEY}` } });
type PaginationParams = { limit?: number, start?: number, sort_by?: string, sort_dir?: 'asc' | 'desc' } type PaginationParams = { limit?: number, start?: number, sort_by?: string, sort_dir?: 'asc' | 'desc' }
// Clients // Clients
async function getAllClients(params: PaginationParams) { async function getAllClients(params: PaginationParams) {
const { limit = 5, start = 0, sort_by = 'id', sort_dir = 'asc' } = params; const { limit = 5, start = 0, sort_by = 'id', sort_dir = 'asc' } = params;
const url = `${BASE_URL}/clients?limit=${limit}&start=${start}&sort_by=${sort_by}&sort_dir=${sort_dir}`; const url = `clients?limit=${limit}&start=${start}&sort_by=${sort_by}&sort_dir=${sort_dir}`;
const response = await fetch(url); const response = api.get(url);
return response.json(); return response.json();
} }
async function getOneClient(id: string) { async function getOneClient(id: string) {
const url = `${BASE_URL}/clients/show?id=${id}`; const url = `clients/show?id=${id}`;
const response = await fetch(url); const response = await api.get(url);
return response.json(); return response.json();
} }
async function createNewClient(data) { async function createNewClient(data) {
const url = `${BASE_URL}/clients/new`; const url = `clients/new`;
const response = await fetch(url, { const response = await api.post(url, {
method: 'POST', json: data
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}); });
return response.json(); return response.json();
} }
async function updateClient(data) { async function updateClient(data) {
const url = `${BASE_URL}/clients/edit`; const url = `clients/edit`;
const response = await fetch(url, { const response = await api.post(url, {
method: 'POST', json: data
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}); });
return response.json(); return response.json();
} }
async function deleteClient(id: string) { async function deleteClient(id: string) {
const url = `${BASE_URL}/clients/delete`; const url = `clients/delete`;
const response = await fetch(url, { const response = await api.post(url, {
method: 'POST', json: { id }
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id })
}); });
return response.json(); return response.json();
} }
type ListResponse<A extends string, T> = {
status: boolean,
message: string,
count: number,
} & Record<A, Array<T>>
type ProjectResponse = {
name: string
}
// Projects // Projects
async function getOneProject(id: string) { async function getOneProject(id: string): Promise<ProjectResponse> {
const url = `${BASE_URL}/projects/show?id=${id}`; const url = `projects/show?id=${id}`;
const response = await fetch(url); const response = await api.get(url);
return response.json(); return response.json();
} }
async function getAllProjects(params: PaginationParams) { async function getAllProjects(params: PaginationParams): Promise<ListResponse<"projects", ProjectResponse>> {
const { limit = 5, start = 0, sort_by = 'id', sort_dir = 'asc' } = params; const { limit = 5, start = 0, sort_by = 'id', sort_dir = 'asc' } = params;
const url = `${BASE_URL}/projects?limit=${limit}&start=${start}&sort_by=${sort_by}&sort_dir=${sort_dir}`; const url = `projects?limit=${limit}&start=${start}&sort_by=${sort_by}&sort_dir=${sort_dir}`;
const response = await fetch(url); const response = await api.get(url);
console.log("🚀 ~ getAllProjects ~ response:", response)
return response.json(); return response.json();
} }
@@ -77,62 +72,46 @@ async function getAllProjects(params: PaginationParams) {
// Projects (continued) // Projects (continued)
async function createNewProject(data) { async function createNewProject(data) {
const url = `${BASE_URL}/projects/new`; const url = `projects/new`;
const response = await fetch(url, { const response = await api.post(url, {
method: 'POST', json: data
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}); });
return response.json(); return response.json();
} }
async function updateProject(data) { async function updateProject(data) {
const url = `${BASE_URL}/projects/edit`; const url = `projects/edit`;
const response = await fetch(url, { const response = await api.post(url, {
method: 'POST', json: data
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}); });
return response.json(); return response.json();
} }
// Tasks // Tasks
async function getTasksByProject(projectId: string) { async function getTasksByProject(projectId: string) {
const url = `${BASE_URL}/projects/tasks?id=${projectId}`; const url = `projects/tasks?id=${projectId}`;
const response = await fetch(url); const response = await api.get(url);
return response.json(); return response.json();
} }
async function createTask(data) { async function createTask(data) {
const url = `${BASE_URL}/projects/tasks/new`; const url = `projects/tasks/new`;
const response = await fetch(url, { const response = await api.post(url, {
method: 'POST', json: data
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}); });
return response.json(); return response.json();
} }
async function updateTask(data) { async function updateTask(data) {
const url = `${BASE_URL}/projects/tasks/update`; const url = `projects/tasks/update`;
const response = await fetch(url, { const response = await api.post(url, {
method: 'POST', json: data
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}); });
return response.json(); return response.json();
} }
async function deleteTask(taskId: string) { async function deleteTask(taskId: string) {
const url = `${BASE_URL}/projects/tasks/show`; const url = `projects/tasks/show`;
const response = await fetch(url, { const response = await fetch(url, {
method: 'POST', method: 'POST',
headers: { headers: {
@@ -144,37 +123,25 @@ async function deleteTask(taskId: string) {
} }
async function logTimeOnTask(data) { async function logTimeOnTask(data) {
const url = `${BASE_URL}/projects/tasks/log_time`; const url = `projects/tasks/log_time`;
const response = await fetch(url, { const response = await api.post(url, {
method: 'POST', json: data
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}); });
return response.json(); return response.json();
} }
async function completeTask(taskId: string) { async function completeTask(taskId: string) {
const url = `${BASE_URL}/projects/tasks/compete`; const url = `projects/tasks/compete`;
const response = await fetch(url, { const response = await api.post(url, {
method: 'POST', json: { id: taskId }
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id: taskId })
}); });
return response.json(); return response.json();
} }
async function reopenTask(taskId: string) { async function reopenTask(taskId: string) {
const url = `${BASE_URL}/projects/tasks/reopen`; const url = `projects/tasks/reopen`;
const response = await fetch(url, { const response = await api.post(url, {
method: 'POST', json: { id: taskId }
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id: taskId })
}); });
return response.json(); return response.json();
} }
@@ -183,136 +150,100 @@ async function reopenTask(taskId: string) {
async function getAllInvoices(params: { client_id: string } & PaginationParams) { async function getAllInvoices(params: { client_id: string } & PaginationParams) {
const { client_id, limit = 5, start = 0, sort_by = 'id', sort_dir = 'asc' } = params; const { client_id, limit = 5, start = 0, sort_by = 'id', sort_dir = 'asc' } = params;
const queryParams = new URLSearchParams({ client_id, limit: limit.toFixed(0), start: start.toFixed(0), sort_by, sort_dir }); const queryParams = new URLSearchParams({ client_id, limit: limit.toFixed(0), start: start.toFixed(0), sort_by, sort_dir });
const url = `${BASE_URL}/invoices?${queryParams.toString()}`; const url = `invoices?${queryParams.toString()}`;
const response = await fetch(url); const response = await api.get(url);
return response.json(); return response.json();
} }
async function getOneInvoice(id: string) { async function getOneInvoice(id: string) {
const url = `${BASE_URL}/invoices/show?id=${id}`; const url = `invoices/show?id=${id}`;
const response = await fetch(url); const response = await api.get(url);
return response.json(); return response.json();
} }
async function createNewInvoice(data) { async function createNewInvoice(data) {
const url = `${BASE_URL}/invoices/new`; const url = `invoices/new`;
const response = await fetch(url, { const response = await api.post(url, {
method: 'POST', json: data
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}); });
return response.json(); return response.json();
} }
async function updateInvoice(data) { async function updateInvoice(data) {
const url = `${BASE_URL}/invoices/edit`; const url = `invoices/edit`;
const response = await fetch(url, { const response = await api.post(url, {
method: 'POST', json: data
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}); });
return response.json(); return response.json();
} }
async function deleteInvoice(invoiceId: string) { async function deleteInvoice(invoiceId: string) {
const url = `${BASE_URL}/invoices/delete`; const url = `invoices/delete`;
const response = await fetch(url, { const response = await api.post(url, {
method: 'POST', json: { id: invoiceId }
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id: invoiceId })
}); });
return response.json(); return response.json();
} }
async function openInvoice(invoiceId: string) { async function openInvoice(invoiceId: string) {
const url = `${BASE_URL}/invoices/open`; const url = `invoices/open`;
const response = await fetch(url, { const response = await api.post(url, {
method: 'POST', json: { id: invoiceId }
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id: invoiceId })
}); });
return response.json(); return response.json();
} }
async function closeInvoice(invoiceId: string) { async function closeInvoice(invoiceId: string) {
const url = `${BASE_URL}/invoices/close`; const url = `invoices/close`;
const response = await fetch(url, { const response = await api.post(url, {
method: 'POST', json: { id: invoiceId }
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id: invoiceId })
}); });
return response.json(); return response.json();
} }
async function markInvoicePaid(invoiceId: string) { async function markInvoicePaid(invoiceId: string) {
const url = `${BASE_URL}/invoices/paid`; const url = `invoices/paid`;
const response = await fetch(url, { const response = await api.post(url, {
method: 'POST', json: { id: invoiceId }
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id: invoiceId })
}); });
return response.json(); return response.json();
} }
async function sendInvoice(invoiceId: string) { async function sendInvoice(invoiceId: string) {
const url = `${BASE_URL}/invoices/send`; const url = `invoices/send`;
const response = await fetch(url, { const response = await api.post(url, {
method: 'POST', json: { id: invoiceId }
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id: invoiceId })
}); });
return response.json(); return response.json();
} }
// Users // Users
async function getOneUser(id: string) { async function getOneUser(id: string) {
const url = `${BASE_URL}/users/show?id=${id}`; const url = `users/show?id=${id}`;
const response = await fetch(url); const response = await api.get(url);
return response.json(); return response.json();
} }
async function getAllUsers(params: PaginationParams) { async function getAllUsers(params: PaginationParams) {
const { limit = 5, start = 0, sort_by = 'id', sort_dir = 'asc' } = params; const { limit = 5, start = 0, sort_by = 'id', sort_dir = 'asc' } = params;
const queryParams = new URLSearchParams({ limit: limit.toFixed(0), start: start.toFixed(0), sort_by, sort_dir }); const queryParams = new URLSearchParams({ limit: limit.toFixed(0), start: start.toFixed(0), sort_by, sort_dir });
const url = `${BASE_URL}/users?${queryParams.toString()}`; const url = `users?${queryParams.toString()}`;
const response = await fetch(url); const response = await api.get(url);
return response.json(); return response.json();
} }
async function updateUser(data) { async function updateUser(data) {
const url = `${BASE_URL}/users/edit`; const url = `users/edit`;
const response = await fetch(url, { const response = await api.post(url, {
method: 'POST', json: data
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}); });
return response.json(); return response.json();
} }
async function deleteUser(userId: string) { async function deleteUser(userId: string) {
const url = `${BASE_URL}/users/delete`; const url = `users/delete`;
const response = await fetch(url, { const response = await api.post(url, {
method: 'POST', json: { id: userId }
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id: userId })
}); });
return response.json(); return response.json();
} }