From ebb7966cd101c00499862804541662638a73d4ac Mon Sep 17 00:00:00 2001 From: WindowsCrashed Date: Tue, 8 Aug 2023 14:23:39 -0300 Subject: [PATCH] fix communication between question and selecte question cards --- .../pages/assessment/NewAssessmentManual.tsx | 12 +++++++----- .../pages/assessment/components/QuestionCard.tsx | 2 +- .../assessment/components/SelectedQuestionCard.tsx | 3 +-- .../components/SelectedQuestionsSideBar.tsx | 11 ++++++----- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app/javascript/pages/assessment/NewAssessmentManual.tsx b/app/javascript/pages/assessment/NewAssessmentManual.tsx index 37b821b..0ebd371 100644 --- a/app/javascript/pages/assessment/NewAssessmentManual.tsx +++ b/app/javascript/pages/assessment/NewAssessmentManual.tsx @@ -28,7 +28,7 @@ export const NewAssessementManual = () => { const { data } = useQuery(NEW_ASSESSEMENT_DATA_QUERY) const axes = data?.axes.nodes - const [questions, setQuestions] = useState<{id: string, label: string}[]>([]) + const [questions, setQuestions] = useState<{id: string, label: string, removeHandler: Function}[]>([]) const [subjectsIds, setSubjectsIds] = useState([]) const { register, control, watch } = useForm({ @@ -52,13 +52,15 @@ export const NewAssessementManual = () => { const notSelectedAxis: Axis[] = axes.filter((axis) => !subjectsIds.includes(axis.id)) const selectedAxis: Axis[] = axes.filter((axis) => subjectsIds.includes(axis.id)) - const addQuestion = (label: string) => { + const addQuestion = (label: string, removeHandler: Function) => { const id: string = label.replace(/\s+/g, '') - setQuestions(q => [...q, { id, label }]) + if (!questions.find(q => q.id === id)) { + setQuestions(q => [...q, { id, label, removeHandler }]) + } } const removeQuestion = (id: string) => { - setQuestions(q => q.filter(i => i.id != id)) + setQuestions(q => q.filter(i => i.id !== id)) } return ( @@ -91,7 +93,7 @@ export const NewAssessementManual = () => { onAddQuestion={addQuestion} onRemoveQuestion={removeQuestion}/> - + ) diff --git a/app/javascript/pages/assessment/components/QuestionCard.tsx b/app/javascript/pages/assessment/components/QuestionCard.tsx index 7981830..d009238 100644 --- a/app/javascript/pages/assessment/components/QuestionCard.tsx +++ b/app/javascript/pages/assessment/components/QuestionCard.tsx @@ -16,7 +16,7 @@ export const QuestionCard: FC = ({ title, onAddQuestion, onRemoveQuestion setButtonState({ bg: 'bg-red-700', label: 'Remover', method: handleRemoveQuestion }) - onAddQuestion(title) + onAddQuestion(title, handleRemoveQuestion) } const handleRemoveQuestion = () => { diff --git a/app/javascript/pages/assessment/components/SelectedQuestionCard.tsx b/app/javascript/pages/assessment/components/SelectedQuestionCard.tsx index 282a336..a685688 100644 --- a/app/javascript/pages/assessment/components/SelectedQuestionCard.tsx +++ b/app/javascript/pages/assessment/components/SelectedQuestionCard.tsx @@ -1,6 +1,5 @@ import React, { FC } from 'react'; import { FaTrash } from 'react-icons/fa'; -import { Link } from 'react-router-dom'; type Props = { label: string @@ -18,7 +17,7 @@ export const SelectedQuestionCard: FC = ({ label, id, onRemoveQuestion })
diff --git a/app/javascript/pages/assessment/components/SelectedQuestionsSideBar.tsx b/app/javascript/pages/assessment/components/SelectedQuestionsSideBar.tsx index a34a1cb..e374c7a 100644 --- a/app/javascript/pages/assessment/components/SelectedQuestionsSideBar.tsx +++ b/app/javascript/pages/assessment/components/SelectedQuestionsSideBar.tsx @@ -1,13 +1,14 @@ -import React, { FC, PropsWithChildren } from "react"; +import React, { FC, } from "react"; import { SideBar } from "./SideBar"; import { SelectedQuestionCard } from "./SelectedQuestionCard"; type Props = { - questions: {id: string, label: string}[], - onRemoveQuestion: Function + questions: { + id: string, label: string, removeHandler: Function + }[] } -export const SelectedQuestionsSideBar: FC = ({ questions, onRemoveQuestion }) => { +export const SelectedQuestionsSideBar: FC = ({ questions }) => { return (

Questões Selecionadas

@@ -16,7 +17,7 @@ export const SelectedQuestionsSideBar: FC = ({ questions, onRemoveQuestio {questions.length ? questions.map(q => ) : + onRemoveQuestion={q.removeHandler}/>) :

Nenhuma questão selecionada