97fe0b39de
- libelles de toast generiques passes en i18n (errors.title/generic/unknown, success.title) dans useFormErrors, useApi et useCategoryForm - nouveau composable useClientFormErrors : factorise l'etat d'erreurs (3 useFormErrors scalaires + 3 tableaux par ligne + mapRowError) partage entre clients/new.vue et [id]/edit.vue - mapRowError retourne un booleen et ne toaste plus : chaque page garde son fallback (toast generique en creation, showError en edition)
56 lines
2.1 KiB
TypeScript
56 lines
2.1 KiB
TypeScript
/**
|
|
* Composable d'erreurs partage des ecrans client (creation + edition, M1
|
|
* Commercial). Factorise le cablage identique entre `clients/new.vue` et
|
|
* `clients/[id]/edit.vue` (suggestion de revue ERP-101) :
|
|
* - un `useFormErrors` par groupe scalaire (Principal / Information /
|
|
* Comptabilite) : violations 422 affichees inline sous chaque champ ;
|
|
* - un tableau d'erreurs PAR LIGNE pour chaque collection (contacts /
|
|
* adresses / RIB), aligne sur l'index du `v-for`.
|
|
*
|
|
* `mapRowError` ne toaste PAS lui-meme : il retourne un booleen (true = mappe
|
|
* inline). Chaque page conserve ainsi son propre fallback dans le `catch`
|
|
* (toast generique en creation, `showError` en edition) sans imposer un
|
|
* comportement commun.
|
|
*/
|
|
import { ref, type Ref } from 'vue'
|
|
import { mapViolationsToRecord } from '~/shared/utils/api'
|
|
|
|
export function useClientFormErrors() {
|
|
const mainErrors = useFormErrors()
|
|
const informationErrors = useFormErrors()
|
|
const accountingErrors = useFormErrors()
|
|
const contactErrors = ref<Record<string, string>[]>([])
|
|
const addressErrors = ref<Record<string, string>[]>([])
|
|
const ribErrors = ref<Record<string, string>[]>([])
|
|
|
|
/**
|
|
* Mappe l'erreur d'une ligne de collection sur le tableau cible (par index).
|
|
* 422 avec violations exploitables → erreurs inline sous les champs de la
|
|
* ligne + retourne true. Sinon → ne touche pas la cible et retourne false
|
|
* (le caller decide du fallback toast).
|
|
*/
|
|
function mapRowError(
|
|
error: unknown,
|
|
target: Ref<Record<string, string>[]>,
|
|
index: number,
|
|
): boolean {
|
|
const response = (error as { response?: { status?: number, _data?: unknown } })?.response
|
|
const mapped = response?.status === 422 ? mapViolationsToRecord(response._data) : {}
|
|
if (Object.keys(mapped).length > 0) {
|
|
target.value[index] = mapped
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
return {
|
|
mainErrors,
|
|
informationErrors,
|
|
accountingErrors,
|
|
contactErrors,
|
|
addressErrors,
|
|
ribErrors,
|
|
mapRowError,
|
|
}
|
|
}
|