From 83a2c0453711eb01be480281ba0dd3a07eb46ec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 29 Oct 2025 12:26:37 +0100 Subject: [PATCH] Refactor violation handling in `PersonEdit.vue` by introducing `useViolationList` composable. - Centralized violation handling logic with `useViolationList` for improved reusability and maintainability. - Replaced local violation functions with composable methods in `PersonEdit.vue`. - Streamlined UI binding for validation errors across multiple inputs. --- .../vuejs/_composables/violationList.ts | 57 +++++++++++++ .../vuejs/_components/OnTheFly/PersonEdit.vue | 83 +++++-------------- 2 files changed, 80 insertions(+), 60 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Resources/public/vuejs/_composables/violationList.ts diff --git a/src/Bundle/ChillMainBundle/Resources/public/vuejs/_composables/violationList.ts b/src/Bundle/ChillMainBundle/Resources/public/vuejs/_composables/violationList.ts new file mode 100644 index 000000000..328972e1b --- /dev/null +++ b/src/Bundle/ChillMainBundle/Resources/public/vuejs/_composables/violationList.ts @@ -0,0 +1,57 @@ +import {ref} from "vue"; +import {ValidationExceptionInterface} from "ChillMainAssets/types"; + +export function useViolationList>>() { + type ViolationKey = Extract; + const violationsList = ref|null>(null); + + function violationTitles

(property: P): string[] { + if (null === violationsList.value) { + return []; + } + const r = violationsList.value.violationsByNormalizedProperty(property).map((v) => v.title); + + + return r; + + } + function violationTitlesWithParameter< + P extends ViolationKey, + Param extends Extract + >( + property: P, + with_parameter: Param, + with_parameter_value: T[P][Param], + ): string[] { + if (violationsList.value === null) { + return []; + } + return violationsList.value.violationsByNormalizedPropertyAndParams(property, with_parameter, with_parameter_value) + .map((v) => v.title); + } + + + function hasViolation

(property: P): boolean { + return violationTitles(property).length > 0; + } + function hasViolationWithParameter< + P extends ViolationKey, + Param extends Extract + >( + property: P, + with_parameter: Param, + with_parameter_value: T[P][Param], + ): boolean { + return violationTitlesWithParameter(property, with_parameter, with_parameter_value).length > 0; + } + + function setValidationException>(validationException: V): void { + violationsList.value = validationException; + } + + function cleanException(): void { + violationsList.value = null; + } + + return {violationTitles, violationTitlesWithParameter, setValidationException, cleanException, hasViolationWithParameter, hasViolation}; +} diff --git a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/OnTheFly/PersonEdit.vue b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/OnTheFly/PersonEdit.vue index 690a62450..5dc9ae02d 100644 --- a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/OnTheFly/PersonEdit.vue +++ b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/OnTheFly/PersonEdit.vue @@ -5,7 +5,7 @@

{{ err }} @@ -40,7 +40,7 @@
{{ err }} @@ -94,7 +94,7 @@
{{ err }} @@ -118,7 +118,7 @@
@@ -161,7 +161,7 @@
{{ err }} @@ -174,7 +174,7 @@
{{ trans(BIRTHDATE) }}
{{ err }} @@ -230,7 +230,7 @@
{{ trans(PERSON_MESSAGES_PERSON_PHONENUMBER) }}
{{ err }} @@ -255,7 +255,7 @@
{{ trans(PERSON_MESSAGES_PERSON_MOBILENUMBER) }}
{{ err }} @@ -280,7 +280,7 @@
{{ trans(PERSON_MESSAGES_PERSON_EMAIL) }}
{{ err }} @@ -381,6 +381,7 @@ import { } from "ChillMainAssets/lib/api/apiMethods"; import {useToast} from "vue-toast-notification"; import {getTimezoneOffsetString, ISOToDate} from "ChillMainAssets/chill/js/date"; +import {useViolationList} from "ChillMainAssets/vuejs/_composables/violationList"; interface PersonEditComponentConfig { id?: number | null; @@ -608,45 +609,7 @@ function addQueryItem(field: "lastName" | "firstName", queryItem: string) { } } -type WritePersonViolationKey = Extract; -const violationsList = ref|null>(null); - -function violationTitles

(property: P): string[] { - if (null === violationsList.value) { - return []; - } - return violationsList.value.violationsByNormalizedProperty(property).map((v) => v.title); - -} -function violationTitlesWithParameter< - P extends WritePersonViolationKey, - Param extends Extract ->( - property: P, - with_parameter: Param, - with_parameter_value: WritePersonViolationMap[P][Param], -): string[] { - if (violationsList.value === null) { - return []; - } - return violationsList.value.violationsByNormalizedPropertyAndParams(property, with_parameter, with_parameter_value) - .map((v) => v.title); -} - - -function hasViolation

(property: P): boolean { - return violationTitles(property).length > 0; -} -function hasViolationWithParameter< - P extends WritePersonViolationKey, - Param extends Extract ->( - property: P, - with_parameter: Param, - with_parameter_value: WritePersonViolationMap[P][Param], -): boolean { - return violationTitlesWithParameter(property, with_parameter, with_parameter_value).length > 0; -} +const violations = useViolationList(); function submitNewAddress(payload: { addressId: number }) { // person.addressId = payload.addressId; @@ -659,7 +622,7 @@ async function postPerson(): Promise { emit("onPersonCreated", { person: createdPerson }); } catch (e: unknown) { if (isValidationException(e)) { - violationsList.value = e; + violations.setValidationException(e); } else { toast.error(trans(PERSON_EDIT_ERROR_WHILE_SAVING)); }