From 00a4cf2601c9f6c922e06be58e2e7416f8f2b1f0 Mon Sep 17 00:00:00 2001 From: 9sneha-n <9sneha.n@gmail.com> Date: Mon, 30 Sep 2024 22:33:46 +0530 Subject: [PATCH] feat: map custom question to entity --- .../form-page/mapFormStateToEntityData.ts | 82 ++++++++++++------- .../mapRiskAssessmentToInitialFormState.ts | 68 ++++++++++----- src/webapp/pages/form-page/useForm.ts | 52 ++++++------ 3 files changed, 129 insertions(+), 73 deletions(-) diff --git a/src/webapp/pages/form-page/mapFormStateToEntityData.ts b/src/webapp/pages/form-page/mapFormStateToEntityData.ts index fbe5b90b..9fa50f58 100644 --- a/src/webapp/pages/form-page/mapFormStateToEntityData.ts +++ b/src/webapp/pages/form-page/mapFormStateToEntityData.ts @@ -19,6 +19,7 @@ import { DiseaseOutbreakEventFormData, RiskAssessmentGradingFormData, RiskAssessmentQuestionnaireFormData, + RiskAssessmentQuestionnaireOptions, RiskAssessmentSummaryFormData, } from "../../../domain/entities/ConfigurableForm"; import { Maybe } from "../../../utils/ts-utils"; @@ -394,40 +395,37 @@ function mapFormStateToRiskAssessmentQuestionnaire( const allFields: FormFieldState[] = getAllFieldsFromSections(formState.sections); const indexes = ["1", "2", "3"] as const; - const questions = indexes.map(index => { - const likelihood = allFields.find(field => - field.id.includes(riskAssessmentQuestionnaireCodes[`likelihood${index}`]) - )?.value as string; - const likelihoodOption = formData.options.likelihood.find( - option => option.id === likelihood - ); - if (!likelihoodOption) throw new Error("Likelihood not found"); - - const consequences = allFields.find(field => - field.id.includes(riskAssessmentQuestionnaireCodes[`consequences${index}`]) - )?.value as string; - const consequencesOption = formData.options.consequences.find( - option => option.id === consequences - ); - if (!consequencesOption) throw new Error("Consequences not found"); - - const risk = allFields.find(field => - field.id.includes(riskAssessmentQuestionnaireCodes[`risk${index}`]) - )?.value as string; - const riskOption = formData.options.risk.find(option => option.id === risk); - if (!riskOption) throw new Error("Risk not found"); - - const question: RiskAssessmentQuestion = { + const questions = indexes.map((index): RiskAssessmentQuestion => { + const { likelihoodOption, consequencesOption, riskOption } = + getRiskAssessmentQuestionsWithOption("std", allFields, formData.options, index); + return { likelihood: likelihoodOption, consequences: consequencesOption, risk: riskOption, - rational: allFields.find(field => - field.id.includes(riskAssessmentQuestionnaireCodes[`rational${index}`]) - )?.value as string, + rational: allFields.find(field => field.id.includes(`std-rational${index}`)) + ?.value as string, }; - return question; }); + const additionalQuestions = formState.sections + .filter(section => section.id.startsWith("additionalQuestions")) + .map((_customSection, index): RiskAssessmentQuestion => { + const { likelihoodOption, consequencesOption, riskOption } = + getRiskAssessmentQuestionsWithOption( + "custom", + allFields, + formData.options, + index.toString() + ); + return { + likelihood: likelihoodOption, + consequences: consequencesOption, + risk: riskOption, + rational: allFields.find(field => field.id.includes(`rational-custom${index}`)) + ?.value as string, + }; + }); + if (!questions[0] || !questions[1] || !questions[2]) throw new Error("Questions not found"); const riskAssessmentQuestionnaire: RiskAssessmentQuestionnaire = @@ -436,7 +434,33 @@ function mapFormStateToRiskAssessmentQuestionnaire( potentialRiskForHumanHealth: questions[0], riskOfEventSpreading: questions[1], riskOfInsufficientCapacities: questions[2], - additionalQuestions: [], + additionalQuestions: additionalQuestions, }); return riskAssessmentQuestionnaire; } + +function getRiskAssessmentQuestionsWithOption( + questionType: "std" | "custom", + allFields: FormFieldState[], + options: RiskAssessmentQuestionnaireOptions, + index: string +) { + const likelihood = allFields.find(field => + field.id.includes(`${questionType}-likelihood${index}`) + )?.value as string; + const likelihoodOption = options.likelihood.find(option => option.id === likelihood); + if (!likelihoodOption) throw new Error("Likelihood not found"); + + const consequences = allFields.find(field => + field.id.includes(`${questionType}-consequences${index}`) + )?.value as string; + const consequencesOption = options.consequences.find(option => option.id === consequences); + if (!consequencesOption) throw new Error("Consequences not found"); + + const risk = allFields.find(field => field.id.includes(`${questionType}-risk${index}`)) + ?.value as string; + const riskOption = options.risk.find(option => option.id === risk); + if (!riskOption) throw new Error("Risk not found"); + + return { likelihoodOption, consequencesOption, riskOption }; +} diff --git a/src/webapp/pages/form-page/risk-assessment/mapRiskAssessmentToInitialFormState.ts b/src/webapp/pages/form-page/risk-assessment/mapRiskAssessmentToInitialFormState.ts index 82830e0e..2b62a241 100644 --- a/src/webapp/pages/form-page/risk-assessment/mapRiskAssessmentToInitialFormState.ts +++ b/src/webapp/pages/form-page/risk-assessment/mapRiskAssessmentToInitialFormState.ts @@ -620,18 +620,7 @@ export function mapRiskAssessmentQuestionnaireToInitialFormState( return getRiskAssessmentCustomQuestionSection("Custom Question", index, sectionOptions); }) ?? []; - const addNewOptionSection: FormSectionState = { - id: "addNewOptionSection", - isVisible: true, - fields: [], - addNewField: { - id: "addNewOption", - isVisible: true, - errors: [], - type: "addNew", - value: null, - }, - }; + const addNewOptionSection: FormSectionState = getAnotherOptionSection(); //SNEHA TO DO : Create this form by iterating over Object Keys return { @@ -650,6 +639,21 @@ export function mapRiskAssessmentQuestionnaireToInitialFormState( }; } +export function getAnotherOptionSection(): FormSectionState { + return { + id: "addNewOptionSection", + isVisible: true, + fields: [], + addNewField: { + id: "addNewOption", + isVisible: true, + errors: [], + type: "addNew", + value: null, + }, + }; +} + function getRiskAssessmentStdQuestionSection( title: | "Potential Risk For Human Health" @@ -673,7 +677,7 @@ function getRiskAssessmentStdQuestionSection( fields: [ { - id: `${riskAssessmentQuestionnaireCodes[`likelihood${index}`]}`, + id: `std-likelihood${index}`, label: "Likelihood", placeholder: "Select..", isVisible: true, @@ -689,7 +693,7 @@ function getRiskAssessmentStdQuestionSection( disabled: false, }, { - id: `${riskAssessmentQuestionnaireCodes[`consequences${index}`]}`, + id: `std-consequences${index}`, label: "Consequences", placeholder: "Select..", isVisible: true, @@ -705,7 +709,7 @@ function getRiskAssessmentStdQuestionSection( disabled: false, }, { - id: `${riskAssessmentQuestionnaireCodes[`risk${index}`]}`, + id: `std-risk${index}`, label: "Risk", placeholder: "Select..", isVisible: true, @@ -719,7 +723,7 @@ function getRiskAssessmentStdQuestionSection( disabled: false, }, { - id: `${riskAssessmentQuestionnaireCodes[`rational${index}`]}`, + id: `std-rational${index}`, label: "Rational", isVisible: true, errors: [], @@ -738,7 +742,7 @@ function getRiskAssessmentStdQuestionSection( return riskAssesssmentQuestionFormSection; } -export function getRiskAssessmentCustomQuestionSection( +function getRiskAssessmentCustomQuestionSection( title: string, index: number, options: { @@ -771,7 +775,7 @@ export function getRiskAssessmentCustomQuestionSection( }, { - id: `likelihood-custom${index}`, + id: `custom-likelihood${index}`, label: "Likelihood", placeholder: "Select..", isVisible: true, @@ -787,7 +791,7 @@ export function getRiskAssessmentCustomQuestionSection( disabled: false, }, { - id: `consequences-custom${index}`, + id: `custom-consequences${index}`, label: "Consequences", placeholder: "Select..", isVisible: true, @@ -803,7 +807,7 @@ export function getRiskAssessmentCustomQuestionSection( disabled: false, }, { - id: `risk-custom${index}`, + id: `custom-risk${index}`, label: "Risk", placeholder: "Select..", isVisible: true, @@ -819,7 +823,7 @@ export function getRiskAssessmentCustomQuestionSection( disabled: false, }, { - id: `rational-custom${index}`, + id: `custom-rational${index}`, label: "Rational", isVisible: true, errors: [], @@ -839,3 +843,25 @@ export function getRiskAssessmentCustomQuestionSection( }; return riskAssesssmentQuestionFormSection; } + +export function addNewCustomQuestionSection(sections: FormSectionState[]): FormSectionState { + const customQuestionSections = sections.filter(section => + section.id.startsWith("additionalQuestions") + ); + + const newCustomQuestionSection = getRiskAssessmentCustomQuestionSection( + "Custom Question", + customQuestionSections.length, + { + riskAssessmentQuestionnaire: undefined, + likelihoodOptions: + sections[0]?.fields[0]?.type === "select" ? sections[0].fields[0].options : [], + consequencesOptions: + sections[0]?.fields[1]?.type === "select" ? sections[0].fields[1].options : [], + riskOptions: + sections[0]?.fields[2]?.type === "select" ? sections[0].fields[2].options : [], + } + ); + + return newCustomQuestionSection; +} diff --git a/src/webapp/pages/form-page/useForm.ts b/src/webapp/pages/form-page/useForm.ts index 88bcda53..cf4e1bcb 100644 --- a/src/webapp/pages/form-page/useForm.ts +++ b/src/webapp/pages/form-page/useForm.ts @@ -12,7 +12,10 @@ import { FormType } from "./FormPage"; import { ConfigurableForm, FormLables } from "../../../domain/entities/ConfigurableForm"; import { mapEntityToFormState } from "./mapEntityToFormState"; import { useCurrentEventTracker } from "../../contexts/current-event-tracker-context"; -import { getRiskAssessmentCustomQuestionSection } from "./risk-assessment/mapRiskAssessmentToInitialFormState"; +import { + addNewCustomQuestionSection, + getAnotherOptionSection, +} from "./risk-assessment/mapRiskAssessmentToInitialFormState"; export type GlobalMessage = { text: string; @@ -89,37 +92,40 @@ export function useForm(formType: FormType, id?: Id): State { case "risk-assessment-questionnaire": { setFormState(prevState => { if (prevState.kind === "loaded") { - const customQuestionSections = prevState.data.sections.filter(section => - section.id.startsWith("additionalQuestions") + const otherSections = prevState.data.sections.filter( + section => section.id !== "addNewOptionSection" ); + const addAnotherSection = getAnotherOptionSection(); - const newCustomQuestionSection = getRiskAssessmentCustomQuestionSection( - "Custom Question", - customQuestionSections.length, - { - riskAssessmentQuestionnaire: undefined, - likelihoodOptions: - customQuestionSections[0]?.fields[0]?.type === "select" - ? customQuestionSections[0].fields[0].options - : [], - consequencesOptions: - customQuestionSections[0]?.fields[1]?.type === "select" - ? customQuestionSections[0].fields[1].options - : [], - riskOptions: - customQuestionSections[0]?.fields[2]?.type === "select" - ? customQuestionSections[0].fields[2].options - : [], - } + const newCustomQuestionSection = addNewCustomQuestionSection( + prevState.data.sections ); + const updatedData = { ...prevState.data, - sections: [...prevState.data.sections, newCustomQuestionSection], + sections: [ + ...otherSections, + newCustomQuestionSection, + addAnotherSection, + ], }; + const allNewFields = newCustomQuestionSection.fields; + + const updatedAndValidatedData = allNewFields.reduce( + (acc, updatedFields) => { + return updateAndValidateFormState( + acc, + updatedFields, + configurableForm + ); + }, + updatedData + ); + return { kind: "loaded", - data: updatedData, + data: updatedAndValidatedData, }; } else { return prevState;