From 68c61ddccd05f655f2bde39d62ddf768a90c021e Mon Sep 17 00:00:00 2001 From: Pkpedram Date: Mon, 7 Mar 2022 11:36:13 +0330 Subject: [PATCH] Model Of Data Changed --- src/redux/reducers/exam.js | 64 +++++++++++++++++-- .../addExam/views/AddQuestionsFromBank.js | 51 +++++++++------ 2 files changed, 91 insertions(+), 24 deletions(-) diff --git a/src/redux/reducers/exam.js b/src/redux/reducers/exam.js index d8a5229..e0d06a4 100644 --- a/src/redux/reducers/exam.js +++ b/src/redux/reducers/exam.js @@ -48,11 +48,13 @@ const initialState = { courseBank: [ { id: 1, - title: 'ریاضی' + title: 'ریاضی', + selectedQuestions: [] }, { id: 2, - title: 'شیمی' + title: 'شیمی', + selectedQuestions: [] }, ], questionBank: [ @@ -143,6 +145,35 @@ const initialState = { }, ] }, + { + questionId: 4, + courseId: 2, + title:'asdaw dsfsdfisdfsdfsfsdfsfsdfsdfermc', + answeredPercentage: '83%', + score: 1, + options: [ + { + id: 1, + title:'asdasd', + isAnswer: false + }, + { + id: 2, + title:'asdasd2', + isAnswer: false + }, + { + id: 3, + title:'sdfsdfsdfsfd', + isAnswer: true + }, + { + id: 4, + title:'asdasd4', + isAnswer: false + }, + ] + }, ], courses: [ @@ -155,7 +186,7 @@ const initialState = { } export default function addExamReduser(state = initialState, { type, payload }) { - + console.log(state.courses) switch (type) { case 'ADD_EXAM_GRADE_AND_GROUP': return { @@ -209,15 +240,30 @@ export default function addExamReduser(state = initialState, { type, payload }) } case 'ADD_EXAM_QUESTIONS': + console.log(payload) + const othercourses = state.courses.filter(itm => itm.id !== payload.courseId) + const selectedC = state.courses.filter(itm => itm.id == payload.courseId) + selectedC[0] = {...selectedC[0], selectedQuestions: [...selectedC[0].selectedQuestions, payload]} + const newCourses = [...othercourses, ...selectedC] + return { ...state, - questions : [...state.questions, payload] + courses: newCourses + } - case 'REMOVE_EXAM_QUESTIONS': + + case 'REMOVE_EXAM_QUESTIONS': + const dontDeleteThese = state.courses.filter(itm => itm.id !== payload.courseId) + const courseForDelete = state.courses.filter(itm => itm.id == payload.courseId) + courseForDelete[0] = {...courseForDelete[0], selectedQuestions: courseForDelete[0].selectedQuestions.filter(itm => itm.id !== payload.id)} + const newCoursesWithDeleted = [...dontDeleteThese, ...courseForDelete] + console.log(newCoursesWithDeleted) + console.log(payload) return { ...state, - questions: state.questions.filter(itm => itm.questionId !== payload.questionId) + courses: newCoursesWithDeleted + } case 'REMOVE_QUESTION_FROM_BANK': return { @@ -230,6 +276,12 @@ export default function addExamReduser(state = initialState, { type, payload }) questionBank : [...state.questionBank, payload] } + case 'CHANGE_QUESTION_ORDER': + return{ + ...state, + questions: payload + } + default: return state; diff --git a/src/view/addExam/views/AddQuestionsFromBank.js b/src/view/addExam/views/AddQuestionsFromBank.js index 97b0978..124ecb8 100644 --- a/src/view/addExam/views/AddQuestionsFromBank.js +++ b/src/view/addExam/views/AddQuestionsFromBank.js @@ -26,7 +26,8 @@ const AddQuestionsFromBank = ({ grades, addExamState, dispatch, stepClick, backS const [choosedQuestion, seChoosedQuestion] = useState(null); const [selectedCourse, setSelectedCourse] = useState({ id: null, - title: '' + title: '', + selectedQuestions: [], }); const [answers, setAnswers] = useState(null); const [newCourse, setNewCourse] = useState({ @@ -36,13 +37,17 @@ const AddQuestionsFromBank = ({ grades, addExamState, dispatch, stepClick, backS name: '', coefficient: 4 }) + + // console.log(choosedQuestionaries) // console.log(newQuestionaire) // console.log(addExamState.questionnaires) - console.log(newCourse) - console.log(addExamState.questionairesCourses) + // console.log(newCourse) + // console.log(addExamState.questionairesCourses) console.log(selectedCourse) - console.log(answers) + // console.log(answers) + console.log(choosedQuestion) + console.log(selectedCourse.selectedQuestions) const [fakeState, setFakeState] = useState(2); @@ -71,9 +76,24 @@ const AddQuestionsFromBank = ({ grades, addExamState, dispatch, stepClick, backS } }else{ - console.log(result) + if(source.droppableId == 'Choosed_Questions_Form_Bank'){ + // const data = { + // startIndex: choosedQuestion, + // endIndex: destination.index + // } + // dispatch({type:'CHANGE_QUESTION_ORDER', payload : data}) + // console.log(data) + + const aaa = [...addExamState.questions]; + const [removed] = aaa.splice(aaa.indexOf(itm => itm.questionId == source.index), 1); + + aaa.splice(aaa.indexOf(itm=> itm.questionId == destination.index), 0, removed); + dispatch({type: 'CHANGE_QUESTION_ORDER', payload: aaa}) + console.log(result) + } } } + console.log(addExamState.courses.filter(itm => itm.id == selectedCourse.id)[0]?.selectedQuestions) return ( <> @@ -197,7 +217,7 @@ const AddQuestionsFromBank = ({ grades, addExamState, dispatch, stepClick, backS { console.log(result) if(result.source.droppableId == 'Choosed_Questions_Form_Bank'){ - seChoosedQuestion(addExamState.questions.find(itm => itm.questionId == result.source.index)) + seChoosedQuestion(addExamState.courses.filter(itm => itm.id == selectedCourse.id)[0]?.selectedQuestions?.find(itm => itm.questionId == result.source.index)) }else{ seChoosedQuestion(addExamState.questionBank.find(itm => itm.questionId == result.source.index)) } @@ -213,11 +233,11 @@ const AddQuestionsFromBank = ({ grades, addExamState, dispatch, stepClick, backS

{selectedCourse.title}

-
+
{ - addExamState.questions.filter(itm => itm.courseId == selectedCourse.id).map( + addExamState.courses?.filter(itm => itm.id == selectedCourse.id)[0]?.selectedQuestions?.map( (item, index) => ( {(provided) => ( @@ -232,6 +252,7 @@ const AddQuestionsFromBank = ({ grades, addExamState, dispatch, stepClick, backS
+ {provided.placeholder} ) } {/* {selectedQuestionare.id !== null && @@ -240,7 +261,7 @@ const AddQuestionsFromBank = ({ grades, addExamState, dispatch, stepClick, backS افزودن درس
} */} - {provided.placeholder} + )} @@ -254,20 +275,14 @@ const AddQuestionsFromBank = ({ grades, addExamState, dispatch, stepClick, backS {/* {setAnswers(data); setFakeState(fakeState - 2)}}/> */} {selectedCourse.id !== null && - addExamState.questionBank.filter(itm => itm.courseId == selectedCourse.id).map( + addExamState.questionBank?.filter(itm => itm.courseId == selectedCourse.id)?.map( (course, index) => ( {(provided) => ( -
- - -
+
-
- - -
+
)}
)