feat: reuse inventory items when configuring machines
This commit is contained in:
@@ -4,406 +4,76 @@
|
||||
<div>
|
||||
<h1 class="text-3xl font-semibold text-base-content">Catalogue des composants</h1>
|
||||
<p class="text-sm text-gray-500">
|
||||
Consultez les catégories disponibles et instanciez des composants à partir de leur squelette canonique.
|
||||
Consultez et gérez tous les composants existants.
|
||||
</p>
|
||||
</div>
|
||||
<NuxtLink to="/component-category" class="btn btn-outline btn-sm md:btn-md self-start">
|
||||
Gérer les catégories
|
||||
</NuxtLink>
|
||||
<div class="flex flex-wrap gap-2">
|
||||
<NuxtLink to="/component/create" class="btn btn-primary btn-sm md:btn-md">
|
||||
Ajouter un composant
|
||||
</NuxtLink>
|
||||
<NuxtLink to="/component-category" class="btn btn-outline btn-sm md:btn-md">
|
||||
Gérer les catégories
|
||||
</NuxtLink>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<section>
|
||||
<div v-if="loadingTypes" class="flex justify-center py-16">
|
||||
<span class="loading loading-spinner loading-lg" aria-hidden="true" />
|
||||
</div>
|
||||
<section class="card border border-base-200 bg-base-100 shadow-sm">
|
||||
<div class="card-body space-y-4">
|
||||
<header class="flex flex-col gap-2">
|
||||
<h2 class="text-xl font-semibold text-base-content">Composants créés</h2>
|
||||
<p class="text-sm text-base-content/70">
|
||||
Retrouvez ici tous les composants enregistrés, indépendamment de leur catégorie.
|
||||
</p>
|
||||
</header>
|
||||
|
||||
<template v-else>
|
||||
<p v-if="!componentTypeList.length" class="text-sm text-gray-500">
|
||||
Aucune catégorie de composant n'est disponible pour le moment. Créez des catégories dans la gestion dédiée pour définir
|
||||
vos squelettes.
|
||||
<div v-if="loadingComposants" class="flex justify-center py-8">
|
||||
<span class="loading loading-spinner" aria-hidden="true" />
|
||||
</div>
|
||||
|
||||
<p v-else-if="!composantsList.length" class="text-sm text-base-content/70">
|
||||
Aucun composant n'a encore été créé.
|
||||
</p>
|
||||
|
||||
<div v-else class="grid gap-6 lg:grid-cols-2">
|
||||
<article
|
||||
v-for="type in componentTypeList"
|
||||
:key="type.id"
|
||||
class="card bg-base-100 border border-base-200 shadow-sm"
|
||||
>
|
||||
<div class="card-body space-y-4">
|
||||
<div class="flex flex-col gap-3 md:flex-row md:items-start md:justify-between">
|
||||
<div class="space-y-1">
|
||||
<h2 class="card-title text-xl">{{ type.name }}</h2>
|
||||
<p v-if="type.description" class="text-sm text-gray-500">
|
||||
{{ type.description }}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<button type="button" class="btn btn-primary btn-sm md:btn-md self-start" @click="openCreationModal(type)">
|
||||
Instancier un composant
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="flex flex-wrap gap-2 text-xs text-gray-500">
|
||||
<span class="badge badge-outline">{{ formatStructurePreview(type.structure) }}</span>
|
||||
<span v-if="getCategoryCustomFields(type).length" class="badge badge-outline">
|
||||
{{ getCategoryCustomFields(type).length }} champ(s) personnalisés
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div v-if="type.structure" class="space-y-3">
|
||||
<details class="collapse collapse-arrow bg-base-200/60">
|
||||
<summary class="collapse-title text-sm font-medium">
|
||||
Détails du squelette
|
||||
</summary>
|
||||
<div class="collapse-content space-y-3 text-sm text-base-content/80">
|
||||
<div v-if="getStructureCustomFields(type.structure).length" class="space-y-1">
|
||||
<h3 class="font-semibold text-sm text-base-content">Valeurs par défaut</h3>
|
||||
<ul class="list-disc list-inside space-y-1">
|
||||
<li
|
||||
v-for="field in getStructureCustomFields(type.structure)"
|
||||
:key="field.key || field.name"
|
||||
>
|
||||
<span class="font-medium">{{ field.key || field.name }}</span>
|
||||
<span v-if="field.value !== undefined && field.value !== null"> : {{ field.value }}</span>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div v-if="getStructurePieces(type.structure).length" class="space-y-1">
|
||||
<h3 class="font-semibold text-sm text-base-content">Pièces imposées</h3>
|
||||
<ul class="list-disc list-inside space-y-1">
|
||||
<li
|
||||
v-for="(piece, index) in getStructurePieces(type.structure)"
|
||||
:key="piece.role || piece.typePieceId || piece.familyCode || index"
|
||||
>
|
||||
{{ resolvePieceLabel(piece) }}
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div v-if="getStructureSubcomponents(type.structure).length" class="space-y-1">
|
||||
<h3 class="font-semibold text-sm text-base-content">Sous-composants</h3>
|
||||
<ul class="list-disc list-inside space-y-1">
|
||||
<li
|
||||
v-for="(subcomponent, index) in getStructureSubcomponents(type.structure)"
|
||||
:key="subcomponent.alias || subcomponent.typeComposantId || subcomponent.familyCode || index"
|
||||
>
|
||||
{{ resolveSubcomponentLabel(subcomponent) }}
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p
|
||||
v-if="!getStructureCustomFields(type.structure).length && !getStructurePieces(type.structure).length && !getStructureSubcomponents(type.structure).length"
|
||||
class="text-xs text-gray-500"
|
||||
>
|
||||
Ce squelette ne définit pas encore de pièces, sous-composants ou valeurs par défaut.
|
||||
</p>
|
||||
</div>
|
||||
</details>
|
||||
</div>
|
||||
|
||||
<div v-if="getCategoryCustomFields(type).length" class="space-y-2">
|
||||
<h3 class="text-sm font-semibold text-base-content">Champs personnalisés de la catégorie</h3>
|
||||
<ul class="space-y-1 text-sm text-base-content/80">
|
||||
<li
|
||||
v-for="field in getCategoryCustomFields(type)"
|
||||
:key="field.id || field.name"
|
||||
class="flex flex-wrap gap-2 items-center"
|
||||
<div v-else class="overflow-x-auto">
|
||||
<table class="table table-sm md:table-md">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Nom</th>
|
||||
<th>Catégorie</th>
|
||||
<th>Référence</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="component in composantsList" :key="component.id">
|
||||
<td>{{ component.name || 'Composant sans nom' }}</td>
|
||||
<td>{{ component.typeComposant?.name || '—' }}</td>
|
||||
<td>{{ component.reference || '—' }}</td>
|
||||
<td>
|
||||
<NuxtLink
|
||||
:to="`/component/${component.id}/edit`"
|
||||
class="btn btn-ghost btn-xs"
|
||||
>
|
||||
<span class="font-medium">{{ field.name }}</span>
|
||||
<span class="badge badge-outline badge-xs">{{ field.type || 'text' }}</span>
|
||||
<span v-if="field.required" class="badge badge-outline badge-xs badge-error">Obligatoire</span>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div v-if="getExistingComponents(type).length" class="space-y-2">
|
||||
<h3 class="text-sm font-semibold text-base-content">Composants existants</h3>
|
||||
<ul class="space-y-1 text-sm text-base-content/80">
|
||||
<li
|
||||
v-for="component in getExistingComponents(type).slice(0, 5)"
|
||||
:key="component.id"
|
||||
>
|
||||
<span class="font-medium">{{ component.name || 'Composant sans nom' }}</span>
|
||||
<span v-if="component.machine?.name" class="text-xs text-gray-500">
|
||||
· Machine : {{ component.machine.name }}
|
||||
</span>
|
||||
</li>
|
||||
</ul>
|
||||
<p
|
||||
v-if="getExistingComponents(type).length > 5"
|
||||
class="text-xs text-gray-500"
|
||||
>
|
||||
+ {{ getExistingComponents(type).length - 5 }} composant(s) supplémentaires.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</article>
|
||||
Modifier
|
||||
</NuxtLink>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<Teleport to="body">
|
||||
<div v-if="creationModalOpen" class="modal modal-open">
|
||||
<div class="modal-box max-w-3xl space-y-6">
|
||||
<div class="space-y-1">
|
||||
<h3 class="text-lg font-semibold">
|
||||
Instancier un composant
|
||||
</h3>
|
||||
<p class="text-sm text-gray-500">
|
||||
Renseignez les informations du composant instancié à partir du squelette de la catégorie sélectionnée.
|
||||
</p>
|
||||
<p v-if="selectedType" class="badge badge-outline badge-sm">
|
||||
Catégorie : {{ selectedType.name }}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<form class="space-y-4" @submit.prevent="submitCreation">
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<div class="form-control">
|
||||
<label class="label">
|
||||
<span class="label-text">Nom du composant</span>
|
||||
</label>
|
||||
<input
|
||||
v-model="creationForm.name"
|
||||
type="text"
|
||||
class="input input-bordered input-sm md:input-md"
|
||||
:disabled="submitting"
|
||||
placeholder="Nom affiché après instanciation"
|
||||
>
|
||||
</div>
|
||||
|
||||
<div class="form-control">
|
||||
<label class="label">
|
||||
<span class="label-text">Référence</span>
|
||||
</label>
|
||||
<input
|
||||
v-model="creationForm.reference"
|
||||
type="text"
|
||||
class="input input-bordered input-sm md:input-md"
|
||||
:disabled="submitting"
|
||||
placeholder="Référence interne ou constructeur"
|
||||
>
|
||||
</div>
|
||||
|
||||
<div class="form-control">
|
||||
<label class="label">
|
||||
<span class="label-text">Constructeur</span>
|
||||
</label>
|
||||
<ConstructeurSelect
|
||||
v-model="creationForm.constructeurId"
|
||||
class="w-full"
|
||||
:disabled="submitting"
|
||||
placeholder="Rechercher un constructeur..."
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-control">
|
||||
<label class="label">
|
||||
<span class="label-text">Prix</span>
|
||||
</label>
|
||||
<input
|
||||
v-model="creationForm.prix"
|
||||
type="number"
|
||||
step="0.01"
|
||||
min="0"
|
||||
class="input input-bordered input-sm md:input-md"
|
||||
:disabled="submitting"
|
||||
placeholder="Valeur indicative (€)"
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div class="modal-action">
|
||||
<button type="button" class="btn btn-ghost" :disabled="submitting" @click="closeCreationModal">
|
||||
Annuler
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-primary"
|
||||
:disabled="!canSubmit"
|
||||
@click="submitCreation"
|
||||
>
|
||||
<span v-if="submitting" class="loading loading-spinner loading-sm mr-2"></span>
|
||||
Créer le composant
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<button class="modal-backdrop" aria-label="Fermer" @click="closeCreationModal"></button>
|
||||
</div>
|
||||
</Teleport>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, reactive, ref } from 'vue'
|
||||
import ConstructeurSelect from '~/components/ConstructeurSelect.vue'
|
||||
import { useComponentTypes } from '~/composables/useComponentTypes'
|
||||
import { computed, onMounted } from 'vue'
|
||||
import { useComposants } from '~/composables/useComposants'
|
||||
import { useToast } from '~/composables/useToast'
|
||||
import { formatStructurePreview, sanitizeDefinitionOverrides } from '~/shared/modelUtils'
|
||||
import type { ComponentModelStructure } from '~/shared/types/inventory'
|
||||
import type { ModelType } from '~/services/modelTypes'
|
||||
|
||||
interface ComponentCatalogType extends ModelType {
|
||||
structure: ComponentModelStructure | null
|
||||
customFields?: Array<Record<string, any>>
|
||||
components?: Array<Record<string, any>>
|
||||
}
|
||||
|
||||
const { componentTypes, loadComponentTypes, loadingComponentTypes } = useComponentTypes()
|
||||
const { createComposant } = useComposants()
|
||||
const toast = useToast()
|
||||
|
||||
const creationModalOpen = ref(false)
|
||||
const selectedType = ref<ComponentCatalogType | null>(null)
|
||||
const submitting = ref(false)
|
||||
const creationForm = reactive({
|
||||
name: '' as string,
|
||||
reference: '' as string,
|
||||
constructeurId: null as string | null,
|
||||
prix: '' as string,
|
||||
})
|
||||
|
||||
const loadingTypes = computed(() => loadingComponentTypes.value)
|
||||
const componentTypeList = computed<ComponentCatalogType[]>(() =>
|
||||
(componentTypes.value || [])
|
||||
.filter((item: any) => item?.category === 'COMPONENT') as ComponentCatalogType[],
|
||||
)
|
||||
|
||||
const canSubmit = computed(() => Boolean(selectedType.value && !submitting.value))
|
||||
|
||||
const getCategoryCustomFields = (type: ComponentCatalogType) => {
|
||||
return Array.isArray(type?.customFields) ? type.customFields : []
|
||||
}
|
||||
|
||||
const getStructureCustomFields = (structure: ComponentModelStructure | null) => {
|
||||
return Array.isArray(structure?.customFields) ? structure.customFields : []
|
||||
}
|
||||
|
||||
const getStructurePieces = (structure: ComponentModelStructure | null) => {
|
||||
return Array.isArray(structure?.pieces) ? structure.pieces : []
|
||||
}
|
||||
|
||||
const getStructureSubcomponents = (structure: ComponentModelStructure | null) => {
|
||||
if (Array.isArray(structure?.subcomponents)) {
|
||||
return structure.subcomponents
|
||||
}
|
||||
const legacy = (structure as any)?.subComponents
|
||||
return Array.isArray(legacy) ? legacy : []
|
||||
}
|
||||
|
||||
const getExistingComponents = (type: ComponentCatalogType) => {
|
||||
return Array.isArray(type?.components) ? type.components : []
|
||||
}
|
||||
|
||||
const resolvePieceLabel = (piece: Record<string, any>) => {
|
||||
const parts: string[] = []
|
||||
if (piece.role) {
|
||||
parts.push(piece.role)
|
||||
}
|
||||
if (piece.typePiece?.name) {
|
||||
parts.push(piece.typePiece.name)
|
||||
} else if (piece.typePieceLabel) {
|
||||
parts.push(piece.typePieceLabel)
|
||||
} else if (piece.familyCode) {
|
||||
parts.push(piece.familyCode)
|
||||
} else if (piece.typePieceId) {
|
||||
parts.push(`#${piece.typePieceId}`)
|
||||
}
|
||||
return parts.length ? parts.join(' • ') : 'Pièce'
|
||||
}
|
||||
|
||||
const resolveSubcomponentLabel = (node: Record<string, any>) => {
|
||||
const parts: string[] = []
|
||||
if (node.alias) {
|
||||
parts.push(node.alias)
|
||||
}
|
||||
if (node.typeComposant?.name) {
|
||||
parts.push(node.typeComposant.name)
|
||||
} else if (node.typeComposantLabel) {
|
||||
parts.push(node.typeComposantLabel)
|
||||
} else if (node.familyCode) {
|
||||
parts.push(node.familyCode)
|
||||
} else if (node.typeComposantId) {
|
||||
parts.push(`#${node.typeComposantId}`)
|
||||
}
|
||||
|
||||
const childCount = Array.isArray(node.subcomponents)
|
||||
? node.subcomponents.length
|
||||
: Array.isArray(node.subComponents)
|
||||
? node.subComponents.length
|
||||
: 0
|
||||
if (childCount) {
|
||||
parts.push(`${childCount} sous-composant(s)`)
|
||||
}
|
||||
return parts.length ? parts.join(' • ') : 'Sous-composant'
|
||||
}
|
||||
|
||||
const clearCreationForm = () => {
|
||||
creationForm.name = ''
|
||||
creationForm.reference = ''
|
||||
creationForm.constructeurId = null
|
||||
creationForm.prix = ''
|
||||
}
|
||||
|
||||
const resetCreationFormForType = () => {
|
||||
clearCreationForm()
|
||||
creationForm.name = selectedType.value?.name || ''
|
||||
}
|
||||
|
||||
const openCreationModal = async (type: ComponentCatalogType) => {
|
||||
selectedType.value = type
|
||||
resetCreationFormForType()
|
||||
creationModalOpen.value = true
|
||||
}
|
||||
|
||||
const closeCreationModal = () => {
|
||||
creationModalOpen.value = false
|
||||
selectedType.value = null
|
||||
clearCreationForm()
|
||||
}
|
||||
|
||||
const submitCreation = async () => {
|
||||
if (!selectedType.value) {
|
||||
toast.showError('Aucune catégorie sélectionnée.')
|
||||
return
|
||||
}
|
||||
|
||||
const payload: Record<string, any> = {
|
||||
typeComposantId: selectedType.value.id,
|
||||
}
|
||||
|
||||
const overrides = sanitizeDefinitionOverrides({
|
||||
name: creationForm.name,
|
||||
reference: creationForm.reference,
|
||||
constructeurId: creationForm.constructeurId,
|
||||
prix: creationForm.prix,
|
||||
})
|
||||
|
||||
if (overrides) {
|
||||
payload.definition = overrides
|
||||
}
|
||||
|
||||
submitting.value = true
|
||||
try {
|
||||
const result = await createComposant(payload)
|
||||
if (result.success) {
|
||||
await loadComponentTypes()
|
||||
closeCreationModal()
|
||||
} else if (result.error) {
|
||||
toast.showError(result.error)
|
||||
}
|
||||
} catch (error: any) {
|
||||
toast.showError(error?.message || "Erreur lors de la création du composant")
|
||||
} finally {
|
||||
submitting.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const { composants, loadComposants, loading: loadingComposantsRef } = useComposants()
|
||||
const loadingComposants = computed(() => loadingComposantsRef.value)
|
||||
const composantsList = computed(() => composants.value || [])
|
||||
onMounted(async () => {
|
||||
await loadComponentTypes()
|
||||
await loadComposants()
|
||||
})
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user