mirasim's picture
Add the tasks, judges, harness, app under test, docs and report; merge the repository README into the card (part 5)
3eb5a0c verified
Raw History Blame Contribute Delete
958 Bytes
import type { Comment, NewTask, Task, TaskPatch, TaskStatus } from "../types";
import { api } from "./client";
// Tasks saved before statuses existed carry only a `done` flag.
type TaskRecord = Omit<Task, "status"> & { status?: TaskStatus; done?: boolean };
const toTask = ({ done, ...t }: TaskRecord): Task => ({ ...t, status: t.status ?? (done ? "done" : "todo") });
export const tasksApi = {
list: (query = "") =>
api.get<TaskRecord[]>(query ? `/tasks?q=${encodeURIComponent(query)}` : "/tasks").then((ts) => ts.map(toTask)),
get: (id: number) => api.get<TaskRecord>(`/tasks/${id}`).then(toTask),
create: (task: NewTask) => api.post<TaskRecord>("/tasks", { ...task, status: "todo" }).then(toTask),
update: (id: number, patch: TaskPatch) => api.patch<TaskRecord>(`/tasks/${id}`, patch).then(toTask),
remove: (id: number) => api.del<{ ok: true }>(`/tasks/${id}`),
comments: (id: number) => api.get<Comment[]>(`/tasks/${id}/comments`),
};