Refactor task list example application

This commit is contained in:
Steve Kinney
2024-09-18 07:52:42 -06:00
parent e29c63f5cb
commit 2a8720330e
26 changed files with 535 additions and 326 deletions

View File

@@ -0,0 +1,50 @@
import { Task } from './types';
export const all = async (): Promise<Task[]> => {
const response = await fetch('/api/tasks');
if (!response.ok) {
throw new Error('Failed to fetch tasks');
}
return response.json();
};
export const add = async (title: string): Promise<Task> => {
const response = await fetch('/api/tasks', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title }),
});
if (!response.ok) {
throw new Error('Failed to add task');
}
return response.json();
};
export const update = async (
id: string,
updatedTask: Partial<Task>,
): Promise<void> => {
const response = await fetch(`/api/tasks/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(updatedTask),
});
if (!response.ok) {
throw new Error('Failed to update task');
}
};
export const remove = async (id: string): Promise<void> => {
const response = await fetch(`/api/tasks/${id}`, {
method: 'DELETE',
});
if (!response.ok) {
throw new Error('Failed to delete task');
}
};