add workspace navbar status + creation modal
This commit is contained in:
42
src/api/workspaces.ts
Normal file
42
src/api/workspaces.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { authFetch } from "./client"
|
||||
|
||||
export interface Workspace {
|
||||
id: string
|
||||
name: string
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
export const fetchWorkspaces = async (): Promise<Workspace[]> => {
|
||||
const response = await authFetch("/api/workspaces/")
|
||||
|
||||
if (!response.ok) {
|
||||
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
|
||||
}
|
||||
|
||||
export const createWorkspace = async (data: { name: string; description: string; members?: any[] }) => {
|
||||
// 1. Only send name and description to the workspaces endpoint
|
||||
const payload = {
|
||||
name: data.name,
|
||||
description: data.description,
|
||||
};
|
||||
|
||||
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');
|
||||
}
|
||||
const newWorkspace = await response.json();
|
||||
return newWorkspace;
|
||||
};
|
||||
Reference in New Issue
Block a user