All checks were successful
Deploy to Production / deploy (push) Successful in 1m7s
- Add InspectionTemplate and InspectionTemplateItem models - Add 9 API endpoints for template CRUD and item management - Add Alembic migration for inspection_templates tables - Add 15 backend tests (39/39 total pass) - Add TemplateEditor component with item management UI - Add templates list, create, and edit pages - Add tab bar, template grid, form row CSS classes - Fix CORS middleware ordering in main.py - Move CORS middleware before router registration Co-Authored-By: Claude Opus 4 <noreply@anthropic.com>
111 lines
2.7 KiB
TypeScript
111 lines
2.7 KiB
TypeScript
import useSWR from 'swr';
|
|
import { fetcher, getTenantUrl } from './api';
|
|
import type { Tenant, Machine, MachineDetail, EquipmentPart, InspectionTemplate } from './types';
|
|
|
|
export function useTenants() {
|
|
const { data, error, isLoading, mutate } = useSWR<{ tenants: Tenant[] }>(
|
|
'/api/tenants',
|
|
fetcher,
|
|
);
|
|
|
|
return {
|
|
tenants: data?.tenants || [],
|
|
error,
|
|
isLoading,
|
|
mutate,
|
|
};
|
|
}
|
|
|
|
export function useTenantData<T>(path: string, tenantId?: string) {
|
|
const url = tenantId ? getTenantUrl(path, tenantId) : null;
|
|
const { data, error, isLoading, mutate } = useSWR<T>(url, fetcher);
|
|
|
|
return {
|
|
data: data ?? null,
|
|
error,
|
|
isLoading,
|
|
mutate,
|
|
};
|
|
}
|
|
|
|
export function useMachines(tenantId?: string) {
|
|
const url = tenantId ? `/api/${tenantId}/machines` : null;
|
|
const { data, error, isLoading, mutate } = useSWR<{ machines: Machine[] }>(
|
|
url,
|
|
fetcher,
|
|
{ refreshInterval: 30000, dedupingInterval: 2000 },
|
|
);
|
|
|
|
return {
|
|
machines: data?.machines || [],
|
|
error,
|
|
isLoading,
|
|
mutate,
|
|
};
|
|
}
|
|
|
|
export function useMachine(tenantId?: string, machineId?: string) {
|
|
const url = tenantId && machineId ? `/api/${tenantId}/machines/${machineId}` : null;
|
|
const { data, error, isLoading, mutate } = useSWR<MachineDetail>(
|
|
url,
|
|
fetcher,
|
|
{ refreshInterval: 30000, dedupingInterval: 2000 },
|
|
);
|
|
|
|
return {
|
|
machine: data ?? null,
|
|
error,
|
|
isLoading,
|
|
mutate,
|
|
};
|
|
}
|
|
|
|
export function useEquipmentParts(tenantId?: string, machineId?: string) {
|
|
const url = tenantId && machineId ? `/api/${tenantId}/machines/${machineId}/parts` : null;
|
|
const { data, error, isLoading, mutate } = useSWR<{ parts: EquipmentPart[] }>(
|
|
url,
|
|
fetcher,
|
|
{ refreshInterval: 30000, dedupingInterval: 2000 },
|
|
);
|
|
|
|
return {
|
|
parts: data?.parts || [],
|
|
error,
|
|
isLoading,
|
|
mutate,
|
|
};
|
|
}
|
|
|
|
export function useTemplates(tenantId?: string, subjectType?: string) {
|
|
const params = subjectType ? `?subject_type=${subjectType}` : '';
|
|
const url = tenantId ? `/api/${tenantId}/templates${params}` : null;
|
|
const { data, error, isLoading, mutate } = useSWR<{ templates: InspectionTemplate[] }>(
|
|
url,
|
|
fetcher,
|
|
{ refreshInterval: 30000, dedupingInterval: 2000 },
|
|
);
|
|
|
|
return {
|
|
templates: data?.templates || [],
|
|
error,
|
|
isLoading,
|
|
mutate,
|
|
};
|
|
}
|
|
|
|
export function useTemplate(tenantId?: string, templateId?: string) {
|
|
const url = tenantId && templateId ? `/api/${tenantId}/templates/${templateId}` : null;
|
|
const { data, error, isLoading, mutate } = useSWR<InspectionTemplate>(
|
|
url,
|
|
fetcher,
|
|
{ refreshInterval: 30000, dedupingInterval: 2000 },
|
|
);
|
|
|
|
return {
|
|
template: data ?? null,
|
|
error,
|
|
isLoading,
|
|
mutate,
|
|
};
|
|
}
|