From 9918501f2914a2814f53440a64903b662fedbfb6 Mon Sep 17 00:00:00 2001 From: Khafid Prayoga Date: Wed, 10 Dec 2025 09:49:30 +0700 Subject: [PATCH] refactor(patient): extract age calculation logic to shared utility Move age calculation logic from multiple components to a shared utility function in person model Add age display to patient entry form and update preview component to use shared utility Remove redundant age field from select-dob component --- app/components/app/patient/entry-form.vue | 26 ++++++- .../app/patient/fields/select-dob.vue | 76 +------------------ app/components/app/patient/preview.vue | 12 +-- app/models/person.ts | 43 +++++++++++ 4 files changed, 75 insertions(+), 82 deletions(-) diff --git a/app/components/app/patient/entry-form.vue b/app/components/app/patient/entry-form.vue index 4f577091..7b6c578f 100644 --- a/app/components/app/patient/entry-form.vue +++ b/app/components/app/patient/entry-form.vue @@ -5,6 +5,9 @@ import { toTypedSchema } from '@vee-validate/zod' // types import { type PatientFormData, PatientSchema } from '~/schemas/patient.schema' +// utils +import { calculateAge } from '~/models/person' + // components import * as DE from '~/components/pub/my-ui/doc-entry' import { InputBase, FileField as FileUpload } from '~/components/pub/my-ui/form' @@ -26,7 +29,9 @@ import { SelectReligion, } from './fields' -interface FormData extends PatientFormData {} +interface FormData extends PatientFormData { + _calculatedAge: string +} // Type untuk initial values (sebelum transform schema) interface PatientFormInput { @@ -74,6 +79,18 @@ defineExpose({ setValues, values, }) + +watch( + () => values.birthDate, + (newValue) => { + if (newValue) { + setFieldValue('_calculatedAge', calculateAge(newValue)) + } + }, + { + immediate: true, + }, +)