mirror of
https://github.com/gethomepage/homepage.git
synced 2025-12-07 09:35:54 -08:00
reduce API calls by filtering in widget.js
This commit is contained in:
@@ -5,15 +5,15 @@ description: Vikunja Widget Configuration
|
||||
|
||||
Learn more about [Vikunja](https://vikunja.io).
|
||||
|
||||
Allowed fields: `["projects", "tasks", "overdue", "inprogress"]`.
|
||||
Allowed fields: `["projects", "tasks7d", "tasksOverdue", "tasksInProgress"]`.
|
||||
|
||||
"Projects" lists the number of non-archived Projects the user has access to.
|
||||
|
||||
"Tasks" lists the number of tasks due within the next 7 days.
|
||||
"Tasks 7d" lists the number of tasks due within the next 7 days.
|
||||
|
||||
"Overdue" lists the number of all tasks overdue.
|
||||
"Tasks Overdue" lists the number of all tasks overdue.
|
||||
|
||||
"In Progress" lists the number of tasks with a progress percentage above 0% and below 100%.
|
||||
"Tasks In Progress" lists the number of tasks with a progress percentage above 0% and below 100%.
|
||||
|
||||
```yaml
|
||||
widget:
|
||||
|
||||
@@ -955,9 +955,9 @@
|
||||
"none": "None"
|
||||
},
|
||||
"vikunja": {
|
||||
"projects": "Total Active Projects",
|
||||
"tasks": "Tasks Due This Week",
|
||||
"overdue": "Overdue Tasks",
|
||||
"inprogress": "Tasks In Progress"
|
||||
"projects": "Active Projects",
|
||||
"tasks7d": "Tasks Due This Week",
|
||||
"tasksOverdue": "Overdue Tasks",
|
||||
"tasksInProgress": "Tasks In Progress"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,42 +6,38 @@ import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
|
||||
const { data: projectsData, error: projectsError } = useWidgetAPI(widget, "projects");
|
||||
const { data: tasksData, error: tasksError } = useWidgetAPI(widget, "tasks", {
|
||||
filter: "done=false&&due_date<=now+7d",
|
||||
});
|
||||
const { data: overdueData, error: overdueError } = useWidgetAPI(widget, "tasks", {
|
||||
filter: "done=false&&due_date<=now/d+1d",
|
||||
});
|
||||
const { data: inProgressData, error: inProgressError } = useWidgetAPI(widget, "tasks", {
|
||||
filter: "done=false&&percent_done>0&&percent_done<100",
|
||||
filter: "done=false&&percent_done<1",
|
||||
sort_by: "due_date",
|
||||
});
|
||||
|
||||
if (projectsError || tasksError || overdueError || inProgressError) {
|
||||
const vikunjaError = projectsError ?? tasksError ?? overdueError ?? inProgressError;
|
||||
if (projectsError || tasksError) {
|
||||
const vikunjaError = projectsError ?? tasksError;
|
||||
return <Container service={service} error={vikunjaError} />;
|
||||
}
|
||||
|
||||
if (!projectsData || !tasksData || !overdueData || !inProgressData) {
|
||||
if (!projectsData || !tasksData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="vikunja.projects" />
|
||||
<Block label="vikunja.tasks" />
|
||||
<Block label="vikunja.overdue" />
|
||||
<Block label="vikunja.inprogress" />
|
||||
<Block label="vikunja.tasks7d" />
|
||||
<Block label="vikunja.tasksOverdue" />
|
||||
<Block label="vikunja.tasksInProgress" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
const projects = projectsData.length;
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="vikunja.projects" value={t("common.number", { value: projectsData.projects })} />
|
||||
<Block label="vikunja.tasks" value={t("common.number", { value: tasksData.tasks })} />
|
||||
<Block label="vikunja.overdue" value={t("common.number", { value: overdueData.tasks })} />
|
||||
<Block label="vikunja.inprogress" value={t("common.number", { value: inProgressData.tasks })} />
|
||||
<Block label="vikunja.projects" value={t("common.number", { value: projects })} />
|
||||
<Block label="vikunja.tasks7d" value={t("common.number", { value: tasksData.tasks7d })} />
|
||||
<Block label="vikunja.tasksOverdue" value={t("common.number", { value: tasksData.overdue })} />
|
||||
<Block label="vikunja.tasksInProgress" value={t("common.number", { value: tasksData.inProgress })} />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -8,16 +8,25 @@ const widget = {
|
||||
mappings: {
|
||||
projects: {
|
||||
endpoint: "projects",
|
||||
map: (data) => ({
|
||||
projects: jsonArrayFilter(data, (item) => !item.isArchived).length,
|
||||
}),
|
||||
},
|
||||
tasks: {
|
||||
endpoint: "tasks/all",
|
||||
// TODO: use filter (and other params?) to allow customizing fields/blocks
|
||||
params: ["filter"],
|
||||
params: ["filter", "sort_by"],
|
||||
map: (data) => ({
|
||||
tasks: jsonArrayFilter(data, (item) => !item.done).length,
|
||||
tasks7d: jsonArrayFilter(
|
||||
data,
|
||||
(item) =>
|
||||
new Date(item.due_date).valueOf() > 978307168000 &&
|
||||
new Date(item.due_date).valueOf() <= new Date(Date.now() + 640800000).valueOf(),
|
||||
).length,
|
||||
inProgress: jsonArrayFilter(data, (item) => !item.done && item.percent_done > 0 && item.percent_done < 1)
|
||||
.length,
|
||||
overdue: jsonArrayFilter(
|
||||
data,
|
||||
(item) =>
|
||||
new Date(item.due_date).valueOf() > 978307168000 &&
|
||||
new Date(item.due_date).valueOf() <= new Date(Date.now()),
|
||||
).length,
|
||||
}),
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user