diff --git a/app/pages/component-catalog.vue b/app/pages/component-catalog.vue index f653435..d2ce710 100644 --- a/app/pages/component-catalog.vue +++ b/app/pages/component-catalog.vue @@ -180,6 +180,30 @@ const resolvePreviewAlt = (component: Record) => { return 'Aperçu du document' } +const resolveDeleteBlockingReasons = (component: Record) => { + const reasons: string[] = [] + const machineLinks = Array.isArray(component?.machineLinks) + ? component.machineLinks.length + : component?.machineLinksCount ?? 0 + const documents = Array.isArray(component?.documents) + ? component.documents.length + : component?.documentsCount ?? 0 + const customFields = Array.isArray(component?.customFieldValues) + ? component.customFieldValues.length + : component?.customFieldValuesCount ?? 0 + + if (machineLinks > 0) { + reasons.push(`${machineLinks} liaison${machineLinks > 1 ? 's' : ''} machine`) + } + if (documents > 0) { + reasons.push(`${documents} document${documents > 1 ? 's' : ''}`) + } + if (customFields > 0) { + reasons.push(`${customFields} valeur${customFields > 1 ? 's' : ''} de champ personnalisé`) + } + return reasons +} + const resolveComparableName = (component: Record) => { const toComparable = (value?: string | null) => (value ?? '').toString().trim().toLowerCase() @@ -235,13 +259,14 @@ const visibleComposants = computed(() => { }) const handleDeleteComponent = async (component: Record) => { - const hasLinkedElements = - (component?.machineLinks?.length ?? 0) > 0 || - (component?.documents?.length ?? 0) > 0 || - (component?.customFieldValues?.length ?? 0) > 0 + const blockingReasons = resolveDeleteBlockingReasons(component) - if (hasLinkedElements) { - showError('Impossible de supprimer ce composant car il possède des éléments liés.') + if (blockingReasons.length) { + showError( + `Impossible de supprimer ce composant car il possède encore: ${blockingReasons.join( + ', ', + )}. Supprimez ou détachez ces éléments avant de réessayer.` + ) return } diff --git a/app/pages/pieces-catalog.vue b/app/pages/pieces-catalog.vue index 6530c14..202cc55 100644 --- a/app/pages/pieces-catalog.vue +++ b/app/pages/pieces-catalog.vue @@ -182,6 +182,30 @@ const resolvePreviewAlt = (piece: Record) => { return 'Aperçu du document' } +const resolveDeleteBlockingReasons = (piece: Record) => { + const reasons: string[] = [] + const machineLinks = Array.isArray(piece?.machineLinks) + ? piece.machineLinks.length + : piece?.machineLinksCount ?? 0 + const documents = Array.isArray(piece?.documents) + ? piece.documents.length + : piece?.documentsCount ?? 0 + const customFields = Array.isArray(piece?.customFieldValues) + ? piece.customFieldValues.length + : piece?.customFieldValuesCount ?? 0 + + if (machineLinks > 0) { + reasons.push(`${machineLinks} liaison${machineLinks > 1 ? 's' : ''} machine`) + } + if (documents > 0) { + reasons.push(`${documents} document${documents > 1 ? 's' : ''}`) + } + if (customFields > 0) { + reasons.push(`${customFields} valeur${customFields > 1 ? 's' : ''} de champ personnalisé`) + } + return reasons +} + const resolveComparableName = (piece: Record) => { const normalise = (value?: string | null) => (value ?? '').toString().trim().toLowerCase() @@ -237,13 +261,14 @@ const visiblePieces = computed(() => { }) const handleDeletePiece = async (piece: Record) => { - const hasLinkedElements = - (piece?.machineLinks?.length ?? 0) > 0 || - (piece?.documents?.length ?? 0) > 0 || - (piece?.customFieldValues?.length ?? 0) > 0 + const blockingReasons = resolveDeleteBlockingReasons(piece) - if (hasLinkedElements) { - showError('Impossible de supprimer cette pièce car elle possède des éléments liés.') + if (blockingReasons.length) { + showError( + `Impossible de supprimer cette pièce car elle possède encore: ${blockingReasons.join( + ', ', + )}. Supprimez ou détachez ces éléments avant de réessayer.` + ) return }