tududi/frontend/utils/tasksService.ts
Chris 03f38f05dc
Setup intelligence (#84)
* Add next suggestions and remove console logs

* Add pomodoro timer

* Add pomodoro switch in settings

* Fix pomodoro setting

* Add timezones to settings

* Fix an issue with password reset

* Cleanup

* Sort tags alphabetically

* Clean up today's view

* Add an indicator for repeatedly added to today

* Refactor tags

* Add due date today item

* Move recurrence to the subtitle area

* Fix today layout

* Add a badge to Inbox items

* Move inbox badge to sidebar

* Add quotes and progress bar

* Add translations for quotes

* Fix test issues

* Add helper script for docker local

* Set up overdue tasks

* Add  linux/arm/v7 build to deploy script

* Add  linux/arm/v7 build to deploy script pt2

* Fix an issue with helmet and SSL

* Add volume db persistence

* Fix cog icon issues
2025-06-27 14:02:18 +03:00

100 lines
3 KiB
TypeScript

import { Metrics } from "../entities/Metrics";
import { Task } from "../entities/Task";
import { handleAuthResponse, getDefaultHeaders, getPostHeaders } from "./authUtils";
export const fetchTasks = async (query = ''): Promise<{ tasks: Task[]; metrics: Metrics }> => {
const response = await fetch(`/api/tasks${query}`, {
credentials: 'include',
headers: getDefaultHeaders(),
});
await handleAuthResponse(response, 'Failed to fetch tasks.');
const result = await response.json();
if (!Array.isArray(result.tasks)) {
throw new Error('Resulting tasks are not an array.');
}
if (!result.metrics) {
throw new Error('Metrics data is not included.');
}
return { tasks: result.tasks, metrics: result.metrics };
};
export const createTask = async (taskData: Task): Promise<Task> => {
const response = await fetch('/api/task', {
method: 'POST',
credentials: 'include',
headers: getPostHeaders(),
body: JSON.stringify(taskData),
});
await handleAuthResponse(response, 'Failed to create task.');
return await response.json();
};
export const updateTask = async (taskId: number, taskData: Task): Promise<Task> => {
const response = await fetch(`/api/task/${taskId}`, {
method: 'PATCH',
credentials: 'include',
headers: getPostHeaders(),
body: JSON.stringify(taskData),
});
await handleAuthResponse(response, 'Failed to update task.');
return await response.json();
};
export const toggleTaskCompletion = async (taskId: number): Promise<Task> => {
const response = await fetch(`/api/task/${taskId}/toggle_completion`, {
method: 'PATCH',
credentials: 'include',
headers: getPostHeaders(),
});
await handleAuthResponse(response, 'Failed to toggle task completion.');
const result = await response.json();
return result;
};
export const deleteTask = async (taskId: number): Promise<void> => {
const response = await fetch(`/api/task/${taskId}`, {
method: 'DELETE',
credentials: 'include',
headers: getDefaultHeaders(),
});
await handleAuthResponse(response, 'Failed to delete task.');
};
export const fetchTaskById = async (taskId: number): Promise<Task> => {
const response = await fetch(`/api/task/${taskId}`, {
credentials: 'include',
headers: getDefaultHeaders(),
});
await handleAuthResponse(response, 'Failed to fetch task.');
return await response.json();
};
export const fetchTaskByUuid = async (uuid: string): Promise<Task> => {
const response = await fetch(`/api/task/uuid/${uuid}`, {
credentials: 'include',
headers: getDefaultHeaders(),
});
await handleAuthResponse(response, 'Failed to fetch task.');
return await response.json();
};
export const toggleTaskToday = async (taskId: number): Promise<Task> => {
const response = await fetch(`/api/task/${taskId}/toggle-today`, {
method: 'PATCH',
credentials: 'include',
headers: getPostHeaders(),
});
await handleAuthResponse(response, 'Failed to toggle task today status.');
return await response.json();
};