[ERP-49] Créer la page Gestion des catégories (datatable + drawer) (#22)
Auto Tag Develop / tag (push) Successful in 7s
Auto Tag Develop / tag (push) Successful in 7s
## Contexte Ticket Lesstime : [#49](https://lesstime.malio.fr/tasks/460) — premier ticket front du M0 (Gestion des catégories). Suit la chaîne back ERP-43..48 mergée sur develop. ## Contenu first draft (Claude Code) - Page Nuxt `/admin/categories` (`MalioDataTable` + bouton `+ Ajouter`) - Composant `<CategoryDrawer>` : modes création / consultation / édition, transition auto view → edit à la première modification, validation client miroir RG-1.02 (name requis) / RG-1.04 (longueur 2-120) / RG-1.05 (type requis), mapping erreurs 409 (doublon) et 422 (violations) - Composant `<CategoryDeleteModal>` : confirmation suppression (soft delete RG-1.12) - Types TS `Category`, `CategoryType`, `User` - i18n `admin.categories.*` ajouté dans `fr.json` - Fix latent en passant : ajout de `'categories'` à `AdminLinkSlug` du Page Object e2e (oublié lors d'ERP-47 quand l'item sidebar a été ajouté) ## Décisions marquantes - Logique `fetch` inline dans `categories.vue` (sera extraite en composables `useCategoriesAdmin` + `useCategoryForm` au ticket ERP-50 / 0.8) - Drawer dans composant séparé pour réutilisabilité - Aucun état de tableau persisté dans l'URL (règle ABSOLUE n°6) - Tous les composants formulaires sont `Malio*` (`MalioDataTable`, `MalioInputText`, `MalioSelect`, `MalioButton`, `MalioDrawer`) ## Polish à venir (Tristan) Tristan testera en navigateur et peaufinera : UX, classes Tailwind, animations, icônes, wording de toasts. Les commits de polish suivront sur la même branche. ## Tests - `npx nuxi typecheck` : net 0 nouvelle erreur (mêmes erreurs pré-existantes que sur `develop`, infrastructure auto-import) + 1 latente corrigée (AdminLinkSlug) - `make nuxt-test` : 43/43 passent (0 régression) - Tests manuels navigateur : voir cahier de test du ticket Lesstime #49 ## Note pre-commit hook Le hook a remonté un échec PHPUnit pré-existant sur `develop` (`CategoryDeleteTest::testPatchOnSoftDeletedReturns404` → 401 au lieu de 404, JWT non initialisé en test runner). Aucun PHP touché dans cette MR. Commit avec `--no-verify` autorisé par Tristan. ## Reviewer suggéré Matthieu (back ↔ front + permissions). --------- Co-authored-by: Matthieu <mtholot19@gmail.com> Reviewed-on: #22 Co-authored-by: tristan <tristan@yuno.malio.fr> Co-committed-by: tristan <tristan@yuno.malio.fr>
This commit was merged in pull request #22.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
<template>
|
||||
<MalioModal
|
||||
:model-value="modelValue"
|
||||
modal-class="max-w-md"
|
||||
@update:model-value="emit('update:modelValue', $event)"
|
||||
>
|
||||
<template #header>
|
||||
<h3 class="text-lg font-semibold text-neutral-900">
|
||||
{{ t('admin.categories.delete.title') }}
|
||||
</h3>
|
||||
</template>
|
||||
|
||||
<p class="text-sm text-neutral-600">
|
||||
{{ t('admin.categories.delete.message', { name: categoryName }) }}
|
||||
</p>
|
||||
|
||||
<template #footer>
|
||||
<MalioButton
|
||||
:label="t('common.cancel')"
|
||||
variant="secondary"
|
||||
@click="emit('update:modelValue', false)"
|
||||
/>
|
||||
<MalioButton
|
||||
:label="t('common.delete')"
|
||||
variant="danger"
|
||||
icon-name="mdi:delete-outline"
|
||||
icon-position="left"
|
||||
:disabled="loading"
|
||||
@click="emit('confirm')"
|
||||
/>
|
||||
</template>
|
||||
</MalioModal>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
const { t } = useI18n()
|
||||
|
||||
defineProps<{
|
||||
modelValue: boolean
|
||||
categoryName: string
|
||||
loading: boolean
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
'update:modelValue': [value: boolean]
|
||||
confirm: []
|
||||
}>()
|
||||
</script>
|
||||
@@ -0,0 +1,370 @@
|
||||
<template>
|
||||
<MalioDrawer
|
||||
:model-value="modelValue"
|
||||
drawer-class="w-full max-w-lg"
|
||||
header-class="border-b border-black"
|
||||
footer-class="justify-between border-t border-black p-6"
|
||||
@update:model-value="emit('update:modelValue', $event)"
|
||||
>
|
||||
<template #header>
|
||||
<h2 class="text-2xl font-bold">
|
||||
{{ headerLabel }}
|
||||
</h2>
|
||||
</template>
|
||||
|
||||
<form class="flex flex-col gap-4 py-4" @submit.prevent="handleSave">
|
||||
<!-- Nom (RG-1.02 obligatoire / RG-1.04 longueur 2-120 apres trim).
|
||||
Erreur miroir client + erreurs server-side (422) mappees sur ce champ. -->
|
||||
<MalioInputText
|
||||
v-model="form.name"
|
||||
:label="t('admin.categories.form.name')"
|
||||
input-class="w-full"
|
||||
:max-length="120"
|
||||
:error="errors.name"
|
||||
required
|
||||
/>
|
||||
|
||||
<!-- Type (RG-1.05 obligatoire). MalioSelect porte la valeur en
|
||||
number (categoryType id) ; conversion en IRI au moment du save. -->
|
||||
<MalioSelect
|
||||
v-model="form.categoryTypeId"
|
||||
:options="categoryTypeOptions"
|
||||
:label="t('admin.categories.form.type')"
|
||||
:empty-option-label="t('admin.categories.form.typePlaceholder')"
|
||||
:error="errors.categoryType"
|
||||
:disabled="loadingTypes"
|
||||
/>
|
||||
|
||||
<!-- Erreur transverse (typiquement reseau / 5xx) — separe des
|
||||
erreurs de validation par champ. -->
|
||||
<p v-if="errors._global" class="text-sm text-red-600">
|
||||
{{ errors._global }}
|
||||
</p>
|
||||
</form>
|
||||
|
||||
<!-- Footer fixe : depuis 1.7.1 le slot #footer est un frere du body
|
||||
scrollable (shrink-0), donc reellement fige sans sticky. -->
|
||||
<template #footer>
|
||||
<MalioButton
|
||||
v-if="canShowDelete"
|
||||
:label="t('common.delete')"
|
||||
variant="danger"
|
||||
icon-name="mdi:delete-outline"
|
||||
icon-position="left"
|
||||
button-class="w-[150px]"
|
||||
@click="emit('delete')"
|
||||
/>
|
||||
<MalioButton
|
||||
v-else
|
||||
:label="t('common.cancel')"
|
||||
variant="tertiary"
|
||||
button-class="w-[150px]"
|
||||
@click="emit('update:modelValue', false)"
|
||||
/>
|
||||
<MalioButton
|
||||
v-if="canShowSave"
|
||||
:label="t('common.save')"
|
||||
variant="primary"
|
||||
button-class="w-[150px]"
|
||||
:disabled="saving || loadingTypes"
|
||||
@click="handleSave"
|
||||
/>
|
||||
</template>
|
||||
</MalioDrawer>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import type { Category, CategoryType } from '~/modules/catalog/types/category'
|
||||
import type { HydraCollection } from '~/shared/utils/api'
|
||||
|
||||
const { t } = useI18n()
|
||||
const api = useApi()
|
||||
const { can } = usePermissions()
|
||||
|
||||
const props = defineProps<{
|
||||
modelValue: boolean
|
||||
category: Category | null
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
'update:modelValue': [value: boolean]
|
||||
saved: []
|
||||
delete: []
|
||||
}>()
|
||||
|
||||
/**
|
||||
* Mode du drawer :
|
||||
* - 'create' : pas de category prop, formulaire vide, POST au save.
|
||||
* - 'view' : category prop set, formulaire pre-rempli, save MASQUE
|
||||
* jusqu'a ce que l'utilisateur modifie un champ.
|
||||
* - 'edit' : category prop set et formulaire « dirty » (au moins un
|
||||
* champ different de l'original), PATCH au save.
|
||||
*
|
||||
* La bascule view → edit est automatique des qu'un champ change (cf. watch
|
||||
* sur form). Le label du header suit le mode courant.
|
||||
*/
|
||||
type DrawerMode = 'create' | 'view' | 'edit'
|
||||
|
||||
const saving = ref(false)
|
||||
const loadingTypes = ref(false)
|
||||
const categoryTypes = ref<CategoryType[]>([])
|
||||
|
||||
const form = ref({
|
||||
name: '',
|
||||
categoryTypeId: null as number | null,
|
||||
})
|
||||
|
||||
// Snapshot des valeurs initiales pour detecter le « dirty » (view → edit).
|
||||
const initial = ref({
|
||||
name: '',
|
||||
categoryTypeId: null as number | null,
|
||||
})
|
||||
|
||||
// Erreurs par champ + erreur transverse globale. Pattern propre pour mapper
|
||||
// les violations 422 sur les MalioInputText / MalioSelect.
|
||||
const errors = ref<{
|
||||
name: string
|
||||
categoryType: string
|
||||
_global: string
|
||||
}>({
|
||||
name: '',
|
||||
categoryType: '',
|
||||
_global: '',
|
||||
})
|
||||
|
||||
const isCreateMode = computed(() => props.category === null)
|
||||
|
||||
const isDirty = computed(
|
||||
() =>
|
||||
form.value.name !== initial.value.name
|
||||
|| form.value.categoryTypeId !== initial.value.categoryTypeId,
|
||||
)
|
||||
|
||||
const mode = computed<DrawerMode>(() => {
|
||||
if (isCreateMode.value) return 'create'
|
||||
return isDirty.value ? 'edit' : 'view'
|
||||
})
|
||||
|
||||
const headerLabel = computed(() => {
|
||||
if (mode.value === 'create') return t('admin.categories.createCategory')
|
||||
if (mode.value === 'edit') return t('admin.categories.editCategory')
|
||||
return t('admin.categories.viewCategory')
|
||||
})
|
||||
|
||||
// Le bouton Supprimer n'est visible qu'en consultation/edition d'une categorie
|
||||
// existante et seulement pour les users ayant la permission manage. En mode
|
||||
// creation on affiche un bouton Annuler a la place.
|
||||
const canShowDelete = computed(
|
||||
() => !isCreateMode.value && can('catalog.categories.manage'),
|
||||
)
|
||||
|
||||
// Save : visible en creation, ou en edition (apres modification d'un champ).
|
||||
// Masque en view tant que rien n'a change.
|
||||
const canShowSave = computed(
|
||||
() => mode.value === 'create' || mode.value === 'edit',
|
||||
)
|
||||
|
||||
const categoryTypeOptions = computed(() =>
|
||||
categoryTypes.value.map(ct => ({
|
||||
label: ct.label,
|
||||
value: ct.id,
|
||||
})),
|
||||
)
|
||||
|
||||
/**
|
||||
* Charge le referentiel CategoryType. Appele a chaque ouverture du drawer
|
||||
* (pas seulement au mount) pour rester a jour si un type est ajoute en
|
||||
* arriere-plan. Volontairement sans toast en cas d'echec : on affiche un
|
||||
* message inline via `errors._global` pour ne pas spammer.
|
||||
*/
|
||||
async function loadCategoryTypes(): Promise<void> {
|
||||
loadingTypes.value = true
|
||||
try {
|
||||
const data = await api.get<HydraCollection<CategoryType>>(
|
||||
'/category_types',
|
||||
{ itemsPerPage: 999 },
|
||||
{ toast: false },
|
||||
)
|
||||
categoryTypes.value = data.member ?? []
|
||||
} catch {
|
||||
categoryTypes.value = []
|
||||
errors.value._global = t('admin.categories.toast.typesLoadFailed')
|
||||
} finally {
|
||||
loadingTypes.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-initialise le formulaire a partir de la prop `category`. Aussi appele
|
||||
* a l'ouverture du drawer pour repartir d'un etat propre.
|
||||
*/
|
||||
function resetForm(): void {
|
||||
errors.value = { name: '', categoryType: '', _global: '' }
|
||||
if (props.category) {
|
||||
form.value.name = props.category.name
|
||||
form.value.categoryTypeId = props.category.categoryType.id
|
||||
initial.value.name = props.category.name
|
||||
initial.value.categoryTypeId = props.category.categoryType.id
|
||||
} else {
|
||||
form.value.name = ''
|
||||
form.value.categoryTypeId = null
|
||||
initial.value.name = ''
|
||||
initial.value.categoryTypeId = null
|
||||
}
|
||||
}
|
||||
|
||||
// Re-initialiser quand la categorie selectionnee change (clic sur une autre
|
||||
// ligne sans fermer le drawer entre-temps).
|
||||
watch(() => props.category, resetForm, { immediate: true })
|
||||
|
||||
// A chaque ouverture du drawer : reset + chargement frais des types. Pas
|
||||
// d'optimisation cache au M0 — le referentiel est petit et statique.
|
||||
watch(
|
||||
() => props.modelValue,
|
||||
(open) => {
|
||||
if (open) {
|
||||
resetForm()
|
||||
loadCategoryTypes()
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
/**
|
||||
* Validation client-side miroir des RG back. Renvoie true si tout passe et
|
||||
* peuple `errors` sinon. Le serveur valide aussi (defense en profondeur) ;
|
||||
* la validation client sert juste a eviter l'aller-retour evitable.
|
||||
*/
|
||||
function validate(): boolean {
|
||||
errors.value = { name: '', categoryType: '', _global: '' }
|
||||
const trimmedName = form.value.name.trim()
|
||||
|
||||
// RG-1.02 — name obligatoire (vide / whitespace-only).
|
||||
if (trimmedName === '') {
|
||||
errors.value.name = t('admin.categories.validation.nameRequired')
|
||||
} else if (trimmedName.length < 2 || trimmedName.length > 120) {
|
||||
// RG-1.04 — longueur 2-120 apres trim.
|
||||
errors.value.name = t('admin.categories.validation.nameLength')
|
||||
}
|
||||
|
||||
// RG-1.05 — categoryType obligatoire.
|
||||
if (form.value.categoryTypeId === null) {
|
||||
errors.value.categoryType = t('admin.categories.validation.typeRequired')
|
||||
}
|
||||
|
||||
return errors.value.name === '' && errors.value.categoryType === ''
|
||||
}
|
||||
|
||||
/**
|
||||
* Mappe une reponse 422 d'API Platform sur le state `errors`. API Platform 4
|
||||
* retourne soit `violations: [{ propertyPath, message }]` soit
|
||||
* `hydra:violations` selon la negociation de format.
|
||||
*/
|
||||
function mapServerViolations(data: unknown): boolean {
|
||||
if (!data || typeof data !== 'object') return false
|
||||
const record = data as Record<string, unknown>
|
||||
const rawViolations = record.violations ?? record['hydra:violations']
|
||||
if (!Array.isArray(rawViolations)) return false
|
||||
|
||||
let mapped = false
|
||||
for (const v of rawViolations) {
|
||||
if (!v || typeof v !== 'object') continue
|
||||
const violation = v as Record<string, unknown>
|
||||
const path = String(violation.propertyPath ?? '')
|
||||
const message = String(violation.message ?? '')
|
||||
if (path === 'name') {
|
||||
errors.value.name = message
|
||||
mapped = true
|
||||
} else if (path === 'categoryType') {
|
||||
errors.value.categoryType = message
|
||||
mapped = true
|
||||
}
|
||||
}
|
||||
return mapped
|
||||
}
|
||||
|
||||
/**
|
||||
* Extrait un message d'erreur HTTP au format API Platform / Hydra.
|
||||
*/
|
||||
function extractErrorMessage(data: unknown): string {
|
||||
if (!data || typeof data !== 'object') return ''
|
||||
const record = data as Record<string, unknown>
|
||||
return (
|
||||
(record['hydra:description'] as string)
|
||||
?? (record.detail as string)
|
||||
?? (record.description as string)
|
||||
?? ''
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sauvegarde la categorie (POST en mode create, PATCH en mode edit).
|
||||
* Trim cote client (miroir RG-1.03), conversion ID → IRI pour categoryType,
|
||||
* mapping des erreurs server.
|
||||
*/
|
||||
async function handleSave(): Promise<void> {
|
||||
if (!validate()) return
|
||||
saving.value = true
|
||||
errors.value._global = ''
|
||||
|
||||
// Trim cote client (miroir RG-1.03). Le serveur retrim de toute facon.
|
||||
const payload = {
|
||||
name: form.value.name.trim(),
|
||||
categoryType: `/api/category_types/${form.value.categoryTypeId}`,
|
||||
}
|
||||
|
||||
try {
|
||||
if (mode.value === 'create') {
|
||||
await api.post('/categories', payload, {
|
||||
toastSuccessMessage: t('admin.categories.toast.created'),
|
||||
toast: false, // gestion fine des erreurs ci-dessous
|
||||
})
|
||||
} else if (mode.value === 'edit' && props.category) {
|
||||
await api.patch(`/categories/${props.category.id}`, payload, {
|
||||
toastSuccessMessage: t('admin.categories.toast.updated'),
|
||||
toast: false,
|
||||
})
|
||||
}
|
||||
|
||||
// Succes : toast manuel (car on a desactive le toast du composable
|
||||
// pour gerer finement les erreurs) + propagation au parent.
|
||||
useToast().success({
|
||||
title: 'Succès',
|
||||
message:
|
||||
mode.value === 'create'
|
||||
? t('admin.categories.toast.created')
|
||||
: t('admin.categories.toast.updated'),
|
||||
})
|
||||
emit('saved')
|
||||
emit('update:modelValue', false)
|
||||
} catch (err: unknown) {
|
||||
const error = err as { response?: { status?: number, _data?: unknown } }
|
||||
const status = error?.response?.status
|
||||
const data = error?.response?._data
|
||||
|
||||
if (status === 409) {
|
||||
// RG-1.07 — doublon (name, categoryType). Toast custom + erreur
|
||||
// mappee sur le champ name (origine du conflit).
|
||||
const duplicateMessage = t('admin.categories.toast.duplicate', {
|
||||
name: payload.name,
|
||||
})
|
||||
errors.value.name = duplicateMessage
|
||||
useToast().error({
|
||||
title: 'Erreur',
|
||||
message: duplicateMessage,
|
||||
})
|
||||
} else if (status === 422 && mapServerViolations(data)) {
|
||||
// Violations mappees sur les champs concernes — pas de toast,
|
||||
// l'utilisateur voit l'erreur directement sous le champ.
|
||||
} else {
|
||||
const extracted = extractErrorMessage(data)
|
||||
errors.value._global = extracted || 'Une erreur est survenue.'
|
||||
useToast().error({
|
||||
title: 'Erreur',
|
||||
message: errors.value._global,
|
||||
})
|
||||
}
|
||||
} finally {
|
||||
saving.value = false
|
||||
}
|
||||
}
|
||||
</script>
|
||||
Reference in New Issue
Block a user