Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/actions/topicAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,26 @@ export const getTopics = (subjectName, standard, chapterNames) => async (dispatc
try {
dispatch({ type: GET_TOPICS_REQUEST });

const { data } = await axios.get(
`${server}/api/get/topic?subjectName=${subjectName}&standard=${standard}&chapterName=${chapterNames}`,
{
withCredentials: true,
}
);
// Ensure chapterNames is properly formatted as a comma-separated string
const chapters = Array.isArray(chapterNames) ? chapterNames.join(',') : chapterNames;

// Make API call to get topics
const { data } = await axios.get(`${server}/api/get/topic`, {
params: {
subjectName,
standard,
chapterName: chapters,
},
withCredentials: true, // Include credentials if needed
});

dispatch({
type: GET_TOPICS_SUCCESS,
payload: data.topics,
});
} catch (error) {
console.error('Error fetching topics:', error); // Log the error for debugging

dispatch({
type: GET_TOPICS_FAIL,
payload: error.response?.data?.message || error.message,
Expand Down
85 changes: 45 additions & 40 deletions src/components/EditModel.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// EditQuestionModal.jsx
import { Select } from 'antd';
import React, { useEffect, useState } from 'react';
import { Select } from 'antd';
import { getSubjects } from "../actions/subjectAction";
import { getChapters } from "../actions/chapterAction";
import { getTopics } from "../actions/topicAction";
Expand All @@ -18,66 +17,68 @@ const EditModel = ({ isOpen, onClose, selectedQuestion, onSave, setIsModalOpen }
const { subtopics } = useSelector((state) => state.getSubtopic);
const [standard, setStandard] = useState(selectedQuestion.standard || '');
const [subject, setSubject] = useState(selectedQuestion.subject || '');
const [chapter, setChapter] = useState(selectedQuestion.chapter || '');
const [chapter, setChapter] = useState(selectedQuestion.chapter || []);
const [topic, setTopic] = useState(selectedQuestion.topics || []);
const [subtopic, setSubtopic] = useState(selectedQuestion.subtopics || []);
const dispatch = useDispatch();
const [error, setError] = useState('');
if (!isOpen) return null;

useEffect(() => {
if (standard) {
dispatch(getSubjects(standard));
}
if (subject && standard) {
dispatch(getChapters(subject, standard));
}

const fetchTopics = async () => {
if (subject && standard && chapter.length > 0) {
setError(''); // Clear any error if chapters are selected
let allTopics = []; // To store all topics from selected chapters
for (const chap of chapter) {
const response = await dispatch(getTopics(subject, standard, chap));
allTopics = [...allTopics, ...response.topics]; // Combine topics from all chapters
setError('');
try {
// Fetch topics for all selected chapters in one go
const chapterNames = chapter.join(','); // Convert array to comma-separated string
const response = await dispatch(getTopics(subject, standard, chapterNames));

if (response && response.payload) {
setTopic(response.payload);
}
} catch (error) {
console.error('Error fetching topics:', error);
}
setTopic(allTopics); // Set the combined topics if different from current state
} else if (subject && standard && chapter.length === 0) {
setError('Chapter selection is required.');
}
};
fetchTopics(); // Call the function to fetch topics
if (subject && standard && chapter.length > 0 && topic) {

fetchTopics();

if (subject && standard && chapter.length > 0 && topic.length > 0) {
dispatch(getSubtopics(subject, standard, chapter, topic));
}
}, [dispatch, standard, subject, chapter, topic, subtopic]);
}, [dispatch, standard, subject, chapter, topic]); // Add `topic` to dependency array

const handleSaveChanges = async () => {
// Check if chapter is selected
if (!chapter || chapter.length === 0) {
setError('Chapter selection is required.');
return; // Prevent form submission if chapter is not selected
return;
}

// Create the updated question object after validation passes

const updatedQuestion = {
standard,
subject,
chapter,
topics: topic,
subtopics: subtopic,
};

try {
// Send the PUT request to update the question
const response = await axios.put(`${server}/api/updatequestion/${selectedQuestion._id}`, updatedQuestion);
const data = response.data;

if (response.status === 200) {
onSave(data.question);
toast.success("Updated successfully");
setIsModalOpen(false); // Close the modal after successful update
setIsModalOpen(false);
} else {
console.error('Failed to update question:', data.message);
toast.error('Failed to update question: ' + data.message);
Expand All @@ -87,8 +88,6 @@ const EditModel = ({ isOpen, onClose, selectedQuestion, onSave, setIsModalOpen }
toast.error('Error updating question: ' + error.message);
}
};



const handleOverlayClick = (e) => {
if (e.target === e.currentTarget) {
Expand All @@ -104,10 +103,11 @@ const EditModel = ({ isOpen, onClose, selectedQuestion, onSave, setIsModalOpen }
>
<div
className="bg-white rounded-lg shadow-lg p-6 w-11/12 sm:w-2/3 md:w-1/2 lg:w-1/3 xl:w-1/4 text-gray-900"
onClick={(e) => e.stopPropagation()}
onClick={(e) => e.stopPropagation()}
>
<h3 className="text-xl mb-4">Edit Details</h3>

{/* Standard Selector */}
<div className="relative z-0 w-full md:w-auto flex flex-col-reverse group">
<Select
showSearch
Expand All @@ -116,9 +116,9 @@ const EditModel = ({ isOpen, onClose, selectedQuestion, onSave, setIsModalOpen }
onChange={(value) => {
setStandard(value);
setSubject(null);
setChapter(null);
setTopic(null);
setSubtopic(null)
setChapter([]);
setTopic([]);
setSubtopic([]);
}}
options={standards}
value={standard}
Expand All @@ -128,6 +128,7 @@ const EditModel = ({ isOpen, onClose, selectedQuestion, onSave, setIsModalOpen }
</label>
</div>

{/* Subject Selector */}
<div className="relative z-0 w-full md:w-auto flex flex-col-reverse group">
<Select
showSearch
Expand All @@ -138,9 +139,9 @@ const EditModel = ({ isOpen, onClose, selectedQuestion, onSave, setIsModalOpen }
}
onChange={(value) => {
setSubject(value);
setChapter(null);
setTopic(null);
setSubtopic(null)
setChapter([]);
setTopic([]);
setSubtopic([]);
}}
options={subjectList?.map((name) => ({
value: name,
Expand All @@ -152,20 +153,21 @@ const EditModel = ({ isOpen, onClose, selectedQuestion, onSave, setIsModalOpen }
Subject
</label>
</div>

{/* Chapter Selector */}
<div className="relative z-0 w-full md:w-auto flex flex-col-reverse group">
<Select
mode="multiple"
showSearch

style={{ width: 200 }}
placeholder="Select Chapter"
filterOption={(input, option) =>
(option.label ?? "").toLowerCase().includes(input.toLowerCase())
}
onChange={(value) => {
setChapter(value);
setTopic(null);
setSubtopic(null)
setTopic([]);
setSubtopic([]);
}}
options={chapterList?.map((chapter) => ({
value: chapter.name,
Expand All @@ -180,6 +182,7 @@ const EditModel = ({ isOpen, onClose, selectedQuestion, onSave, setIsModalOpen }
{error && <span className="text-red-500 text-xs mt-1">{error}</span>}
</div>

{/* Topic Selector */}
<div className="relative z-0 w-full md:w-auto flex flex-col-reverse group">
<Select
mode="multiple"
Expand All @@ -191,6 +194,7 @@ const EditModel = ({ isOpen, onClose, selectedQuestion, onSave, setIsModalOpen }
}
onChange={(value) => {
setTopic(value);
setSubtopic([]); // Clear subtopics when topics change
}}
options={topicList?.map((el) => ({
value: el.name,
Expand All @@ -202,6 +206,8 @@ const EditModel = ({ isOpen, onClose, selectedQuestion, onSave, setIsModalOpen }
Topic
</label>
</div>

{/* Subtopic Selector */}
<div className="relative z-0 w-full md:w-auto flex flex-col-reverse group">
<Select
mode="multiple"
Expand All @@ -214,16 +220,15 @@ const EditModel = ({ isOpen, onClose, selectedQuestion, onSave, setIsModalOpen }
onChange={(value) => {
setSubtopic(value)
}}
options={subtopics.map((subtopic) => ({
value: subtopic.name,
options={subtopics && subtopics.map((subtopic) => ({
value: subtopic._id, // Ensure that `_id` is used as the value
label: subtopic.name,
}))}
value={subtopic}
/>
<label className="text-white-500 text-sm dark:text-white-400">
Subtopic
</label>

</div>

<div className="mt-4 flex justify-end">
Expand Down
Loading