Random tasks, more info
This commit is contained in:
parent
9187461ed4
commit
f93ad89194
7 changed files with 288 additions and 100 deletions
93
app/src/api/fetcher.ts
Normal file
93
app/src/api/fetcher.ts
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
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> {
|
||||
type Expand = { "lists(parent)": unknown[]; "tasks(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(parent),tasks(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(parent)"]?.length || 0,
|
||||
taskCount: l.expand?.["tasks(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(parent),tasks(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(parent)"]?.length || 0,
|
||||
taskCount: l.expand?.["tasks(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}`, {});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue