Files
Inventory/frontend/app/composables/useMachineCreatePage.ts
Matthieu 974a4a0781 refactor : merge Inventory_frontend submodule into frontend/ directory
Merges the full git history of Inventory_frontend into the monorepo
under frontend/. Removes the submodule in favor of a unified repo.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-01 14:17:57 +02:00

124 lines
3.5 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Machine creation page orchestration composable.
*
* Simplified: no more TypeMachine / skeleton system.
* Supports direct creation or cloning from an existing machine.
*/
import { ref, reactive, onMounted } from 'vue'
import { useMachines } from '~/composables/useMachines'
import { useSites } from '~/composables/useSites'
import { useToast } from '~/composables/useToast'
import { humanizeError } from '~/shared/utils/errorMessages'
export function useMachineCreatePage() {
// ---------------------------------------------------------------------------
// Composable calls
// ---------------------------------------------------------------------------
const { machines, loadMachines, createMachine, cloneMachine } = useMachines()
const { sites, loadSites } = useSites()
const toast = useToast()
// ---------------------------------------------------------------------------
// Local state
// ---------------------------------------------------------------------------
const submitting = ref(false)
const loading = ref(true)
const newMachine = reactive({
name: '',
siteId: '',
reference: '',
cloneFromMachineId: '',
})
// ---------------------------------------------------------------------------
// Machine creation
// ---------------------------------------------------------------------------
const finalizeMachineCreation = async () => {
if (submitting.value) return
if (!newMachine.name?.trim()) {
toast.showError('Merci de renseigner un nom pour la machine')
return
}
submitting.value = true
try {
let result: any
if (newMachine.cloneFromMachineId) {
result = await cloneMachine(newMachine.cloneFromMachineId, {
name: newMachine.name,
siteId: newMachine.siteId,
...(newMachine.reference ? { reference: newMachine.reference } : {}),
})
} else {
result = await createMachine({
name: newMachine.name,
siteId: newMachine.siteId || undefined,
reference: newMachine.reference || undefined,
} as any)
}
if (result.success) {
const machineId = result.data?.id
|| (result.data?.machine as any)?.id
|| null
newMachine.name = ''
newMachine.siteId = ''
newMachine.reference = ''
newMachine.cloneFromMachineId = ''
if (machineId) {
await navigateTo(`/machine/${machineId}`)
} else {
await navigateTo('/machines')
}
} else if (result.error) {
toast.showError(`Impossible de créer la machine : ${humanizeError(result.error)}`)
}
} catch (error: any) {
toast.showError(`Impossible de créer la machine : ${humanizeError(error.message)}`)
} finally {
submitting.value = false
}
}
// ---------------------------------------------------------------------------
// Lifecycle
// ---------------------------------------------------------------------------
onMounted(async () => {
loading.value = true
try {
await Promise.all([
loadSites(),
loadMachines(),
])
} finally {
loading.value = false
}
})
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
return {
// State
newMachine,
sites,
machines,
submitting,
loading,
// Actions
finalizeMachineCreation,
}
}