All checks were successful
Deploy to Production / deploy (push) Successful in 1m5s
Backend: - InspectionSession + InspectionRecord models with alembic migration - 6 API endpoints: create, list, get detail, save records, complete, delete - Auto pass/fail judgment for numeric (spec range) and boolean items - Completed inspections are immutable, required items enforced on complete - 14 new tests (total 53/53 passed) Frontend: - Inspection list page with in_progress/completed tabs - Template select modal for starting new inspections - Inspection execution page with data-type-specific inputs - Auto-save with 1.5s debounce, manual save button - Completion modal with notes and required item validation - Read-only view for completed inspections - Pass/fail badges and color-coded item cards Co-Authored-By: Claude Opus 4 <noreply@anthropic.com>
147 lines
3.7 KiB
TypeScript
147 lines
3.7 KiB
TypeScript
import useSWR from 'swr';
|
|
import { fetcher, getTenantUrl } from './api';
|
|
import type { Tenant, Machine, MachineDetail, EquipmentPart, InspectionTemplate, InspectionSession } 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,
|
|
};
|
|
}
|
|
|
|
export function useInspections(tenantId?: string, status?: string, templateId?: string) {
|
|
const params = new URLSearchParams();
|
|
if (status) params.set('status', status);
|
|
if (templateId) params.set('template_id', templateId);
|
|
const qs = params.toString();
|
|
const url = tenantId ? `/api/${tenantId}/inspections${qs ? `?${qs}` : ''}` : null;
|
|
const { data, error, isLoading, mutate } = useSWR<{ inspections: InspectionSession[] }>(
|
|
url,
|
|
fetcher,
|
|
{ refreshInterval: 10000, dedupingInterval: 2000 },
|
|
);
|
|
|
|
return {
|
|
inspections: data?.inspections || [],
|
|
error,
|
|
isLoading,
|
|
mutate,
|
|
};
|
|
}
|
|
|
|
export function useInspection(tenantId?: string, inspectionId?: string) {
|
|
const url = tenantId && inspectionId ? `/api/${tenantId}/inspections/${inspectionId}` : null;
|
|
const { data, error, isLoading, mutate } = useSWR<InspectionSession>(
|
|
url,
|
|
fetcher,
|
|
{ refreshInterval: 5000, dedupingInterval: 2000 },
|
|
);
|
|
|
|
return {
|
|
inspection: data ?? null,
|
|
error,
|
|
isLoading,
|
|
mutate,
|
|
};
|
|
}
|