From 80a5a24f65eb24771ee30e0578e1e99a9f05bf6d Mon Sep 17 00:00:00 2001 From: Aram <37216945+alimpens@users.noreply.github.com> Date: Thu, 19 Dec 2024 17:21:20 +0100 Subject: [PATCH] Add end of form API call (#641) --- .../[classification]/[panelId]/actions.ts | 26 ++++++++++++++----- .../[classification]/[panelId]/page.tsx | 6 ++++- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/apps/public/src/app/(general)/aanvullende-vragen/[classification]/[panelId]/actions.ts b/apps/public/src/app/(general)/aanvullende-vragen/[classification]/[panelId]/actions.ts index 63f4a9b..b00c878 100644 --- a/apps/public/src/app/(general)/aanvullende-vragen/[classification]/[panelId]/actions.ts +++ b/apps/public/src/app/(general)/aanvullende-vragen/[classification]/[panelId]/actions.ts @@ -1,25 +1,32 @@ 'use server' -import { postMeldingByMeldingIdQuestionByQuestionId } from '@meldingen/api-client' +import { postMeldingByMeldingIdQuestionByQuestionId, putMeldingByMeldingIdAnswerQuestions } from '@meldingen/api-client' import { cookies } from 'next/headers' import { redirect } from 'next/navigation' import { mergeCheckboxAnswers } from './_utils/mergeCheckboxAnswers' type ArgsType = { - questionIds: { key: string; id: number }[] + isLastPanel: boolean lastPanelPath: string nextPanelPath: string + questionIds: { key: string; id: number }[] } -export const postForm = async (args: ArgsType, _: unknown, formData: FormData) => { +export const postForm = async ( + { isLastPanel, lastPanelPath, nextPanelPath, questionIds }: ArgsType, + _: unknown, + formData: FormData, +) => { // Get session variables from cookies const cookieStore = await cookies() const meldingId = cookieStore.get('id')?.value const token = cookieStore.get('token')?.value + if (!meldingId || !token) return undefined + // Set last panel path in cookies - cookieStore.set('lastPanelPath', args.lastPanelPath) + cookieStore.set('lastPanelPath', lastPanelPath) // Checkbox answers are stored as separate key-value pairs in the FormData object. // This function merges these answers into a single string value per question, using an identifier in the Checkbox component. @@ -34,9 +41,9 @@ export const postForm = async (args: ArgsType, _: unknown, formData: FormData) = // Filter out empty answers if (value.length === 0) return undefined - const questionId = args.questionIds.find((component) => component.key === key)?.id + const questionId = questionIds.find((component) => component.key === key)?.id - if (!meldingId || !questionId || !token) return undefined + if (!questionId) return undefined return postMeldingByMeldingIdQuestionByQuestionId({ meldingId: parseInt(meldingId, 10), @@ -55,5 +62,10 @@ export const postForm = async (args: ArgsType, _: unknown, formData: FormData) = return { message: erroredResults.map((error) => error.message).join(', ') } } - return redirect(args.nextPanelPath) + // Let BE know the last panel has successfully been submitted + if (isLastPanel) { + putMeldingByMeldingIdAnswerQuestions({ meldingId: parseInt(meldingId, 10), token }) + } + + return redirect(nextPanelPath) } diff --git a/apps/public/src/app/(general)/aanvullende-vragen/[classification]/[panelId]/page.tsx b/apps/public/src/app/(general)/aanvullende-vragen/[classification]/[panelId]/page.tsx index df67ef1..c729afa 100644 --- a/apps/public/src/app/(general)/aanvullende-vragen/[classification]/[panelId]/page.tsx +++ b/apps/public/src/app/(general)/aanvullende-vragen/[classification]/[panelId]/page.tsx @@ -72,6 +72,9 @@ export default async ({ params }: { params: Params }) => { id: question.question, })) + // Pass isLastPanel to the action + const isLastPanel = currentPanelIndex === formData.components.length - 1 + // Pass last panel path to the action const lastPanelPath = `/aanvullende-vragen/${classification}/${formData.components[formData.components.length - 1].key}` @@ -79,9 +82,10 @@ export default async ({ params }: { params: Params }) => { const nextPanelPath = getNextPanelPath(classification, currentPanelIndex, formData) const extraArgs = { - questionIds, + isLastPanel, lastPanelPath, nextPanelPath, + questionIds, } const postFormWithExtraArgs = postForm.bind(null, extraArgs)