Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable form flow without classification #730

Merged
merged 8 commits into from
Mar 3, 2025
20 changes: 13 additions & 7 deletions apps/public/src/app/(general)/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,36 @@
import { cookies } from 'next/headers'
import { redirect } from 'next/navigation'

import type { ApiError } from 'apps/public/src/apiClientProxy'
import { getFormClassificationByClassificationId, postMelding } from 'apps/public/src/apiClientProxy'

export const postPrimaryForm = async (_: unknown, formData: FormData) => {
const formDataObj = Object.fromEntries(formData)

let nextPage = ''
let nextPage = '/locatie'

try {
const { classification, id, token } = await postMelding({ requestBody: { text: formDataObj.primary.toString() } })

if (classification) {
// Set session variables in cookies
const cookieStore = await cookies()
cookieStore.set('id', id.toString())
cookieStore.set('token', token)
// Set session variables in cookies
const cookieStore = await cookies()
cookieStore.set('id', id.toString())
cookieStore.set('token', token)

if (classification) {
// Get entire form, in order to redirect to its first panel
const nextFormData = await getFormClassificationByClassificationId({ classificationId: classification })
const nextFormFirstKey = nextFormData.components && nextFormData.components[0].key

nextPage = `/aanvullende-vragen/${classification}/${nextFormFirstKey}`
}
} catch (error) {
return { message: (error as Error).message }
// If there are no additional questions for a classification, redirect to /locatie.
if ((error as ApiError)?.status === 404) {
redirect(nextPage)
}

return { message: (error as ApiError).message }
}

return redirect(nextPage)
Expand Down