diff --git a/src/lib/components/composable/use-form-state.svelte.js b/src/lib/components/composable/use-form-state.svelte.js index a7511e5..b170672 100644 --- a/src/lib/components/composable/use-form-state.svelte.js +++ b/src/lib/components/composable/use-form-state.svelte.js @@ -1,7 +1,7 @@ export function useFormState(initial) { const form = $state(structuredClone(initial)) const isSaving = $state({ current: false }); - + console.log(form); // function resetForm() { // Object.assign(form, structuredClone(initial)); // } diff --git a/src/lib/components/composable/use-form.svelte.js b/src/lib/components/composable/use-form.svelte.js index e770d7b..d84231d 100644 --- a/src/lib/components/composable/use-form.svelte.js +++ b/src/lib/components/composable/use-form.svelte.js @@ -13,6 +13,8 @@ export function useForm({schema, initialForm, defaultErrors, mode, modeOpt, save try { // const payload = { ...state.form }; const payload = customPayload || { ...state.form }; + // const { ProvinceID, CityID, ...rest } = state.form; + // const payload = customPayload || rest; const result = currentMode === 'edit' ? await editEndpoint(payload) : await saveEndpoint(payload); return result; } catch (error) { diff --git a/src/lib/components/composable/use-master-detail.svelte.js b/src/lib/components/composable/use-master-detail.svelte.js index 61ee763..3ac5182 100644 --- a/src/lib/components/composable/use-master-detail.svelte.js +++ b/src/lib/components/composable/use-master-detail.svelte.js @@ -1,5 +1,6 @@ import { useResponsive } from "./use-responsive.svelte.js"; import { useForm } from "./use-form.svelte.js"; +import { tick } from "svelte"; export function useMasterDetail(options = {}) { const { onSelect = null, formConfig = null, } = options; @@ -8,6 +9,7 @@ export function useMasterDetail(options = {}) { let mode = $state("view"); let isLoadingDetail = $state(false); let formSnapshot = $state(null); + let showExitConfirm = $state(false); const formState = useForm(formConfig); @@ -28,6 +30,8 @@ export function useMasterDetail(options = {}) { JSON.stringify(formState.form) !== JSON.stringify(formSnapshot) ); + $inspect(formState.form) + async function select(item) { mode = "view"; @@ -47,7 +51,7 @@ export function useMasterDetail(options = {}) { } } - function enterCreate(initialData = null) { + async function enterCreate(initialData = null) { mode = "create"; selectedItem = null; @@ -56,6 +60,8 @@ export function useMasterDetail(options = {}) { if (initialData) { formState.setForm(initialData); } + await tick(); + formSnapshot = $state.snapshot(formState.form); } function enterEdit(param) { @@ -84,15 +90,21 @@ export function useMasterDetail(options = {}) { formSnapshot = $state.snapshot(formState.form); } - function exitForm() { - if (isDirty) { - const ok = confirm('You have unsaved changes. Discard them?'); - if (!ok) return; + function exitForm(force = false) { + if (!force && isDirty) { + showExitConfirm = true; + return; } + // Direct exit mode = "view"; selectedItem = null; + formSnapshot = null; + } + function confirmExit() { + mode = "view"; + selectedItem = null; formSnapshot = null; } @@ -142,11 +154,14 @@ export function useMasterDetail(options = {}) { get formState() { return formState; }, + get showExitConfirm() { return showExitConfirm; }, + set showExitConfirm(value) { showExitConfirm = value; }, select, enterCreate, enterEdit, exitForm, + confirmExit, backToList, saveForm, }; diff --git a/src/lib/components/patient/list/config/patient-form-config.js b/src/lib/components/patient/list/config/patient-form-config.js index c64a845..09a113e 100644 --- a/src/lib/components/patient/list/config/patient-form-config.js +++ b/src/lib/components/patient/list/config/patient-form-config.js @@ -44,8 +44,10 @@ export const patientInitialForm = { Citizenship: "", Street_1: "", City: "", + CityID: "", Street_2: "", Province: "", + ProvinceID: "", Street_3: "", ZIP: "", Country: "", diff --git a/src/lib/components/patient/list/page/create-page.svelte b/src/lib/components/patient/list/page/create-page.svelte index eb0b0af..5f3fe23 100644 --- a/src/lib/components/patient/list/page/create-page.svelte +++ b/src/lib/components/patient/list/page/create-page.svelte @@ -3,6 +3,7 @@ import FormPageContainer from "$lib/components/patient/reusable/form-page-container.svelte"; import PatientFormRenderer from "$lib/components/patient/reusable/patient-form-renderer.svelte"; import { toast } from "svelte-sonner"; + import ReusableAlertDialog from "$lib/components/reusable/reusable-alert-dialog.svelte"; let props = $props(); @@ -20,12 +21,25 @@ const actions = formActions(handlers); + let showConfirm = $state(false); + + function handleExit() { + const ok = masterDetail.exitForm(); + if (!ok) { + showConfirm = true; + } + } + + function confirmDiscard() { + masterDetail.exitForm(true); + } + async function handleSave() { const result = await formState.save(masterDetail.mode); if (result.status === 'success') { toast('Patient Created!'); - masterDetail?.exitForm(); + masterDetail?.exitForm(true); } else { console.error('Failed to save patient'); } @@ -62,4 +76,9 @@ validateFieldAsync={helpers.validateFieldAsync} mode="create" /> - \ No newline at end of file + + + \ No newline at end of file diff --git a/src/lib/components/patient/list/page/edit-page.svelte b/src/lib/components/patient/list/page/edit-page.svelte index 4ae73aa..223eccc 100644 --- a/src/lib/components/patient/list/page/edit-page.svelte +++ b/src/lib/components/patient/list/page/edit-page.svelte @@ -5,6 +5,7 @@ import { toast } from "svelte-sonner"; import { untrack } from "svelte"; import { API } from "$lib/config/api"; + import ReusableAlertDialog from "$lib/components/reusable/reusable-alert-dialog.svelte"; let props = $props(); @@ -15,6 +16,19 @@ const helpers = usePatientForm(formState, schema); + let showConfirm = $state(false); + + function handleExit() { + const ok = masterDetail.exitForm(); + if (!ok) { + showConfirm = true; + } + } + + function confirmDiscard() { + masterDetail.exitForm(true); + } + $effect(() => { // const backendData = masterDetail?.selectedItem?.patient; // if (!backendData) return; @@ -118,3 +132,8 @@ mode="edit" /> + + \ No newline at end of file diff --git a/src/lib/components/reusable/reusable-alert-dialog.svelte b/src/lib/components/reusable/reusable-alert-dialog.svelte new file mode 100644 index 0000000..1c73982 --- /dev/null +++ b/src/lib/components/reusable/reusable-alert-dialog.svelte @@ -0,0 +1,44 @@ + + + + + + {title} + + {description} + + + + + {cancelText} + + + {confirmText} + + + + \ No newline at end of file diff --git a/src/lib/components/ui/alert-dialog/alert-dialog-action.svelte b/src/lib/components/ui/alert-dialog/alert-dialog-action.svelte new file mode 100644 index 0000000..313fdb5 --- /dev/null +++ b/src/lib/components/ui/alert-dialog/alert-dialog-action.svelte @@ -0,0 +1,18 @@ + + + \ No newline at end of file diff --git a/src/lib/components/ui/alert-dialog/alert-dialog-cancel.svelte b/src/lib/components/ui/alert-dialog/alert-dialog-cancel.svelte new file mode 100644 index 0000000..c8b3fd7 --- /dev/null +++ b/src/lib/components/ui/alert-dialog/alert-dialog-cancel.svelte @@ -0,0 +1,18 @@ + + + \ No newline at end of file diff --git a/src/lib/components/ui/alert-dialog/alert-dialog-content.svelte b/src/lib/components/ui/alert-dialog/alert-dialog-content.svelte new file mode 100644 index 0000000..3c06f5b --- /dev/null +++ b/src/lib/components/ui/alert-dialog/alert-dialog-content.svelte @@ -0,0 +1,25 @@ + + + + + + \ No newline at end of file diff --git a/src/lib/components/ui/alert-dialog/alert-dialog-description.svelte b/src/lib/components/ui/alert-dialog/alert-dialog-description.svelte new file mode 100644 index 0000000..b5cdb41 --- /dev/null +++ b/src/lib/components/ui/alert-dialog/alert-dialog-description.svelte @@ -0,0 +1,17 @@ + + + \ No newline at end of file diff --git a/src/lib/components/ui/alert-dialog/alert-dialog-footer.svelte b/src/lib/components/ui/alert-dialog/alert-dialog-footer.svelte new file mode 100644 index 0000000..953f119 --- /dev/null +++ b/src/lib/components/ui/alert-dialog/alert-dialog-footer.svelte @@ -0,0 +1,18 @@ + + +
+ {@render children?.()} +
\ No newline at end of file diff --git a/src/lib/components/ui/alert-dialog/alert-dialog-header.svelte b/src/lib/components/ui/alert-dialog/alert-dialog-header.svelte new file mode 100644 index 0000000..ec3accc --- /dev/null +++ b/src/lib/components/ui/alert-dialog/alert-dialog-header.svelte @@ -0,0 +1,19 @@ + + +
+ {@render children?.()} +
\ No newline at end of file diff --git a/src/lib/components/ui/alert-dialog/alert-dialog-overlay.svelte b/src/lib/components/ui/alert-dialog/alert-dialog-overlay.svelte new file mode 100644 index 0000000..5f14199 --- /dev/null +++ b/src/lib/components/ui/alert-dialog/alert-dialog-overlay.svelte @@ -0,0 +1,20 @@ + + + \ No newline at end of file diff --git a/src/lib/components/ui/alert-dialog/alert-dialog-portal.svelte b/src/lib/components/ui/alert-dialog/alert-dialog-portal.svelte new file mode 100644 index 0000000..52131da --- /dev/null +++ b/src/lib/components/ui/alert-dialog/alert-dialog-portal.svelte @@ -0,0 +1,7 @@ + + + \ No newline at end of file diff --git a/src/lib/components/ui/alert-dialog/alert-dialog-title.svelte b/src/lib/components/ui/alert-dialog/alert-dialog-title.svelte new file mode 100644 index 0000000..bc0c15d --- /dev/null +++ b/src/lib/components/ui/alert-dialog/alert-dialog-title.svelte @@ -0,0 +1,17 @@ + + + \ No newline at end of file diff --git a/src/lib/components/ui/alert-dialog/alert-dialog-trigger.svelte b/src/lib/components/ui/alert-dialog/alert-dialog-trigger.svelte new file mode 100644 index 0000000..b974f4d --- /dev/null +++ b/src/lib/components/ui/alert-dialog/alert-dialog-trigger.svelte @@ -0,0 +1,7 @@ + + + \ No newline at end of file diff --git a/src/lib/components/ui/alert-dialog/alert-dialog.svelte b/src/lib/components/ui/alert-dialog/alert-dialog.svelte new file mode 100644 index 0000000..bc87887 --- /dev/null +++ b/src/lib/components/ui/alert-dialog/alert-dialog.svelte @@ -0,0 +1,7 @@ + + + \ No newline at end of file diff --git a/src/lib/components/ui/alert-dialog/index.js b/src/lib/components/ui/alert-dialog/index.js new file mode 100644 index 0000000..9812321 --- /dev/null +++ b/src/lib/components/ui/alert-dialog/index.js @@ -0,0 +1,37 @@ +import Root from "./alert-dialog.svelte"; +import Portal from "./alert-dialog-portal.svelte"; +import Trigger from "./alert-dialog-trigger.svelte"; +import Title from "./alert-dialog-title.svelte"; +import Action from "./alert-dialog-action.svelte"; +import Cancel from "./alert-dialog-cancel.svelte"; +import Footer from "./alert-dialog-footer.svelte"; +import Header from "./alert-dialog-header.svelte"; +import Overlay from "./alert-dialog-overlay.svelte"; +import Content from "./alert-dialog-content.svelte"; +import Description from "./alert-dialog-description.svelte"; + +export { + Root, + Title, + Action, + Cancel, + Portal, + Footer, + Header, + Trigger, + Overlay, + Content, + Description, + // + Root as AlertDialog, + Title as AlertDialogTitle, + Action as AlertDialogAction, + Cancel as AlertDialogCancel, + Portal as AlertDialogPortal, + Footer as AlertDialogFooter, + Header as AlertDialogHeader, + Trigger as AlertDialogTrigger, + Overlay as AlertDialogOverlay, + Content as AlertDialogContent, + Description as AlertDialogDescription, +}; \ No newline at end of file diff --git a/src/routes/patient/list/+page.svelte b/src/routes/patient/list/+page.svelte index bf03f6a..63b2474 100644 --- a/src/routes/patient/list/+page.svelte +++ b/src/routes/patient/list/+page.svelte @@ -28,8 +28,8 @@ }, LinkTo: Array.isArray(data.LinkTo) ? data.LinkTo : [], Custodian: data.Custodian ?? { - InternalPID: "", - PatientID: "" + InternalPID: "", + PatientID: "" }, }) }