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>
124 lines
3.5 KiB
TypeScript
124 lines
3.5 KiB
TypeScript
/**
|
||
* 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,
|
||
}
|
||
}
|