fix(machines): fix skeleton creation — load all items + atomic creation

- Load composants/pieces/products with itemsPerPage: 200 instead of 30
  (root cause: only first 30 items were available in creation dropdowns)
- Rollback machine if skeleton PATCH fails (delete orphaned machine)
- Initialize custom fields after successful machine creation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Matthieu
2026-02-18 10:39:45 +01:00
parent dbf8c8856b
commit 6bed715b7f

View File

@@ -34,7 +34,7 @@ export function useMachineCreatePage() {
// Composable calls // Composable calls
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
const { createMachine, createMachineFromType, reconfigureSkeleton } = useMachines() const { createMachine, createMachineFromType, reconfigureSkeleton, addMissingCustomFields, deleteMachine } = useMachines()
const { sites, loadSites } = useSites() const { sites, loadSites } = useSites()
const { machineTypes, loadMachineTypes, loading: machineTypesLoading } = useMachineTypesApi() const { machineTypes, loadMachineTypes, loading: machineTypesLoading } = useMachineTypesApi()
const { composants, loadComposants, loading: composantsLoading } = useComposants() const { composants, loadComposants, loading: composantsLoading } = useComposants()
@@ -340,17 +340,24 @@ export function useMachineCreatePage() {
: await createMachineFromType(baseMachineData as any, type) : await createMachineFromType(baseMachineData as any, type)
if (result.success) { if (result.success) {
if (hasRequirements && result.data?.id) { const machineId = result.data?.id
const skeletonResult: any = await reconfigureSkeleton(result.data.id, { if (hasRequirements && machineId) {
const skeletonResult: any = await reconfigureSkeleton(machineId, {
componentLinks, componentLinks,
pieceLinks, pieceLinks,
productLinks, productLinks,
} as any) } as any)
if (!skeletonResult.success) { if (!skeletonResult.success) {
toast.showError(skeletonResult.error || 'Impossible d\'enregistrer les pièces/composants') // Rollback: delete the orphaned machine
await deleteMachine(machineId).catch(() => {})
toast.showError(skeletonResult.error || 'Impossible d\'enregistrer les pièces/composants. La machine n\'a pas été créée.')
return return
} }
} }
// Initialize custom fields for the machine type
if (machineId) {
await addMissingCustomFields(machineId, { showToast: false }).catch(() => {})
}
newMachine.name = '' newMachine.name = ''
newMachine.siteId = '' newMachine.siteId = ''
newMachine.typeMachineId = '' newMachine.typeMachineId = ''
@@ -386,9 +393,9 @@ export function useMachineCreatePage() {
await Promise.all([ await Promise.all([
loadSites(), loadSites(),
loadMachineTypes(), loadMachineTypes(),
loadComposants(), loadComposants({ itemsPerPage: 200, force: true }),
loadPieces(), loadPieces({ itemsPerPage: 200, force: true }),
loadProducts(), loadProducts({ itemsPerPage: 200, force: true }),
]) ])
}) })