From 9ff014dff98a260f99e767d62619d6c6e0d9909a Mon Sep 17 00:00:00 2001 From: karimalden Date: Wed, 26 Jun 2024 16:00:54 +0300 Subject: [PATCH] end_Questions --- src/Hooks/useKeyPress.tsx | 22 +++++ src/Layout/Ui/NavBar.tsx | 5 +- src/Pages/question/AddPage.tsx | 23 ++++- src/Pages/question/EditPage.tsx | 40 +++++--- src/Pages/question/Model/Add.tsx | 14 ++- src/Pages/question/Model/Delete.tsx | 94 +++++++++++++++++++ src/Pages/question/Model/Edit.tsx | 4 +- .../question/Model/Field/ChoiceFields.tsx | 2 +- src/Pages/question/Model/Field/TextField.tsx | 3 +- src/Pages/question/Model/Malty/Add.tsx | 94 ++++++++++--------- src/Pages/question/Model/Malty/Edit.tsx | 61 ++++++------ .../question/Model/Malty/Tags/DynamicTags.tsx | 8 +- src/Pages/question/Model/Malty/Tags/Tag.tsx | 7 +- src/Pages/question/Model/Tags/DynamicTags.tsx | 3 +- src/Pages/question/Model/Tags/Tag.tsx | 2 + src/Pages/question/Model/formUtil.ts | 41 ++++++-- src/Pages/question/Page.tsx | 3 +- src/Pages/question/Table.tsx | 2 +- src/Pages/question/useTableColumns.tsx | 20 ++-- src/api/Question.ts | 4 +- src/api/ability.ts | 2 +- src/api/absence.ts | 2 +- src/api/admin.ts | 2 +- src/api/attendence.ts | 2 +- src/api/auth.ts | 2 +- src/api/branch.ts | 2 +- src/api/config.ts | 4 +- src/api/course.ts | 2 +- src/api/cycle.ts | 2 +- src/api/eduClass.ts | 2 +- src/api/exam.ts | 2 +- src/api/examTypes.ts | 2 +- src/api/example.ts | 2 +- src/api/helper/useAddMutation.ts | 4 +- src/api/helper/useAxios.ts | 8 +- src/api/helper/useDeleteMutation.ts | 4 +- src/api/helper/useUpdateMutation.ts | 4 +- src/api/lateArrival.ts | 2 +- src/api/lesson.ts | 2 +- src/api/mark.ts | 2 +- src/api/note.ts | 2 +- src/api/payment.ts | 2 +- src/api/presence.ts | 2 +- src/api/registrationRecord.ts | 2 +- src/api/role.ts | 2 +- src/api/semantics.ts | 2 +- src/api/session.ts | 2 +- src/api/sessionContent.ts | 2 +- src/api/student.ts | 2 +- src/api/subject.ts | 2 +- src/api/summary.ts | 2 +- src/api/tags.ts | 2 +- src/api/teacher.ts | 2 +- src/api/term.ts | 2 +- src/api/unit.ts | 2 +- src/enums/Model.ts | 4 + src/translate/ar.json | 9 +- src/types/Item.ts | 2 +- 58 files changed, 385 insertions(+), 166 deletions(-) create mode 100644 src/Hooks/useKeyPress.tsx create mode 100644 src/Pages/question/Model/Delete.tsx diff --git a/src/Hooks/useKeyPress.tsx b/src/Hooks/useKeyPress.tsx new file mode 100644 index 0000000..64f6bea --- /dev/null +++ b/src/Hooks/useKeyPress.tsx @@ -0,0 +1,22 @@ +import { useEffect } from 'react'; + +type ModifierKey = 'ctrlKey' | 'shiftKey' | 'altKey' | 'metaKey'; + +const useKeyPress = (targetKey: string, modifierKey: ModifierKey, callback: () => void) => { + useEffect(() => { + const handleKeyDown = (event: KeyboardEvent) => { + if (event[modifierKey] && event.key === targetKey) { + event.preventDefault(); + callback(); + } + }; + + document.addEventListener('keydown', handleKeyDown); + + return () => { + document.removeEventListener('keydown', handleKeyDown); + }; + }, [targetKey, modifierKey, callback]); +}; + +export default useKeyPress; diff --git a/src/Layout/Ui/NavBar.tsx b/src/Layout/Ui/NavBar.tsx index d96d1dd..e4d7474 100644 --- a/src/Layout/Ui/NavBar.tsx +++ b/src/Layout/Ui/NavBar.tsx @@ -24,7 +24,8 @@ const NavBar = () => { const translateArray = translateOptions(search_array, t); const { course_id } = useParams(); - + console.log(location); + const navigate = useNavigate(); const handelNavigate = () => { @@ -36,7 +37,7 @@ const NavBar = () => { if (location.pathname === "/") { return; } - navigate(-1); + navigate(-1); }; const { handel_open_model } = useModalHandler(); diff --git a/src/Pages/question/AddPage.tsx b/src/Pages/question/AddPage.tsx index f92aca5..4a13877 100644 --- a/src/Pages/question/AddPage.tsx +++ b/src/Pages/question/AddPage.tsx @@ -2,7 +2,7 @@ import React, { useEffect } from "react"; import { Modal, Spin } from "antd"; import FormikForm from "../../Layout/Dashboard/FormikFormModel"; import ModelBody from "./Model/Add"; -import { getInitialValues, getValidationSchema ,getInitialValuesBase, getValidationSchemaBase} from "./Model/formUtil"; +import { getInitialValues, getValidationSchema ,getInitialValuesBase, getValidationSchemaBase, processTags} from "./Model/formUtil"; import { useAddQuestion } from "../../api/Question"; import { useTranslation } from "react-i18next"; import { useNavigate, useParams } from "react-router-dom"; @@ -13,36 +13,43 @@ import Header from "../../Components/exercise/Header"; import { Question } from "../../types/Item"; import BaseForm from './Model/Malty/Add' import Form from './Model/Add' +import { toast } from "react-toastify"; const AddPage: React.FC = () => { const { mutate, isSuccess, isLoading ,mutateAsync} = useAddQuestion(); - const {object_to_edit} = useObjectToEdit() + const {object_to_edit,set_Tags_search} = useObjectToEdit() const {subject_id} = useParams() const {isBseQuestion,set_isBseQuestion} = useObjectToEdit() const handleSubmit = (values: Question) => { const DataToSend = structuredClone(values); + console.log(DataToSend,"DataToSend"); + set_Tags_search(null) + if(isBseQuestion){ const newBseQuestion = { "subject_id" : subject_id, "content" : DataToSend?.content, "image": DataToSend?.image ?? "", - "max_mark" : DataToSend?.max_mark, - "min_mark_to_pass" : DataToSend?.min_mark_to_pass, "isBase" : 1, } mutateAsync(newBseQuestion).then((data)=>{ const newBseQuestionId = (data as any )?.data?.id ; const Questions = DataToSend?.Questions; + + Questions?.map((item:Question)=>{ + const tags = processTags(item) + mutate({ ...item, parent_id:newBseQuestionId, "subject_id" : subject_id, + tags }) }) @@ -51,12 +58,16 @@ const AddPage: React.FC = () => { }) }else{ - mutate({ ...values, subject_id:subject_id }); + const tags = processTags(DataToSend) + console.log(tags); + + mutate({ ...values, subject_id:subject_id , tags }); } }; const navigate = useNavigate() useEffect(() => { if(isSuccess){ + toast.success(t("validation.the_possess_done_successful")) navigate(-1) } @@ -132,3 +143,5 @@ const AddPage: React.FC = () => { }; export default AddPage; + + diff --git a/src/Pages/question/EditPage.tsx b/src/Pages/question/EditPage.tsx index 38a47a9..fa8a300 100644 --- a/src/Pages/question/EditPage.tsx +++ b/src/Pages/question/EditPage.tsx @@ -1,7 +1,7 @@ import React, { useEffect } from "react"; import { Modal, Spin } from "antd"; import FormikForm from "../../Layout/Dashboard/FormikFormModel"; -import { getInitialValues, getValidationSchema ,getInitialValuesBase, getValidationSchemaBase} from "./Model/formUtil"; +import { getInitialValues, getValidationSchema ,getInitialValuesBase, getValidationSchemaBase, processTags} from "./Model/formUtil"; import { useAddQuestion, useGetAllQuestion, useUpdateQuestion } from "../../api/Question"; import { useQueryClient } from "react-query"; import { useTranslation } from "react-i18next"; @@ -14,18 +14,19 @@ import Header from "../../Components/exercise/Header"; import Form from './Model/Edit' import BaseForm from './Model/Malty/Edit' import { Question } from "../../types/Item"; +import { toast } from "react-toastify"; const EditPage: React.FC = () => { const {question_id,subject_id} = useParams() - const {isBseQuestion,set_isBseQuestion} = useObjectToEdit() + const {isBseQuestion,set_isBseQuestion,set_Tags_search} = useObjectToEdit() const { mutate, isSuccess, isLoading } = useUpdateQuestion(); const { mutate:AddQuestion} = useAddQuestion(); const {data,isLoading:dataLoading}= useGetAllQuestion({show:question_id}) - const {data:Questions,isLoading:QuestionsDataLoading}= useGetAllQuestion({parent_id:question_id}) + const {data:Questions,isLoading:QuestionsDataLoading}= useGetAllQuestion({questionParentId:question_id}) const object_to_edit = {...data?.data,Questions:Questions?.data } ; @@ -40,41 +41,52 @@ const EditPage: React.FC = () => { const handleSubmit = (values: any) => { const DataToSend = structuredClone(values); - - console.log(isBseQuestion); - - if(isBseQuestion){ + set_Tags_search(null) + console.log(DataToSend); + + if(isBseQuestion){ console.log(1); const UpdateBseQuestion = { "id":DataToSend?.id, "content" : DataToSend?.content, "image": DataToSend?.image ?? "", - "max_mark" : DataToSend?.max_mark, - "min_mark_to_pass" : DataToSend?.min_mark_to_pass, } if( typeof UpdateBseQuestion?.image === "string"){ delete UpdateBseQuestion["image"] } mutate(UpdateBseQuestion) + const Questions = DataToSend?.Questions; - + Questions?.map((item:Question)=>{ + console.log(item); + const itemToSend = structuredClone(item); const keysToRemove = ['image', 'answer_image']; const updatedObject = removeStringKeys(itemToSend, keysToRemove); console.log(updatedObject,"updatedObject"); if(updatedObject?.id){ + const tags = processTags(updatedObject) + console.log(tags); + mutate({ ...updatedObject, + tags }) }else{ + + const tags = processTags(updatedObject) + console.log(tags); + + AddQuestion({ ...updatedObject, parent_id:UpdateBseQuestion?.id, - subject_id:subject_id + subject_id:subject_id, + tags }) } @@ -86,7 +98,10 @@ const EditPage: React.FC = () => { const keysToRemove = ['image', 'answer_image']; const updatedObject = removeStringKeys(DataToSend, keysToRemove); - mutate({ ...updatedObject }); + delete updatedObject["parent_id"]; + const tags = processTags(updatedObject) + console.log(tags); + mutate({ ...updatedObject,tags }); } }; @@ -99,6 +114,7 @@ const EditPage: React.FC = () => { useEffect(() => { if(isSuccess){ + toast.success(t("validation.the_possess_done_successful")) navigate(-1) } }, [isSuccess]) diff --git a/src/Pages/question/Model/Add.tsx b/src/Pages/question/Model/Add.tsx index 0882d15..6d7dbed 100644 --- a/src/Pages/question/Model/Add.tsx +++ b/src/Pages/question/Model/Add.tsx @@ -10,6 +10,7 @@ import { Choice } from "../../../types/Item"; import { useTranslation } from "react-i18next"; import DynamicTags from "./Tags/DynamicTags"; import { useGetAllQuestion } from "../../../api/Question"; +import useKeyPress from "../../../Hooks/useKeyPress"; const Form = () => { const formik = useFormikContext(); @@ -32,11 +33,18 @@ const Form = () => { { answer:null, answer_image:null, - isCorrect:0 + isCorrect:0, + }]) } const [t] = useTranslation() + + + useKeyPress('q', 'ctrlKey', handleAddChoice); + const selectedInput = document.getElementById("choice_1") + console.log(selectedInput,"selectedInput"); + return (
@@ -55,12 +63,12 @@ const Form = () => { { (((formik?.values as any)?.QuestionOptions as Choice[])||[]) .map((item:Choice,index:number)=>{ - return + return } ) }

- {t("header.add_new_choice")} + {t("header.add_new_choice")}

diff --git a/src/Pages/question/Model/Delete.tsx b/src/Pages/question/Model/Delete.tsx new file mode 100644 index 0000000..b9c413b --- /dev/null +++ b/src/Pages/question/Model/Delete.tsx @@ -0,0 +1,94 @@ +import React, { useEffect, useState } from "react"; +import { Input, Modal, Spin } from "antd"; +import { useModalState } from "../../../zustand/Modal"; +import { ModalEnum } from "../../../enums/Model"; +import { useObjectToEdit } from "../../../zustand/ObjectToEditState"; +import { useDeleteQuestion } from "../../../api/Question"; +import { useTranslation } from "react-i18next"; + +const ModalForm: React.FC = () => { + const { isOpen, setIsOpen } = useModalState((state) => state); + const [inputValue, setInputValue] = useState(""); + + const { mutate, isLoading, isSuccess } = useDeleteQuestion(); + const { object_to_edit, set_object_to_edit } = useObjectToEdit(); + + useEffect(() => { + if (isSuccess) { + setIsOpen(""); + setInputValue(""); + } + }, [isSuccess, setIsOpen]); + + const handleSubmit = () => { + mutate({ + id: Number(object_to_edit?.id), + }); + }; + + const handleCancel = () => { + setInputValue(""); + setIsOpen(""); + set_object_to_edit({}); + }; + + const handleChange = (e: React.ChangeEvent) => { + // Step 2: Handle changes to the input field + setInputValue(e.target.value); + }; + const [t] = useTranslation(); + + return ( + <> + +
+ {t("practical.delete")} ({object_to_edit?.max_mark}){" "} +
+
+
+ + + +
+ +
+
{t("practical.cancel")}
+ +
+
+
+ + ); +}; + +export default ModalForm; diff --git a/src/Pages/question/Model/Edit.tsx b/src/Pages/question/Model/Edit.tsx index dba5338..1244a5a 100644 --- a/src/Pages/question/Model/Edit.tsx +++ b/src/Pages/question/Model/Edit.tsx @@ -12,7 +12,7 @@ import DynamicTags from "./Tags/DynamicTags"; import { useGetAllQuestion } from "../../../api/Question"; const Form = () => { - const formik = useFormikContext(); + const formik = useFormikContext(); const { isOpen } = useModalState((state) => state); // const {data} = useGetAllQuestion(); @@ -27,6 +27,8 @@ const Form = () => { const handleAddChoice = () => { + + formik.setFieldValue('QuestionOptions', [...(formik?.values as any)?.QuestionOptions as Choice[], { diff --git a/src/Pages/question/Model/Field/ChoiceFields.tsx b/src/Pages/question/Model/Field/ChoiceFields.tsx index c3bef6e..a44d61f 100644 --- a/src/Pages/question/Model/Field/ChoiceFields.tsx +++ b/src/Pages/question/Model/Field/ChoiceFields.tsx @@ -16,7 +16,7 @@ const ChoiceFields = ({index,data}:{index:number , data :Choice }) => {
- + diff --git a/src/Pages/question/Model/Field/TextField.tsx b/src/Pages/question/Model/Field/TextField.tsx index e2c3ca6..5d65fb6 100644 --- a/src/Pages/question/Model/Field/TextField.tsx +++ b/src/Pages/question/Model/Field/TextField.tsx @@ -15,6 +15,7 @@ const TextField = ({ props, no_label, label_icon, + id, className }: any) => { const newName = `QuestionOptions[${name}].answer` @@ -60,7 +61,7 @@ const TextField = ({ maxLength={1000} onChange={onChange || TextFilehandleChange} style={{height:120}} - + id={id} /> diff --git a/src/Pages/question/Model/Malty/Add.tsx b/src/Pages/question/Model/Malty/Add.tsx index 673f784..304e8d9 100644 --- a/src/Pages/question/Model/Malty/Add.tsx +++ b/src/Pages/question/Model/Malty/Add.tsx @@ -25,7 +25,8 @@ const Form = () => { }, [isOpen]); // console.log(formik?.errors); - + console.log(formik?.values?.Questions,"formik?.values?.Questions"); + const handleAddChoice = (parent_index:number) => { console.log(parent_index); @@ -64,57 +65,58 @@ const Form = () => { return ( -
+
- - - -
- - + + + + {/*
+ + -
-
+
*/} +
+ +
+ +
+
+ + { + (((formik?.values as any)?.Questions)||[])?.map((item:Choice,parent_index:number)=>{ + + return ( +
+ +
-
-
- -
- - { - (((formik?.values as any)?.Questions as Choice[])||[]) .map((item:Choice,parent_index:number)=>{ - - return ( -
- -
- -
- - { - (((formik?.values as any)?.Questions?.[parent_index]?.QuestionOptions as Choice[])||[]) .map((item:Choice,index:number)=>{ - - return - } - ) - } -

- handleAddChoice(parent_index)} size={23} /> {t("header.add_new_choice")} -

- - -
- ) - } + +
+ + { + (((formik?.values as any)?.Questions?.[parent_index]?.QuestionOptions)||[]) .map((item:Choice,index:number)=>{ + + return + } + ) + } +

+ handleAddChoice(parent_index)} size={23} /> {t("header.add_new_choice")} +

+ + +
) - } - -

- {t("header.add_new_question")} -

- + } + ) + } + +

+ {t("header.add_new_question")} +

+ ); }; diff --git a/src/Pages/question/Model/Malty/Edit.tsx b/src/Pages/question/Model/Malty/Edit.tsx index d7a1359..dd26b22 100644 --- a/src/Pages/question/Model/Malty/Edit.tsx +++ b/src/Pages/question/Model/Malty/Edit.tsx @@ -28,37 +28,38 @@ const Form = () => { const [t] = useTranslation() - const handleAddChoice = (parent_index:number) => { - console.log(parent_index); - - formik.setFieldValue(`Questions.[${parent_index}].QuestionOptions`, [...(formik?.values as any)?.Questions?.[parent_index].QuestionOptions, - - { - answer:null, - answer_image:null, - isCorrect:0 - }]) - } - - - const handleAddQuestion = () => { - formik.setFieldValue('Questions', [...(formik?.values as any)?.Questions, - + + const handleAddChoice = (parent_index:number) => { + console.log(parent_index); + console.log(formik?.values?.Questions); + formik.setFieldValue(`Questions.[${parent_index}].QuestionOptions`, [...(formik?.values as any)?.Questions?.[parent_index].QuestionOptions as Choice[], + { - content: "", - image: "", - parent: '', - isBase: 0, - max_mark: 1, - min_mark_to_pass: 1, - QuestionOptions: [{ answer: null, answer_image: null, isCorrect: 0 }], - tags: [] + answer:null, + answer_image:null, + isCorrect:0 }]) - - const max_mark = formik?.values?.max_mark + 1 - - formik.setFieldValue('max_mark', max_mark) } + + + const handleAddQuestion = () => { + formik.setFieldValue('Questions', [...(formik?.values as any)?.Questions as Choice[], + + { + content: "", + image: "", + parent: '', + isBase: 0, + max_mark: 1, + min_mark_to_pass: 1, + QuestionOptions: [{ answer: null, answer_image: null, isCorrect: 0 }], + tags: [] + }]) + + const max_mark = formik?.values?.max_mark + 1 + + formik.setFieldValue('max_mark', max_mark) +} console.log(formik?.values); return ( @@ -68,11 +69,11 @@ const Form = () => { -
+ {/*
-
+
*/}
diff --git a/src/Pages/question/Model/Malty/Tags/DynamicTags.tsx b/src/Pages/question/Model/Malty/Tags/DynamicTags.tsx index 375fe4c..f620183 100644 --- a/src/Pages/question/Model/Malty/Tags/DynamicTags.tsx +++ b/src/Pages/question/Model/Malty/Tags/DynamicTags.tsx @@ -20,7 +20,8 @@ const DynamicTags = ({parent_index}:{parent_index:number}) => { const handleAddChoice = () => { const length = formik?.values?.Questions?.[parent_index]?.tags.length; const lastElement = formik?.values?.Questions?.[parent_index]?.tags[length - 1]?.name; - + set_Tags_search(null) + if(lastElement !== ""){ formik.setFieldValue(`Questions.[${parent_index}].tags`, [...(formik?.values as any)?.Questions?.[parent_index]?.tags as any[], @@ -40,7 +41,7 @@ const DynamicTags = ({parent_index}:{parent_index:number}) => { const handleChoice = (item: any) => { - const length = formik.values.tags.length; + const length = formik?.values?.Questions?.[parent_index]?.tags?.length; console.log(currentTag); formik.setFieldValue(`Questions.[${parent_index}].tags[${currentTag}]`, {...item,key:length}); @@ -53,7 +54,7 @@ const handleChoice = (item: any) => { return (
- {formik?.values?.tags?.length < 1 && + {formik?.values?.Questions?.[parent_index]?.tags?.length < 1 &&

{t("header.add_tag")} @@ -79,6 +80,7 @@ return ( {formik?.values?.Questions?.[parent_index]?.tags?.length > 0 &&

+

diff --git a/src/Pages/question/Model/Malty/Tags/Tag.tsx b/src/Pages/question/Model/Malty/Tags/Tag.tsx index 09ed75f..92854b2 100644 --- a/src/Pages/question/Model/Malty/Tags/Tag.tsx +++ b/src/Pages/question/Model/Malty/Tags/Tag.tsx @@ -9,7 +9,8 @@ const Tag = ({ data, index,parent_index }: { data: any, index: number,parent_ind const DEBOUNCE_DELAY = 500; const formik = useFormikContext(); const { set_Tags_search ,set_currentTag,set_current_parent_index} = useObjectToEdit(); - + console.log(formik?.values?.Questions); + useEffect(() => { if (inputRef.current) { inputRef.current.style.width = `${(formik?.values?.Questions?.[parent_index]?.tags[index]?.name?.length + 1) * 8}px`; @@ -18,7 +19,9 @@ const Tag = ({ data, index,parent_index }: { data: any, index: number,parent_ind const handleEditInputChange = (e: React.ChangeEvent) => { // console.log(e.target.value); - formik.setFieldValue(`Questions.[${parent_index}].tags[${index}].name`, e.target.value); + + + formik.setFieldValue(`Questions.[${parent_index}].tags[${index}]`, {key:parent_index, name :e.target.value , id:`${parent_index}_key`}); set_currentTag(index) set_current_parent_index(parent_index) if (timeoutRef.current) { diff --git a/src/Pages/question/Model/Tags/DynamicTags.tsx b/src/Pages/question/Model/Tags/DynamicTags.tsx index b2664c0..5d5087f 100644 --- a/src/Pages/question/Model/Tags/DynamicTags.tsx +++ b/src/Pages/question/Model/Tags/DynamicTags.tsx @@ -15,12 +15,13 @@ const DynamicTags = () => { name : Tags_search }) const suggests = data?.data + console.log(Tags_search); const handleAddChoice = () => { const length = formik?.values?.tags.length; const lastElement = formik?.values?.tags[length - 1]?.name; - + set_Tags_search(null) if(lastElement !== ""){ formik.setFieldValue('tags', [...(formik?.values as any)?.tags as any[], diff --git a/src/Pages/question/Model/Tags/Tag.tsx b/src/Pages/question/Model/Tags/Tag.tsx index 09de022..b51c22d 100644 --- a/src/Pages/question/Model/Tags/Tag.tsx +++ b/src/Pages/question/Model/Tags/Tag.tsx @@ -19,6 +19,8 @@ const Tag = ({ data, index }: { data: any, index: number }) => { // console.log(e.target.value); formik.setFieldValue(`tags[${index}].name`, e.target.value); // set_Tags_search(e.target.value) + formik.setFieldValue(`tags.[${index}]`, {key:index, name :e.target.value , id:`${index}_key`}); + set_currentTag(index) if (timeoutRef.current) { clearTimeout(timeoutRef.current); diff --git a/src/Pages/question/Model/formUtil.ts b/src/Pages/question/Model/formUtil.ts index 3afca5c..d5783ae 100644 --- a/src/Pages/question/Model/formUtil.ts +++ b/src/Pages/question/Model/formUtil.ts @@ -13,9 +13,7 @@ export const getInitialValues = (objectToEdit: Question): any => { image: objectToEdit?.image ?? "", subject_id: objectToEdit?.subject_id ?? '', isBase: objectToEdit?.isBase, - max_mark: 1, - min_mark_to_pass: 1, - parent: objectToEdit?.parent ?? '', + parent_id: objectToEdit?.parent_id ?? '', QuestionOptions: objectToEdit?.QuestionOptions ?? [], tags: tags ?? [], }; @@ -26,8 +24,6 @@ export const getValidationSchema = () => { return Yup.object().shape({ image: Yup.string().nullable(), content: Yup.string().required("validation.required"), - max_mark: Yup.number().required("validation.required").min(Yup.ref("min_mark_to_pass"),"validation.max_mark_must_be_greater_than_min_mark_to_pass"), - min_mark_to_pass: Yup.number().required("validation.required"), QuestionOptions: Yup.array().of( Yup.object().shape({ answer: Yup.string().required("validation.required"), @@ -46,7 +42,20 @@ export const getInitialValuesBase = (objectToEdit: Question): any => { }); console.log(objectToEdit); - const questions = objectToEdit?.Questions ?? []; + const newQuestions = objectToEdit?.Questions?.map((item: any) => { + const tags = item?.tags?.map((tag: any) => ({ + id: tag?.id, + name: tag?.name, + key: `${tag?.id}_key_${tag?.name}` + })); + + return { + ...item, + tags + }; + }); + + const questions = newQuestions ?? []; return { @@ -57,8 +66,7 @@ export const getInitialValuesBase = (objectToEdit: Question): any => { isBase: objectToEdit?.isBase, max_mark: questions.length ?? 1, min_mark_to_pass: 1, - parent: objectToEdit?.parent ?? '', - tags: tags ?? [], + parent_id: objectToEdit?.parent_id ?? '', Questions: questions, }; }; @@ -88,3 +96,20 @@ export const getValidationSchemaBase = () => { }); }; + + +export function processTags(DataToSend: any) { + const oldTags = DataToSend?.tags?.map((item: any, index: number) => { + if (typeof item?.id === "number") { + return item?.id; + } + }).filter((item:any) => item !== undefined); + + const newTags = DataToSend?.tags?.map((item: any, index: number) => { + if (typeof item?.id === "string") { + return { name: item?.name }; + } + }).filter((item:any) => item !== undefined); + + return { new:newTags, old:oldTags }; +} diff --git a/src/Pages/question/Page.tsx b/src/Pages/question/Page.tsx index 1c52e71..38a6762 100644 --- a/src/Pages/question/Page.tsx +++ b/src/Pages/question/Page.tsx @@ -1,7 +1,7 @@ import { useTranslation } from "react-i18next"; import { lazy, Suspense } from 'react'; import { Spin } from "antd"; - +import DeleteModel from './Model/Delete' const Table = lazy(() => import('./Table')); const TableHeader = () => { @@ -17,6 +17,7 @@ const TableHeader = () => { + ); diff --git a/src/Pages/question/Table.tsx b/src/Pages/question/Table.tsx index cd26b77..8d53131 100644 --- a/src/Pages/question/Table.tsx +++ b/src/Pages/question/Table.tsx @@ -7,7 +7,7 @@ import { ParamsEnum } from "../../enums/params"; const App: React.FC = () => { const {subject_id} = useParams() - const response = useGetAllQuestion({ subject_id:subject_id, pagination: true}); + const response = useGetAllQuestion({ subject_id:subject_id, pagination: true }); return ; }; diff --git a/src/Pages/question/useTableColumns.tsx b/src/Pages/question/useTableColumns.tsx index 774e0bd..3e881cb 100644 --- a/src/Pages/question/useTableColumns.tsx +++ b/src/Pages/question/useTableColumns.tsx @@ -14,15 +14,18 @@ import { ABILITIES_ENUM, ABILITIES_VALUES_ENUM } from "../../enums/abilities"; import { formatNumber } from "../../utils/formatNumber"; import { BsEyeFill } from "react-icons/bs"; import { useNavigate } from "react-router-dom"; +import { useModalState } from "../../zustand/Modal"; export const useColumns = () => { const { handel_open_model } = useModalHandler(); const { set_object_to_edit } = useObjectToEdit((state) => state); const navigate = useNavigate() + const { setIsOpen } = useModalState((state) => state); - const handelShow = (record: any) => { - navigate(`${ABILITIES_ENUM.QUESTION}/${record?.id}`); + const handelAdd = () => { + set_object_to_edit({}) + navigate(`${ABILITIES_ENUM?.QUESTION}/add`) }; const can_edit_Question = hasAbility( @@ -43,6 +46,8 @@ export const useColumns = () => { ); const handelDelete = (data: any) => { set_object_to_edit(data); + setIsOpen(ModalEnum?.QUESTION_DELETE); + }; const handleEdit = (record: any) => { @@ -67,19 +72,18 @@ export const useColumns = () => { align: "center", render: (text, record) => record?.content, }, - { - title: t("columns.description"), - dataIndex: "description", - key: "description", + title: t("columns.isBase"), + dataIndex: "isBase", + key: "isBase", align: "center", - render: (text, record) => record?.max_mark, + render: (text, record) => record?.isBase === 1 ? t("practical.yes") : t ("practical.no"), }, { title: can_add_Question ? (