Skip to content

Commit

Permalink
feat: map custom question to entity
Browse files Browse the repository at this point in the history
  • Loading branch information
9sneha-n committed Sep 30, 2024
1 parent 5cd8e88 commit 00a4cf2
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 73 deletions.
82 changes: 53 additions & 29 deletions src/webapp/pages/form-page/mapFormStateToEntityData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
DiseaseOutbreakEventFormData,
RiskAssessmentGradingFormData,
RiskAssessmentQuestionnaireFormData,
RiskAssessmentQuestionnaireOptions,
RiskAssessmentSummaryFormData,
} from "../../../domain/entities/ConfigurableForm";
import { Maybe } from "../../../utils/ts-utils";
Expand Down Expand Up @@ -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 =
Expand All @@ -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 };
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"
Expand All @@ -673,7 +677,7 @@ function getRiskAssessmentStdQuestionSection(

fields: [
{
id: `${riskAssessmentQuestionnaireCodes[`likelihood${index}`]}`,
id: `std-likelihood${index}`,
label: "Likelihood",
placeholder: "Select..",
isVisible: true,
Expand All @@ -689,7 +693,7 @@ function getRiskAssessmentStdQuestionSection(
disabled: false,
},
{
id: `${riskAssessmentQuestionnaireCodes[`consequences${index}`]}`,
id: `std-consequences${index}`,
label: "Consequences",
placeholder: "Select..",
isVisible: true,
Expand All @@ -705,7 +709,7 @@ function getRiskAssessmentStdQuestionSection(
disabled: false,
},
{
id: `${riskAssessmentQuestionnaireCodes[`risk${index}`]}`,
id: `std-risk${index}`,
label: "Risk",
placeholder: "Select..",
isVisible: true,
Expand All @@ -719,7 +723,7 @@ function getRiskAssessmentStdQuestionSection(
disabled: false,
},
{
id: `${riskAssessmentQuestionnaireCodes[`rational${index}`]}`,
id: `std-rational${index}`,
label: "Rational",
isVisible: true,
errors: [],
Expand All @@ -738,7 +742,7 @@ function getRiskAssessmentStdQuestionSection(
return riskAssesssmentQuestionFormSection;
}

export function getRiskAssessmentCustomQuestionSection(
function getRiskAssessmentCustomQuestionSection(
title: string,
index: number,
options: {
Expand Down Expand Up @@ -771,7 +775,7 @@ export function getRiskAssessmentCustomQuestionSection(
},

{
id: `likelihood-custom${index}`,
id: `custom-likelihood${index}`,
label: "Likelihood",
placeholder: "Select..",
isVisible: true,
Expand All @@ -787,7 +791,7 @@ export function getRiskAssessmentCustomQuestionSection(
disabled: false,
},
{
id: `consequences-custom${index}`,
id: `custom-consequences${index}`,
label: "Consequences",
placeholder: "Select..",
isVisible: true,
Expand All @@ -803,7 +807,7 @@ export function getRiskAssessmentCustomQuestionSection(
disabled: false,
},
{
id: `risk-custom${index}`,
id: `custom-risk${index}`,
label: "Risk",
placeholder: "Select..",
isVisible: true,
Expand All @@ -819,7 +823,7 @@ export function getRiskAssessmentCustomQuestionSection(
disabled: false,
},
{
id: `rational-custom${index}`,
id: `custom-rational${index}`,
label: "Rational",
isVisible: true,
errors: [],
Expand All @@ -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;
}
52 changes: 29 additions & 23 deletions src/webapp/pages/form-page/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 00a4cf2

Please sign in to comment.