diff --git a/web-citizen-reporting/web-citizen-reporting-template/src/api/get-forms.ts b/web-citizen-reporting/web-citizen-reporting-template/src/api/get-forms.ts index c4e20d432..7f475847e 100644 --- a/web-citizen-reporting/web-citizen-reporting-template/src/api/get-forms.ts +++ b/web-citizen-reporting/web-citizen-reporting-template/src/api/get-forms.ts @@ -7,3 +7,13 @@ export const getForms = (): Promise> => { `/api/election-rounds/${electionRoundId}/citizen-reporting-forms` ).then((res) => res.data.forms); }; + +export const getFormById = async ( + formId: string +): Promise => { + const formData = getForms().then((forms) => + forms.find((form) => form.id === formId) + ); + + return formData; +}; diff --git a/web-citizen-reporting/web-citizen-reporting-template/src/components/ReportAnswersStep.tsx b/web-citizen-reporting/web-citizen-reporting-template/src/components/ReportAnswersStep.tsx index adca0797f..3025bf8b2 100644 --- a/web-citizen-reporting/web-citizen-reporting-template/src/components/ReportAnswersStep.tsx +++ b/web-citizen-reporting/web-citizen-reporting-template/src/components/ReportAnswersStep.tsx @@ -1,10 +1,3 @@ -import { - type BaseQuestion, - type MultiSelectQuestion, - type SingleSelectQuestion, -} from "@/common/types"; -import { Button } from "@/components/ui/button"; -import { Calendar } from "@/components/ui/calendar"; import { Card, CardContent, @@ -12,41 +5,11 @@ import { CardHeader, CardTitle, } from "@/components/ui/card"; -import { Checkbox } from "@/components/ui/checkbox"; -import { FileUploader } from "@/components/ui/file-uploader"; -import { - FormControl, - FormDescription, - FormField, - FormItem, - FormLabel, - FormMessage, -} from "@/components/ui/form"; -import { Input } from "@/components/ui/input"; -import { - Popover, - PopoverContent, - PopoverTrigger, -} from "@/components/ui/popover"; -import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; -import { Separator } from "@/components/ui/separator"; -import { Textarea } from "@/components/ui/textarea"; -import { useUploadFile } from "@/hooks/use-upload-file"; -import { - cn, - isDateQuestion, - isMultiSelectQuestion, - isNumberQuestion, - isSingleSelectQuestion, - isTextQuestion, -} from "@/lib/utils"; -import { formsOptions } from "@/queries/use-forms"; +import { currentFormLanguageAtom } from "@/features/forms/atoms"; +import { FormQuestion } from "@/features/forms/components/FormQuestion"; import { Route } from "@/routes/forms/$formId"; -import { useSuspenseQuery } from "@tanstack/react-query"; -import { notFound } from "@tanstack/react-router"; -import { format } from "date-fns"; -import { CalendarIcon } from "lucide-react"; -import React, { useCallback, useEffect, useState } from "react"; +import { useAtom } from "jotai"; +import { useEffect } from "react"; import { useFormContext } from "react-hook-form"; import { Select, @@ -57,103 +20,18 @@ import { SelectValue, } from "./ui/select"; -function QuestionText({ - languageCode, - question, -}: { - languageCode: string; - question: BaseQuestion; -}) { - return ( - - {question.code} - - - {question.text[languageCode]} - - ); -} - -function QuestionDescription({ - languageCode, - question, -}: { - languageCode: string; - question: BaseQuestion; -}) { - return ( - {question?.helptext?.[languageCode]} - ); -} - function ReportAnswersStep() { - const { formId } = Route.useParams(); - const { data: citizenReportFoms } = useSuspenseQuery(formsOptions()); - const [loading, setLoading] = React.useState(false); - const { onUpload, progresses, uploadedFiles, isUploading } = useUploadFile({ - defaultUploadedFiles: [], - }); - - const citizenReportForm = citizenReportFoms.find((f) => f.id === formId); - - if (citizenReportForm === undefined) { - throw notFound({ throw: false }); - } - - const form = useFormContext(); - - const formValues = form.watch(); - - const [selectedLanguage, setSelectedLanguage] = useState( - citizenReportForm.defaultLanguage - ); - - useEffect(() => { - citizenReportForm.questions.forEach((question) => { - if (isMultiSelectQuestion(question)) { - form.setValue(`question-${question.id}.selection`, []); - } - - if (isTextQuestion(question)) { - form.setValue(`question-${question.id}`, ""); - } - }); - }, [form.setValue, citizenReportForm]); - - const questionHasFreeTextOption = useCallback( - (question: SingleSelectQuestion | MultiSelectQuestion) => { - return question.options.some((option) => option.isFreeText); - }, - [] + const citizenReportForm = Route.useLoaderData(); + const [selectedLanguage, setSelectedLanguage] = useAtom( + currentFormLanguageAtom ); - const getFreeTextOption = useCallback( - (question: SingleSelectQuestion | MultiSelectQuestion) => { - return question.options.find((option) => option.isFreeText); - }, - [] - ); - - const isFreeTextOptionSelected = useCallback( - (question: SingleSelectQuestion | MultiSelectQuestion) => { - const freeTextOption = getFreeTextOption(question); - const selection = formValues[`question-${question.id}.selection`]; - - if (!selection) { - return false; - } - - if (isSingleSelectQuestion(question)) { - return !!selection || selection === freeTextOption?.id; - } else { - return selection.some((s: string) => s === freeTextOption?.id); - } - }, - [formValues] - ); + const { watch } = useFormContext(); + const values = watch(); - function onSubmit() { - console.log("submit"); - } + useEffect(() => { + console.table(values); + }, [values]); return ( @@ -190,313 +68,7 @@ function ReportAnswersStep() { {citizenReportForm.questions.map((question) => ( -
- {isTextQuestion(question) && ( - ( - - - - - -