From bd49291bf22e255b96efa71379e6a4f2dc4f4726 Mon Sep 17 00:00:00 2001 From: Denis Lugowski Date: Mon, 14 Jul 2025 06:37:13 +0200 Subject: [PATCH] fix(frontend): Keep dirty state when switching sections --- .../application-forms/[id]/[sectionIndex].vue | 46 +++++++------------ legalconsenthub/pages/create.vue | 25 ++++------ 2 files changed, 27 insertions(+), 44 deletions(-) diff --git a/legalconsenthub/pages/application-forms/[id]/[sectionIndex].vue b/legalconsenthub/pages/application-forms/[id]/[sectionIndex].vue index 1b965d9..43b630a 100644 --- a/legalconsenthub/pages/application-forms/[id]/[sectionIndex].vue +++ b/legalconsenthub/pages/application-forms/[id]/[sectionIndex].vue @@ -72,6 +72,16 @@ const { getApplicationFormById, updateApplicationForm } = useApplicationForm() const route = useRoute() const { user } = useAuth() +definePageMeta({ + // Prevent whole page from re-rendering when navigating between sections to keep state + key: (route) => `${route.params.id}`, +}) + +onMounted(() => { + const sectionIndex = parseInt(route.params.sectionIndex[0]) + activeStepperItemIndex.value = !isNaN(sectionIndex) ? sectionIndex : 0 +}) + const items = [ [ { @@ -85,25 +95,12 @@ const items = [ const stepper = useTemplateRef('stepper') const activeStepperItemIndex = ref(0) -const currentFormElementSection = computed(() => { - return applicationForm.value?.formElementSections[activeStepperItemIndex.value] -}) - -watch(activeStepperItemIndex, async (newActiveStepperItem: number) => { - activeStepperItemIndex.value = newActiveStepperItem - await navigateTo(`/application-forms/${route.params.id}/${newActiveStepperItem}`) -}) - -watch( - () => route.path, - (_) => { - const sectionIndex = parseInt(route.params.sectionIndex[0]) - activeStepperItemIndex.value = !isNaN(sectionIndex) ? sectionIndex : 0 - }, - { immediate: true } +const currentFormElementSection = computed( + () => applicationForm.value?.formElementSections[activeStepperItemIndex.value] ) -const { data, error } = await useAsyncData(async () => { +const { data, error } = await useAsyncData(`application-form-${route.params.id}`, async () => { + console.log('Fetching application form with ID:', route.params.id) return await getApplicationFormById(Array.isArray(route.params.id) ? route.params.id[0] : route.params.id) }) @@ -111,14 +108,7 @@ if (error.value) { throw createError({ statusText: error.value.message }) } -const applicationForm = computed({ - get: () => data?.value as ApplicationFormDto, - set: (val: ApplicationFormDto) => { - if (val && data.value) { - data.value = val - } - } -}) +const applicationForm = computed(() => data?.value as ApplicationFormDto) const isReadOnly = computed(() => { return applicationForm.value?.createdBy.id !== user.value?.id @@ -126,7 +116,7 @@ const isReadOnly = computed(() => { const stepperItems = computed(() => { const stepperItems: StepperItem[] = [] - applicationForm.value.formElementSections.forEach((section: FormElementSectionDto, index: number, _) => { + applicationForm.value.formElementSections.forEach((section: FormElementSectionDto) => { stepperItems.push({ title: section.shortTitle, description: section.description @@ -141,9 +131,7 @@ async function navigateStepper(direction: 'forward' | 'backward') { } else { stepper.value?.prev() } - const targetSectionIndex = - direction === 'forward' ? activeStepperItemIndex.value + 1 : activeStepperItemIndex.value - 1 - await navigateTo(`/application-forms/${route.params.id}/${targetSectionIndex}`) + await navigateTo(`/application-forms/${route.params.id}/${activeStepperItemIndex.value}`) } async function onSubmit() { diff --git a/legalconsenthub/pages/create.vue b/legalconsenthub/pages/create.vue index 7e04920..67f4ced 100644 --- a/legalconsenthub/pages/create.vue +++ b/legalconsenthub/pages/create.vue @@ -26,7 +26,7 @@ - +

{{ currentFormElementSection.title }}

@@ -72,14 +72,14 @@ const { validateFormElements, getHighestComplianceStatus } = useApplicationFormV const { userDto, selectedOrganization } = useAuth() const stepper = useTemplateRef('stepper') -const activeStepperItem = ref(0) +const activeStepperItemIndex = ref(0) -const currentFormElementSection = computed(() => { - return applicationFormTemplate.value?.formElementSections[activeStepperItem.value] -}) +const currentFormElementSection = computed( + () => applicationFormTemplate.value?.formElementSections[activeStepperItemIndex.value] +) -watch(activeStepperItem, async (newActiveStepperItem: number) => { - activeStepperItem.value = newActiveStepperItem +watch(activeStepperItemIndex, async (newActiveStepperItem: number) => { + activeStepperItemIndex.value = newActiveStepperItem }) const { data, error } = await useAsyncData(async () => { @@ -113,15 +113,10 @@ async function navigateStepper(direction: 'forward' | 'backward') { } } -const applicationFormTemplate = computed({ +const applicationFormTemplate = computed( // TODO: Don't select always the first item, allow user to select a template - get: () => data?.value?.content[0] ?? undefined, - set: (val) => { - if (val && data.value) { - data.value.content[0] = val - } - } -}) + () => data?.value?.content[0] ?? undefined +) const formElements = computed({ get: () => currentFormElementSection?.value?.formElements ?? [],