Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/ohcnetwork/care_fe into …
Browse files Browse the repository at this point in the history
…structured_prefills
  • Loading branch information
amjithtitus09 committed Jan 25, 2025
2 parents cbfa87a + acbb262 commit ba2a421
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
22 changes: 21 additions & 1 deletion renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,25 @@
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:recommended"
],
"labels": [
"dependencies"
],
"reviewers": [
"@ohcnetwork/care-fe-code-reviewers"
],
"automergeStrategy": "squash",
"automergeType": "pr",
"bumpVersion": "minor",
"packageRules": [
{
"groupName": "all dependencies (minor, patch)",
"matchUpdateTypes": [
"minor",
"patch"
],
"matchCurrentVersion": "!/^0/",
"automerge": true
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ export function NumberQuestion({
disabled,
}: NumberQuestionProps) {
const handleChange = (value: string) => {
const emptyValue = value === "";
const numericValue =
question.type === "decimal" ? parseFloat(value) : parseInt(value);

updateQuestionnaireResponseCB(
[
{
type: "number",
value: numericValue,
value: emptyValue ? undefined : numericValue,
},
],
questionnaireResponse.question_id,
Expand Down
65 changes: 62 additions & 3 deletions src/components/Questionnaire/QuestionnaireForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ export function QuestionnaireForm({

results.forEach((result, index) => {
const form = updatedForms[index];
if (!result.data?.errors) {
return;
}

result.data.errors.forEach(
(error: QuestionValidationError | DetailedValidationError) => {
Expand Down Expand Up @@ -211,13 +214,68 @@ export function QuestionnaireForm({

const handleSubmit = async () => {
setIsDirty(false);
if (hasErrors) return;

// Clear existing errors first
const formsWithClearedErrors = questionnaireForms.map((form) => ({
...form,
errors: [],
}));
let firstErrorId: string | undefined = undefined;

// Validate all required fields
const formsWithValidation = formsWithClearedErrors.map((form) => {
const errors: QuestionValidationError[] = [];

const validateQuestion = (q: Question) => {
// Handle nested questions in groups
if (q.type === "group" && q.questions) {
q.questions.forEach(validateQuestion);
return;
}

if (q.required) {
const response = form.responses.find((r) => r.question_id === q.id);
const hasValue = response?.values?.some(
(v) => v.value !== undefined && v.value !== null && v.value !== "",
);

if (!hasValue) {
errors.push({
question_id: q.id,
error: t("field_required"),
type: "validation_error",
msg: t("field_required"),
});
firstErrorId = firstErrorId ? firstErrorId : q.id;
}
}
};

form.questionnaire.questions.forEach(validateQuestion);
return { ...form, errors };
});

setQuestionnaireForms(formsWithValidation);

if (firstErrorId) {
const element = document.querySelector(
`[data-question-id="${firstErrorId}"]`,
);
if (element) {
element.scrollIntoView({ behavior: "smooth", block: "center" });
}
}

if (formsWithValidation.some((form) => form.errors.length > 0)) {
return;
}

// Continue with existing submission logic...
const requests: BatchRequest[] = [];
if (encounterId && patientId) {
const context = { patientId, encounterId };
// First, collect all structured data requests if encounterId is provided
questionnaireForms.forEach((form) => {
formsWithValidation.forEach((form) => {
form.responses.forEach((response) => {
if (response.structured_type) {
const structuredData = response.values?.[0]?.value;
Expand All @@ -235,7 +293,7 @@ export function QuestionnaireForm({
}

// Then, add questionnaire submission requests
questionnaireForms.forEach((form) => {
formsWithValidation.forEach((form) => {
const nonStructuredResponses = form.responses.filter(
(response) => !response.structured_type,
);
Expand Down Expand Up @@ -375,6 +433,7 @@ export function QuestionnaireForm({
? { ...r, values, note: note }
: r,
),
errors: [],
}
: formItem,
),
Expand Down

0 comments on commit ba2a421

Please sign in to comment.