From b18e569053e7ba471386ff3f25a7119de1c53d09 Mon Sep 17 00:00:00 2001 From: wayangalihpratama Date: Wed, 25 Dec 2024 18:16:28 +0800 Subject: [PATCH] [#558] Handle load data NA & comment --- .../src/pages/data-cleaning/DataDetail.jsx | 5 +- frontend/src/pages/survey/WebformPage.jsx | 48 ++++++++++++++++++- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/frontend/src/pages/data-cleaning/DataDetail.jsx b/frontend/src/pages/data-cleaning/DataDetail.jsx index bce3cfa2..9208e8f8 100644 --- a/frontend/src/pages/data-cleaning/DataDetail.jsx +++ b/frontend/src/pages/data-cleaning/DataDetail.jsx @@ -83,7 +83,7 @@ const DataDetail = ({ record }) => { }); record["answer"] = recordAnswer; - const history = record?.history?.map((h) => { + let history = record?.history?.map((h) => { const identifierAnswer = h?.answer?.filter((a) => a?.is_repeat_identifier); const historyAnswer = h?.answer?.map((ha) => { // handle repeat group index with is_repeat_identifier @@ -113,6 +113,9 @@ const DataDetail = ({ record }) => { }); return { ...h, answer: historyAnswer }; }); + // show only 1 history + history = _.orderBy(history, "year", "desc"); + // history = history?.length ? [history[0]] : []; // EOL Remap answer and history to provide the repeat_identifier // generate history columns diff --git a/frontend/src/pages/survey/WebformPage.jsx b/frontend/src/pages/survey/WebformPage.jsx index 0f74b489..8438261f 100644 --- a/frontend/src/pages/survey/WebformPage.jsx +++ b/frontend/src/pages/survey/WebformPage.jsx @@ -412,6 +412,16 @@ const WebformPage = ({ const { data, status } = res; // const { form, initial_values, mismatch, collaborators } = test; // testing purpose const { form, initial_values, mismatch, collaborators } = data; + // handle leading question + const questionWithLeadingQuestionGroup = form.question_group + .flatMap((qg) => { + const question = qg.question.map((q) => ({ + ...q, + lead_by_question: qg?.leading_question, + })); + return question; + }) + ?.filter((q) => q?.lead_by_question); // submission already submitted if (status === 208) { setErrorPage(true); @@ -424,8 +434,24 @@ const WebformPage = ({ setSavedData(initial_values); const answers = initial_values.answer.map((a) => { const { question, repeat_index, comment } = a; + // handle leading question + let repeatIndexString = + repeat_index > 0 ? String(repeat_index) : null; + const findQuestion = questionWithLeadingQuestionGroup.find( + (q) => q.id === question + ); + if (findQuestion?.lead_by_question) { + const leadingAnswer = initial_values.answer.find( + (a) => a.question === findQuestion.lead_by_question + )?.value; + repeatIndexString = + leadingAnswer?.[repeat_index] || repeat_index; + } + // eol handle leading question const commentQid = - repeat_index > 0 ? `${question}-${repeat_index}` : question; + repeat_index > 0 || repeatIndexString + ? `${question}-${repeatIndexString}` + : question; commentValues = { ...commentValues, [commentQid]: comment, @@ -433,6 +459,8 @@ const WebformPage = ({ return { ...a, repeatIndex: repeat_index, + repeat_index: repeat_index, + repeat_index_string: repeatIndexString, }; }); setInitialAnswers(answers); @@ -444,8 +472,24 @@ const WebformPage = ({ setInitialAnswers(answer); answer.forEach((a) => { const { question, repeat_index, comment } = a; + // handle leading question + let repeatIndexString = + repeat_index > 0 ? String(repeat_index) : null; + const findQuestion = questionWithLeadingQuestionGroup.find( + (q) => q.id === question + ); + if (findQuestion?.lead_by_question) { + const leadingAnswer = initial_values.answer.find( + (a) => a.question === findQuestion.lead_by_question + )?.value; + repeatIndexString = + leadingAnswer?.[repeat_index] || repeat_index; + } + // eol handle leading question const commentQid = - repeat_index > 0 ? `${question}-${repeat_index}` : question; + repeat_index > 0 || repeatIndexString + ? `${question}-${repeatIndexString}` + : question; commentValues = { ...commentValues, [commentQid]: comment,