feat(projects): add Projects page and component modals + translations
This commit is contained in:
97
src/api/projects.ts
Normal file
97
src/api/projects.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
import { authFetch } from "./client";
|
||||
|
||||
export interface ProjectClient {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface Project {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
color: string;
|
||||
is_archived: boolean;
|
||||
workspace: string;
|
||||
client: ProjectClient | null;
|
||||
}
|
||||
|
||||
export interface ProjectPayload {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
color: string;
|
||||
is_archived: boolean;
|
||||
workspace: string;
|
||||
client: string | null;
|
||||
}
|
||||
|
||||
export const getProjects = async (
|
||||
workspaceId: string,
|
||||
params: { limit?: number; offset?: number; search?: string; client?: string; is_archived?: boolean, ordering?: string } = {}
|
||||
) => {
|
||||
const queryParams = new URLSearchParams({ workspace: workspaceId });
|
||||
|
||||
if (params.limit !== undefined) queryParams.append("limit", params.limit.toString());
|
||||
if (params.offset !== undefined) queryParams.append("offset", params.offset.toString());
|
||||
if (params.search) queryParams.append("search", params.search);
|
||||
if (params.client) queryParams.append("client", params.client);
|
||||
if (params.is_archived !== undefined) queryParams.append("is_archived", params.is_archived.toString());
|
||||
if (params.ordering !== undefined) queryParams.append("ordering", params.ordering.toString());
|
||||
|
||||
const response = await authFetch(`/api/projects/?${queryParams.toString()}`);
|
||||
|
||||
if (!response.ok) throw new Error("Failed to fetch projects");
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const createProject = async (data: Partial<ProjectPayload> & { workspace: string; name: string }) => {
|
||||
const response = await authFetch("/api/projects/", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => null);
|
||||
throw new Error(errorData?.detail || errorData?.message || "Failed to create project");
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const updateProject = async (id: string, data: Partial<ProjectPayload>) => {
|
||||
const response = await authFetch(`/api/projects/${id}/`, {
|
||||
method: "PATCH",
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => null);
|
||||
throw new Error(errorData?.detail || errorData?.message || "Failed to update project");
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const deleteProject = async (id: string) => {
|
||||
const response = await authFetch(`/api/projects/${id}/`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => null);
|
||||
throw new Error(errorData?.detail || errorData?.message || "Failed to delete project");
|
||||
}
|
||||
|
||||
if (response.status === 204) return { success: true };
|
||||
return response.json().catch(() => ({ success: true }));
|
||||
};
|
||||
|
||||
export const toggleArchiveProject = async (id: string) => {
|
||||
const response = await authFetch(`/api/projects/${id}/archive/`, {
|
||||
method: "POST",
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => null);
|
||||
throw new Error(errorData?.detail || errorData?.message || `Failed to archive project`);
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
Reference in New Issue
Block a user