/** * Dashboard API client functions. * All calls use credentials: 'include' for cookie-based auth. */ const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001'; // --- Reiter (quick-260923-ad9) --------------------------------------------- export interface DashboardTab { id: string; name: string; position: number; } export async function fetchDashboards(): Promise { const res = await fetch(`${API_URL}/dashboard/tabs`, { credentials: 'include', }); if (!res.ok) throw new Error('Failed to fetch dashboards'); return res.json(); } export async function createDashboardTab(): Promise { const res = await fetch(`${API_URL}/dashboard/tabs`, { method: 'POST', credentials: 'include', }); if (!res.ok) throw new Error('Failed to create dashboard'); return res.json(); } export async function renameDashboardTab(id: string, name: string): Promise { const res = await fetch(`${API_URL}/dashboard/tabs/${id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ name }), }); if (!res.ok) throw new Error('Failed to rename dashboard'); return res.json(); } export async function deleteDashboardTab(id: string): Promise { const res = await fetch(`${API_URL}/dashboard/tabs/${id}`, { method: 'DELETE', credentials: 'include', }); if (!res.ok) throw new Error('Failed to delete dashboard'); } export async function reorderDashboardTabs(ids: string[]): Promise { const res = await fetch(`${API_URL}/dashboard/tabs/order`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ ids }), }); if (!res.ok) throw new Error('Failed to reorder dashboards'); return res.json(); } // --- Anordnung und Kacheln — je Reiter (quick-260923-ad9) ------------------ export async function fetchLayout(dashboardId: string): Promise> { const res = await fetch( `${API_URL}/dashboard/layout?dashboardId=${encodeURIComponent(dashboardId)}`, { credentials: 'include' }, ); if (!res.ok) throw new Error('Failed to fetch layout'); return res.json(); } export async function saveLayout( dashboardId: string, layouts: Record, ): Promise { const res = await fetch(`${API_URL}/dashboard/layout`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ dashboardId, layouts }), }); if (!res.ok) throw new Error('Failed to save layout'); } export async function fetchWidgets( dashboardId: string, ): Promise }>> { const res = await fetch( `${API_URL}/dashboard/widgets?dashboardId=${encodeURIComponent(dashboardId)}`, { credentials: 'include' }, ); if (!res.ok) throw new Error('Failed to fetch widgets'); return res.json(); } export async function addWidget( dashboardId: string, widgetType: string, ): Promise<{ id: string; widgetType: string; config: Record }> { const res = await fetch(`${API_URL}/dashboard/widgets`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ dashboardId, widgetType }), }); if (!res.ok) throw new Error('Failed to add widget'); return res.json(); } export async function removeWidget(id: string): Promise { const res = await fetch(`${API_URL}/dashboard/widgets/${id}`, { method: 'DELETE', credentials: 'include', }); if (!res.ok) throw new Error('Failed to remove widget'); } export async function updateWidgetConfig( id: string, config: Record, signal?: AbortSignal, ): Promise { const res = await fetch(`${API_URL}/dashboard/widgets/${id}/config`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ config }), signal, }); if (!res.ok) throw new Error('Failed to update widget config'); } // --- Search Provider API (05-02) --- export interface SearchProviderResponse { id: string; name: string; urlTemplate: string; isDefault: boolean; userId?: string | null; } export async function fetchSearchProviders(): Promise { const res = await fetch(`${API_URL}/dashboard/search-providers`, { credentials: 'include', }); if (!res.ok) throw new Error('Failed to fetch search providers'); return res.json(); } export async function addSearchProvider(payload: { name: string; urlTemplate: string; }): Promise { const res = await fetch(`${API_URL}/dashboard/search-providers`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify(payload), }); if (!res.ok) throw new Error('Failed to add search provider'); return res.json(); } export async function removeSearchProvider(id: string): Promise { const res = await fetch(`${API_URL}/dashboard/search-providers/${id}`, { method: 'DELETE', credentials: 'include', }); if (!res.ok) throw new Error('Failed to remove search provider'); }