diff --git a/src/Layout/Dashboard/FormikFormModel.tsx b/src/Layout/Dashboard/FormikFormModel.tsx index 05d7257..091a230 100644 --- a/src/Layout/Dashboard/FormikFormModel.tsx +++ b/src/Layout/Dashboard/FormikFormModel.tsx @@ -8,7 +8,7 @@ interface FormValues { } interface FormikFormProps { - handleSubmit: (values: any) => void; + handleSubmit: any; initialValues: FormValues; validationSchema: any; title?: string; @@ -30,6 +30,7 @@ const FormikFormModel: React.FC = ({ initialValues={initialValues} validationSchema={validationSchema} onSubmit={handleSubmit} + > {(formik) => { useEffect(() => { diff --git a/src/Pages/question/AddPage.tsx b/src/Pages/question/AddPage.tsx index 1d454da..a8e4be4 100644 --- a/src/Pages/question/AddPage.tsx +++ b/src/Pages/question/AddPage.tsx @@ -14,69 +14,71 @@ import { Question } from "../../types/Item"; import BaseForm from './Model/Malty/Add' import Form from './Model/Add' import { toast } from "react-toastify"; +import AcceptModal from "./Model/AcceptModal"; +import { useModalState } from "../../zustand/Modal"; +import { ModalEnum } from "../../enums/Model"; const AddPage: React.FC = () => { const { mutate, isSuccess, isLoading ,mutateAsync} = useAddQuestion(); - const {object_to_edit,set_Tags_search,set_object_to_edit} = useObjectToEdit() + const {object_to_edit,set_Tags_search,set_object_to_edit,set_Success} = useObjectToEdit() const {subject_id} = useParams() const {isBseQuestion,set_isBseQuestion} = useObjectToEdit() + const { setIsOpen } = useModalState((state) => state); - const handleSubmit = (values: Question) => { + + + const handleSubmit = (values: any, { resetForm }: { resetForm: () => void }) => { 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 ?? "", - "isBase" : 1, - } + console.log(DataToSend, "DataToSend"); + set_Tags_search(null); + + if (isBseQuestion) { + const newBseQuestion = { + "subject_id": subject_id, + "content": DataToSend?.content, + "image": DataToSend?.image ?? "", + "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 + }); + }); + console.log(newBseQuestionId, "newBseQuestionId"); + - 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 - }) - - }) - console.log(newBseQuestionId,"newBseQuestionId"); - - - }) - }else{ - const tags = processTags(DataToSend) - console.log(tags); - - mutate({ ...values, subject_id:subject_id , tags }); - } + }); + } else { + const tags = processTags(DataToSend); + mutate({ ...values, subject_id: subject_id, tags }) + } }; + const navigate = useNavigate() useEffect(() => { if(isSuccess){ toast.success(t("validation.the_possess_done_successful")) - // navigate(-1) set_object_to_edit(null) - + set_Success(true) } }, [isSuccess]) const handleCancel = () => { - navigate(-1) + setIsOpen(ModalEnum?.QUESTION_ACCEPT); }; const [t] = useTranslation(); @@ -108,7 +110,7 @@ const AddPage: React.FC = () => { - + ); } @@ -119,6 +121,7 @@ const AddPage: React.FC = () => { handleSubmit={handleSubmit} initialValues={getInitialValues(object_to_edit)} validationSchema={getValidationSchema} + >
@@ -139,7 +142,7 @@ const AddPage: React.FC = () => {
- + ); }; diff --git a/src/Pages/question/EditPage.tsx b/src/Pages/question/EditPage.tsx index d9b37d7..321e05f 100644 --- a/src/Pages/question/EditPage.tsx +++ b/src/Pages/question/EditPage.tsx @@ -60,49 +60,68 @@ const EditPage: React.FC = () => { mutate(UpdateBseQuestion) const Questions = DataToSend?.Questions; - + console.log(Questions,"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){ + console.log(updatedObject,"updatedObject"); + const tags = processTags(updatedObject) - console.log(tags); - + const oldQuestionOptions = [] as any; + const newQuestionOptions = [] as any; + + updatedObject?.QuestionOptions?.forEach((item:any) => { + if (item?.id) { + oldQuestionOptions.push(item); + } else { + newQuestionOptions.push(item); + } + }); + const QuestionOptions = { + old: oldQuestionOptions, + new: newQuestionOptions + }; + mutate({ ...updatedObject, + QuestionOptions, tags }) - }else{ - - const tags = processTags(updatedObject) - console.log(tags); - - - AddQuestion({ - ...updatedObject, - parent_id:UpdateBseQuestion?.id, - subject_id:subject_id, - tags - - }) - } + }) }else{ - console.log(2); - + const keysToRemove = ['image', 'answer_image']; const updatedObject = removeStringKeys(DataToSend, keysToRemove); delete updatedObject["parent_id"]; const tags = processTags(updatedObject) - console.log(tags); - mutate({ ...updatedObject,tags }); + console.log(updatedObject,"updatedObject"); + const oldQuestionOptions = [] as any; + const newQuestionOptions = [] as any; + + updatedObject?.QuestionOptions?.forEach((item:any) => { + if (item?.id) { + oldQuestionOptions.push(item); + } else { + newQuestionOptions.push(item); + } + }); + + const QuestionOptions = { + old: oldQuestionOptions, + new: newQuestionOptions + }; + console.log(QuestionOptions,"QuestionOptions"); + + + mutate({ ...updatedObject,QuestionOptions,tags }); } }; @@ -116,7 +135,7 @@ const EditPage: React.FC = () => { useEffect(() => { if(isSuccess){ toast.success(t("validation.the_possess_done_successful")) - navigate(-1) + // navigate(-1) } }, [isSuccess]) diff --git a/src/Pages/question/Model/AcceptModal.tsx b/src/Pages/question/Model/AcceptModal.tsx new file mode 100644 index 0000000..37a7828 --- /dev/null +++ b/src/Pages/question/Model/AcceptModal.tsx @@ -0,0 +1,66 @@ +import React, { useEffect, useState } from "react"; +import { Input, Modal, Spin } from "antd"; +import { useModalState } from "../../../zustand/Modal"; +import { ModalEnum } from "../../../enums/Model"; +import { useTranslation } from "react-i18next"; +import { useNavigate } from "react-router-dom"; +import { useObjectToEdit } from "../../../zustand/ObjectToEditState"; + +const AcceptModal: React.FC = () => { + const { isOpen, setIsOpen } = useModalState((state) => state); + const navigate = useNavigate() + + + const handleSubmit = () => { + console.log("Handle submit clicked"); + navigate(-1) + }; + + const handleCancel = () => { + setIsOpen(""); + + }; + + + const [t] = useTranslation(); + return ( + <> + +
+ {" "} + {t("practical.accept_back")} +
+ +
+
+ + +
+ +
+
{t("practical.cancel")}
+
+ {t("practical.accept")} + +
+
+
+
+ + ); +}; + +export default AcceptModal; diff --git a/src/Pages/question/Model/Add.tsx b/src/Pages/question/Model/Add.tsx index f489864..a977876 100644 --- a/src/Pages/question/Model/Add.tsx +++ b/src/Pages/question/Model/Add.tsx @@ -11,22 +11,23 @@ import { useTranslation } from "react-i18next"; import DynamicTags from "./Tags/DynamicTags"; import { useGetAllQuestion } from "../../../api/Question"; import useKeyPress from "../../../Hooks/useKeyPress"; +import { useObjectToEdit } from "../../../zustand/ObjectToEditState"; const Form = () => { const formik = useFormikContext(); - const { isOpen } = useModalState((state) => state); - // const {data} = useGetAllQuestion(); - - useEffect(() => { - if (isOpen === "") { - formik.setErrors({}); - formik.resetForm(); - } - }, [isOpen]); + const{set_Success,Success} = useObjectToEdit() + + useEffect(() => { + if (Success) { + formik.setErrors({}); + formik.resetForm({ values: {} }); + set_Success(false) + } + }, [Success]); + + + - // console.log(formik?.errors); - - const handleAddChoice = () => { console.log(formik?.values?.QuestionOptions?.length); diff --git a/src/Pages/question/Model/Field/CheckboxField.tsx b/src/Pages/question/Model/Field/CheckboxField.tsx index 63b5a64..a9eab75 100644 --- a/src/Pages/question/Model/Field/CheckboxField.tsx +++ b/src/Pages/question/Model/Field/CheckboxField.tsx @@ -14,11 +14,21 @@ const CheckboxField = ({ }: any) => { const formik = useFormikContext() const [t] = useTranslation() - const CheckboxhandleChange = (value: any) => { - console.log(value?.target?.checked); + const CheckboxhandleChange = (value: any, index: number) => { + + const allAreZero = formik?.values?.QuestionOptions?.some((item: any) => item.isCorrect === 1); + if (allAreZero) { + + formik?.values.QuestionOptions.forEach((item: any,index:number) => { + + formik.setFieldValue(`QuestionOptions[${index}].isCorrect`, 0); + }); + } + formik.setFieldValue(`QuestionOptions[${name}].isCorrect`, value?.target?.checked ? 1 : 0); - }; + + }; return (
{ const formik = useFormikContext(); const { isOpen } = useModalState((state) => state); // const {data} = useGetAllQuestion(); - + const{set_Success,Success} = useObjectToEdit() + useEffect(() => { - if (isOpen === "") { + if (Success) { + console.log(1); + formik.setErrors({}); - formik.resetForm(); + formik.resetForm({ values: {} }); + set_Success(false) } - }, [isOpen]); + }, [Success]); // console.log(formik?.errors); console.log(formik?.values?.Questions,"formik?.values?.Questions"); diff --git a/src/Pages/question/Model/Malty/ChoiceField/CheckboxField.tsx b/src/Pages/question/Model/Malty/ChoiceField/CheckboxField.tsx index 630ecb6..9844f8d 100644 --- a/src/Pages/question/Model/Malty/ChoiceField/CheckboxField.tsx +++ b/src/Pages/question/Model/Malty/ChoiceField/CheckboxField.tsx @@ -18,7 +18,20 @@ const CheckboxField = ({ const CheckboxhandleChange = (value: any) => { console.log(value?.target?.checked); + + + const allAreZero = formik?.values?.Questions?.[parent_index]?.QuestionOptions?.some((item: any) => item.isCorrect === 1); + if (allAreZero) { + + formik?.values?.Questions?.[parent_index]?.QuestionOptions.forEach((item: any,index:number) => { + + formik.setFieldValue(`Questions[${parent_index}].QuestionOptions[${index}].isCorrect`, 0); + }); + } + formik.setFieldValue(`Questions[${parent_index}].QuestionOptions[${name}].isCorrect`, value?.target?.checked ? 1 : 0); + + }; return (
diff --git a/src/Styles/App/classNames.scss b/src/Styles/App/classNames.scss index 8863c3a..b031070 100644 --- a/src/Styles/App/classNames.scss +++ b/src/Styles/App/classNames.scss @@ -268,4 +268,9 @@ button:disabled { } .relative{ position: relative; +} +.h1{ + text-align: center; + font-weight: bold; + } \ No newline at end of file diff --git a/src/enums/Model.ts b/src/enums/Model.ts index 45723fa..bcb176f 100644 --- a/src/enums/Model.ts +++ b/src/enums/Model.ts @@ -144,5 +144,6 @@ export enum ModalEnum { ///// Question QUESTION_DELETE = "Question.delete", + QUESTION_ACCEPT = "Question.ACCEPT", } diff --git a/src/translate/ar.json b/src/translate/ar.json index e6b6499..07d1857 100644 --- a/src/translate/ar.json +++ b/src/translate/ar.json @@ -189,7 +189,10 @@ "presence": "الحضور", "add_new_role": "اضافة صلاحية جديدة", "yes":"نعم", - "no":"لا" + "no":"لا", + "Are you sure you want to go back and not save any changes?":"هل أنت متأكد أنك تريد العودة وعدم حفظ أي تغييرات؟" , + "accept_back":"قبول العودة ", + "accept":"قبول " }, "Table": { "header": "", diff --git a/src/zustand/ObjectToEditState.ts b/src/zustand/ObjectToEditState.ts index ea3461c..4e7db17 100644 --- a/src/zustand/ObjectToEditState.ts +++ b/src/zustand/ObjectToEditState.ts @@ -19,6 +19,8 @@ interface ModelState { set_current_parent_index: (data: any) => void; isBseQuestion: boolean; set_isBseQuestion: (data: any) => void; + Success: boolean; + set_Success: (data: any) => void; } export const useObjectToEdit = create((set) => ({ @@ -40,4 +42,6 @@ export const useObjectToEdit = create((set) => ({ set_current_parent_index: (data) => set(() => ({ current_parent_index: data })), isBseQuestion: false, set_isBseQuestion: (data) => set(() => ({ isBseQuestion: data })), + Success: false, + set_Success: (data) => set(() => ({ Success: data })), }));