tududi/frontend/utils/projectsService.ts
Chris bd6d176666
Add sorting projects (#175)
* Add sorting dropdown to projects list

* Fix lint issues

* fixup! Fix lint issues

* Fix new task space and completed expose

* Fix completed switch and filters

* Fix an issue with projects not appearing as links

* Fix an issue with modal appearing relative to task list

* Remove obsolete loads

* Fix list issues

* Fix mobile/desktop settings dropdown functionality

* Update project modal layout

* Realign project options

* Fix tags dropdown in Project modal

* Fix inbox amount sidebar layout

* Fix project banner tags listing

* Remove icons from titles

* Fix lint errors

* fixup! Fix lint errors

* fixup! fixup! Fix lint errors

* Beautify notes and areas

* Experiment with new notes layout

* fixup! Experiment with new notes layout

* fixup! fixup! Experiment with new notes layout

* Fix note layout

* fixup! Fix note layout

* Fix an issue with adding an area and refreshing
the area list

* Fix an issue with project edit delete menu

* fixup! Fix an issue with project edit delete menu

* Fix an issue with projects create

* Update interaction with Notes

* Update interaction with tags

* fixup! Update interaction with tags

* Fix lint issues

* Extract shared filter

* Add sorting dropdown translations

* fixup! Add sorting dropdown translations

* fixup! fixup! Add sorting dropdown translations
2025-07-21 18:41:11 +03:00

120 lines
3.4 KiB
TypeScript

import { Project } from '../entities/Project';
import { handleAuthResponse } from './authUtils';
export const fetchProjects = async (
activeFilter = 'all',
areaFilter = ''
): Promise<Project[]> => {
let url = `/api/projects`;
const params = new URLSearchParams();
if (activeFilter !== 'all') params.append('active', activeFilter);
if (areaFilter) params.append('area_id', areaFilter);
if (params.toString()) url += `?${params.toString()}`;
const response = await fetch(url, {
credentials: 'include',
headers: { Accept: 'application/json' },
});
await handleAuthResponse(response, 'Failed to fetch projects.');
const data = await response.json();
return data.projects || data;
};
export const fetchGroupedProjects = async (
activeFilter = 'all',
areaFilter = ''
): Promise<Record<string, Project[]>> => {
let url = `/api/projects`;
const params = new URLSearchParams();
params.append('grouped', 'true');
if (activeFilter !== 'all') params.append('active', activeFilter);
if (areaFilter) params.append('area_id', areaFilter);
if (params.toString()) url += `?${params.toString()}`;
const response = await fetch(url, {
credentials: 'include',
headers: { Accept: 'application/json' },
});
await handleAuthResponse(response, 'Failed to fetch projects.');
const data = await response.json();
return data;
};
export const fetchProjectById = async (
projectId: string,
options?: {
sort?: string;
completed?: boolean;
}
): Promise<Project> => {
const url = new URL(`/api/project/${projectId}`, window.location.origin);
if (options?.sort) {
url.searchParams.set('sort', options.sort);
}
if (options?.completed !== undefined) {
url.searchParams.set('completed', options.completed.toString());
}
const response = await fetch(url.toString(), {
credentials: 'include',
headers: { Accept: 'application/json' },
});
await handleAuthResponse(response, 'Failed to fetch project details.');
return await response.json();
};
export const createProject = async (
projectData: Partial<Project>
): Promise<Project> => {
const response = await fetch('/api/project', {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify(projectData),
});
await handleAuthResponse(response, 'Failed to create project.');
return await response.json();
};
export const updateProject = async (
projectId: number,
projectData: Partial<Project>
): Promise<Project> => {
const response = await fetch(`/api/project/${projectId}`, {
method: 'PATCH',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify(projectData),
});
await handleAuthResponse(response, 'Failed to update project.');
return await response.json();
};
export const deleteProject = async (projectId: number): Promise<void> => {
const response = await fetch(`/api/project/${projectId}`, {
method: 'DELETE',
credentials: 'include',
headers: {
Accept: 'application/json',
},
});
await handleAuthResponse(response, 'Failed to delete project.');
};