Files
qlockify-frontend-deployment/src/api/workspaces.ts

67 lines
2.1 KiB
TypeScript

// src/api/workspaces.ts
import { authFetch } from "./client";
export interface Workspace {
id: string;
name: string;
description?: string;
owner?: string;
my_role?: 'owner' | 'admin' | 'member' | 'guest';
[key: string]: any;
}
export const fetchWorkspaces = async (params?: Record<string, string>): Promise<Workspace[]> => {
const query = params ? new URLSearchParams(params).toString() : '';
const url = `/api/workspaces/${query ? `?${query}` : ''}`;
const response = await authFetch(url);
if (!response.ok) {
throw new Error("Failed to fetch workspaces");
}
const data = await response.json();
return data.results || data;
};
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 response = await authFetch('/api/workspaces/', {
method: 'POST',
body: JSON.stringify(data),
});
if (!response.ok) {
const errorData = await response.json();
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',
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');
}
};