feat(workspaces): add workspace management page

This commit is contained in:
2026-03-12 20:51:41 +08:00
parent 2ffd1efc61
commit 5e4f4ec6e4
9 changed files with 224 additions and 33 deletions

View File

@@ -1,42 +1,75 @@
import { authFetch } from "./client"
import { authFetch } from "./client";
export interface Workspace {
id: string
name: string
[key: string]: any
id: string;
name: string;
description?: string;
owner?: string;
my_role?: 'owner' | 'admin' | 'member' | 'guest';
[key: string]: any;
}
export const fetchWorkspaces = async (): Promise<Workspace[]> => {
const response = await authFetch("/api/workspaces/")
const response = await authFetch("/api/workspaces/");
if (!response.ok) {
throw new Error("Failed to fetch workspaces")
throw new Error("Failed to fetch workspaces");
}
const data = await response.json()
// Handles paginated responses if the API returns { count, next, previous, results: [...] }
return data.results || data
}
const data = await response.json();
return data.results || data;
};
export const createWorkspace = async (data: { name: string; description: string; members?: any[] }) => {
// 1. Only send name and description to the workspaces endpoint
export const getWorkspace = async (id: string): Promise<Workspace> => {
const response = await authFetch(`/api/workspaces/${id}/`);
if (!response.ok) {
throw new Error("Failed to fetch workspace details");
}
return await response.json();
};
export const createWorkspace = async (data: { name: string; description: string; members?: any[] }): Promise<Workspace> => {
const payload = {
name: data.name,
description: data.description,
members: data.members,
};
const response = await authFetch('/api/workspaces/', {
method: 'POST',
headers: {
'Content-Type': 'application/json', // CRITICAL for DRF to parse the string correctly
},
body: JSON.stringify(payload),
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || 'Failed to create workspace');
throw new Error(errorData.error || JSON.stringify(errorData) || 'Failed to create workspace');
}
return await response.json();
};
export const updateWorkspace = async (id: string, data: { name?: string; description?: string }): Promise<Workspace> => {
const response = await authFetch(`/api/workspaces/${id}/`, {
method: 'PATCH', // Using PATCH as defined in API docs for partial updates
body: JSON.stringify(data),
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || JSON.stringify(errorData) || 'Failed to update workspace');
}
return await response.json();
};
export const deleteWorkspace = async (id: string): Promise<void> => {
const response = await authFetch(`/api/workspaces/${id}/`, {
method: 'DELETE',
});
if (!response.ok) {
throw new Error('Failed to delete workspace');
}
const newWorkspace = await response.json();
return newWorkspace;
};