From e417ff7838ca4802a425ce302291ff7a784a7ee2 Mon Sep 17 00:00:00 2001 From: Wilson Mutua Date: Mon, 28 Oct 2024 08:20:26 +0300 Subject: [PATCH] check for duplicate added answer --- src/components/interactive-builder/add-question.modal.tsx | 7 ++++++- src/components/interactive-builder/edit-question.modal.tsx | 6 +++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/components/interactive-builder/add-question.modal.tsx b/src/components/interactive-builder/add-question.modal.tsx index 17719a2c..999192d6 100644 --- a/src/components/interactive-builder/add-question.modal.tsx +++ b/src/components/interactive-builder/add-question.modal.tsx @@ -215,8 +215,13 @@ const AddQuestionModal: React.FC = ({ setConceptAnsToLookup(''); setSelectedAnsConcept(concept); const newAnswer = { id: concept.uuid, text: concept.display }; - setaddedAnswers((prevAnswers) => [...prevAnswers, newAnswer]); + const answerExistsInSelected = selectedAnswers.some((answer) => answer.id === newAnswer.id); + const answerExistsInAdded = addedAnswers.some((answer) => answer.id === newAnswer.id); + if (!answerExistsInSelected && !answerExistsInAdded) { + setaddedAnswers((prevAnswers) => [...prevAnswers, newAnswer]); + } }; + const handlePersonAttributeTypeChange = ({ selectedItem }: { selectedItem: PersonAttributeType }) => { setSelectedPersonAttributeType(selectedItem); }; diff --git a/src/components/interactive-builder/edit-question.modal.tsx b/src/components/interactive-builder/edit-question.modal.tsx index 0f09d128..6fa0ae86 100644 --- a/src/components/interactive-builder/edit-question.modal.tsx +++ b/src/components/interactive-builder/edit-question.modal.tsx @@ -239,7 +239,11 @@ const EditQuestionModal: React.FC = ({ setConceptAnsToLookup(''); setSelectedAnsConcept(concept); const newAnswer = { id: concept.uuid, text: concept.display }; - setaddedAnswers((prevAnswers) => [...prevAnswers, newAnswer]); + const answerExistsInSelected = selectedAnswers.some((answer) => answer.id === newAnswer.id); + const answerExistsInAdded = addedAnswers.some((answer) => answer.id === newAnswer.id); + if (!answerExistsInSelected && !answerExistsInAdded) { + setaddedAnswers((prevAnswers) => [...prevAnswers, newAnswer]); + } }; const showAddQuestion = () => { if (!addAnswer) {