97 lines
2.4 KiB
TypeScript
97 lines
2.4 KiB
TypeScript
import { pb } from "./pb";
|
|
import { ListsResponse, TasksResponse } from "./pocketbase-types";
|
|
|
|
export interface ListData {
|
|
id: string;
|
|
name: string;
|
|
parentId: string | null;
|
|
lists: Array<{
|
|
id: string;
|
|
name: string;
|
|
listCount: number;
|
|
taskCount: number;
|
|
}>;
|
|
tasks: Array<TasksResponse>;
|
|
}
|
|
|
|
export async function listFetcher(id: string): Promise<ListData> {
|
|
interface Expand {
|
|
lists_via_parent: unknown[];
|
|
tasks_via_list: unknown[];
|
|
}
|
|
type ListsResponseExpand = ListsResponse<Expand>;
|
|
|
|
if (id === "all") {
|
|
const lists = await pb
|
|
.collection<ListsResponseExpand>("lists")
|
|
.getList(0, 50, {
|
|
filter: pb.filter("parent = null"),
|
|
expand: "lists_via_parent,tasks_via_list",
|
|
});
|
|
const tasks = await pb.collection("tasks").getList(0, 50, {
|
|
filter: pb.filter("list = null"),
|
|
});
|
|
return {
|
|
id,
|
|
name: "Lists",
|
|
parentId: null,
|
|
lists: lists.items.map((l) => ({
|
|
id: l.id,
|
|
name: l.name,
|
|
listCount: l.expand?.["lists_via_parent"]?.length || 0,
|
|
taskCount: l.expand?.["tasks_via_list"]?.length || 0,
|
|
})),
|
|
tasks: tasks.items,
|
|
};
|
|
}
|
|
|
|
const list = await pb.collection("lists").getOne(id);
|
|
const lists = await pb
|
|
.collection<ListsResponseExpand>("lists")
|
|
.getList(0, 50, {
|
|
filter: pb.filter("parent = {:id}", { id }),
|
|
expand: "lists_via_parent,tasks_via_list",
|
|
});
|
|
const tasks = await pb.collection("tasks").getList(0, 50, {
|
|
filter: pb.filter("list = {:id}", { id }),
|
|
});
|
|
return {
|
|
id,
|
|
name: list.name,
|
|
parentId: list.parent,
|
|
lists: lists.items.map((l) => ({
|
|
id: l.id,
|
|
name: l.name,
|
|
listCount: l.expand?.["lists_via_parent"]?.length || 0,
|
|
taskCount: l.expand?.["tasks_via_list"]?.length || 0,
|
|
})),
|
|
tasks: tasks.items,
|
|
};
|
|
}
|
|
|
|
export interface TaskData extends TasksResponse {
|
|
listId: string;
|
|
imageSource: string;
|
|
}
|
|
|
|
export async function taskFetcher(id: string): Promise<TaskData> {
|
|
const task = await pb.collection("tasks").getOne(id);
|
|
const icon = await pb.collection("icons").getOne(task.icon);
|
|
const imageSource = pb.getFileUrl(icon, icon.image, {
|
|
thumb: "300x300",
|
|
});
|
|
return {
|
|
...task,
|
|
listId: task.list,
|
|
imageSource,
|
|
};
|
|
}
|
|
|
|
export interface RandomTaskData {
|
|
id: string;
|
|
name: string;
|
|
}
|
|
|
|
export async function randomTask(parent: string | null) {
|
|
return await pb.send<RandomTaskData>(`/api/extras/random/${parent}`, {});
|
|
}
|