Skip to content

Commit

Permalink
Add end of form API call (#641)
Browse files Browse the repository at this point in the history
  • Loading branch information
alimpens authored Dec 19, 2024
1 parent c43c722 commit 80a5a24
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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),
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,20 @@ 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}`

// Pass next panel path to the action
const nextPanelPath = getNextPanelPath(classification, currentPanelIndex, formData)

const extraArgs = {
questionIds,
isLastPanel,
lastPanelPath,
nextPanelPath,
questionIds,
}

const postFormWithExtraArgs = postForm.bind(null, extraArgs)
Expand Down

0 comments on commit 80a5a24

Please sign in to comment.