{topics.map((topic, index) => (
-
-
handleTopicChange(index, e.target.value)}
- required
- />
-
+
))}
+
+
- {/* Add More Topics Button */}
{
Add more topics
- {/* Submit Button */}
diff --git a/src/pages/EditDetails.jsx b/src/pages/EditDetails.jsx
index a5b5dd0..ba44e15 100644
--- a/src/pages/EditDetails.jsx
+++ b/src/pages/EditDetails.jsx
@@ -29,6 +29,8 @@ const EditDetails = () => {
const [isChapterModalOpen, setIsChapterModalOpen] = useState(false);
const [newChapterName, setNewChapterName] = useState("");
const [currentChapterId, setCurrentChapterId] = useState(null);
+ const [newChapterNumber, setNewChapterNumber] = useState(null);
+ const [newTopicNumber, setNewTopicNumber] = useState(null);
useEffect(() => {
const fetchData = async () => {
try {
@@ -72,14 +74,14 @@ const EditDetails = () => {
try {
setLoadingTopics((prev) => ({ ...prev, [chapterId]: true }));
-
+
const response = await axios.get(
- `${server}/api/get/topic?subjectName=${subjectName}&standard=${standard}&chapterId=${chapterId}`
+ `${server}/api/get/topic?subjectName=${subjectName}&standard=${standard}&chapterId=${chapterId}`
);
-
+
setTopicsByChapter((prev) => ({
...prev,
- [chapterId]: response.data.topics || [],
+ [chapterId]: response.data.topics || [],
}));
} catch (error) {
console.error("Error fetching topics:", error.message);
@@ -87,12 +89,12 @@ const EditDetails = () => {
setLoadingTopics((prev) => ({ ...prev, [chapterId]: false }));
}
};
-
+
const handleChapterClick = (subjectName, chapterId) => {
if (!topicsByChapter[chapterId]) {
fetchTopics(subjectName, chapterId);
}
-
+
setVisibleChapters((prev) => {
const updatedChapters = {
...prev,
@@ -102,7 +104,7 @@ const EditDetails = () => {
return updatedChapters;
});
};
-
+
const fetchSubTopics = async (
subjectName,
@@ -112,11 +114,11 @@ const EditDetails = () => {
) => {
try {
setLoadingSubtopics((prev) => ({ ...prev, [topicId]: true }));
-
+
const response = await axios.get(
`${server}/api/get/subtopic?subjectName=${subjectName}&standard=${standard}&chapterId=${chapterId}&topicId=${topicId}` // Use chapterId and topicId
);
-
+
setSubTopicByTopics((prev) => ({
...prev,
[topicId]: response.data.subtopics || [], // Store subtopics using topicId
@@ -127,63 +129,74 @@ const EditDetails = () => {
setLoadingSubtopics((prev) => ({ ...prev, [topicId]: false }));
}
};
-
+
const handleTopicClick = (subjectName, chapterId, topicId, topicName) => {
if (!subTopicByTopics[topicId]) {
fetchSubTopics(subjectName, chapterId, topicId, topicName);
}
-
+
setVisibleTopics((prev) => ({
...prev,
[topicId]: !prev[topicId],
}));
};
-
+
const handleUpdateTopic = async () => {
try {
- // Ensure both topicId and newTopicName are provided
- if (!currentTopicId || !newTopicName) {
- toast.error("Topic ID and new name must be provided");
+ // Validate the input fields
+ if (!currentTopicId || !newTopicName || newTopicNumber === undefined) {
+ toast.error("Topic ID, new name, and topic number must be provided");
return;
}
- // API request to update the topic name
+ // Make the API request to update the topic
const response = await axios.put(
`${server}/api/update/topic/${currentTopicId}`,
- { name: newTopicName }
+ { name: newTopicName, topicNumber: newTopicNumber }
);
if (response.data.success) {
- toast.success("Topic name updated successfully");
+ toast.success("Topic name and number updated successfully");
+
// Update the local state to reflect the changes
setTopicsByChapter((prev) => {
const updatedChapters = { ...prev };
Object.keys(updatedChapters).forEach((chapter) => {
updatedChapters[chapter] = updatedChapters[chapter].map((topic) =>
topic._id === currentTopicId
- ? { ...topic, name: newTopicName }
+ ? { ...topic, name: newTopicName, topicNumber: newTopicNumber }
: topic
);
});
return updatedChapters;
});
} else {
- toast.error(response.data.message || "Failed to update topic name");
+ // Display error message from backend
+ toast.error(response.data.message || "Failed to update topic");
}
} catch (error) {
console.error("Error in handleUpdateTopic:", error);
- toast.error("An unexpected error occurred. Please try again later.");
+
+ // Display backend error message or a fallback error message
+ if (error.response && error.response.data && error.response.data.message) {
+ toast.error(error.response.data.message);
+ } else {
+ toast.error("An unexpected error occurred. Please try again later.");
+ }
} finally {
- setIsModalOpen(false); // Close the modal after the update
+ // Close the modal after the update
+ setIsModalOpen(false);
}
};
+
const openEditModal = (topicId, topicName) => {
setCurrentTopicId(topicId);
setNewTopicName(topicName);
setIsModalOpen(true);
+ setNewTopicNumber(topicNumber)
};
const handleDeleteTopic = (topicId) => {
Modal.confirm({
@@ -256,7 +269,7 @@ const EditDetails = () => {
console.error("Error in handleUpdateSubtopic:", error);
toast.error("An unexpected error occurred. Please try again later.");
} finally {
- setIsSubtopicModalOpen(false);
+ setIsSubtopicModalOpen(false);
}
};
@@ -308,43 +321,56 @@ const EditDetails = () => {
const handleUpdateChapter = async () => {
try {
- if (!currentChapterId || !newChapterName) {
- toast.error("Chapter ID and new name must be provided");
+ if (!currentChapterId || !newChapterName || newChapterNumber === undefined) {
+ toast.error("Chapter ID, new name, and chapter number must be provided.");
return;
}
const response = await axios.put(
`${server}/api/update/chapter/${currentChapterId}`,
- { name: newChapterName }
+ {
+ name: newChapterName,
+ chapterNumber: newChapterNumber
+ }
);
if (response.data.success) {
- toast.success("Topic name updated successfully");
+ toast.success("Chapter name and number updated successfully!");
+
setChaptersBySubject((prev) => {
const updatedChapters = { ...prev };
- Object.keys(updatedChapters).forEach((chapter) => {
- updatedChapters[chapter] = updatedChapters[chapter].map((chapter) =>
+
+ Object.keys(updatedChapters).forEach((subject) => {
+ updatedChapters[subject] = updatedChapters[subject].map((chapter) =>
chapter._id === currentChapterId
- ? { ...chapter, name: newChapterName }
+ ? { ...chapter, name: newChapterName, chapterNumber: newChapterNumber }
: chapter
);
});
+
return updatedChapters;
});
} else {
- toast.error(response.data.message || "Failed to update chapter name");
+ toast.error(response.data.message || "Failed to update chapter.");
}
} catch (error) {
console.error("Error in handleUpdateChapter:", error);
- toast.error("An unexpected error occurred. Please try again later.");
- } finally {
+
+ const errorMessage = error.response?.data?.message || "An unexpected error occurred. Please try again later.";
+
+ toast.error(errorMessage);
+ }
+ finally {
setIsChapterModalOpen(false);
}
};
+
+
const openEditChapterModal = (chapterId, chapterName) => {
setCurrentChapterId(chapterId);
setNewChapterName(chapterName);
setIsChapterModalOpen(true);
+ setNewChapterNumber(chapterNumber);
};
const handleDeleteChapter = (chapterId) => {
@@ -363,7 +389,7 @@ const EditDetails = () => {
if (response.data.success) {
toast.success("Subtopic deleted successfully");
-
+
setSubTopicByTopics((prev) => {
const updatedTopics = { ...prev };
Object.keys(updatedTopics).forEach((chapter) => {
@@ -428,121 +454,121 @@ const EditDetails = () => {
/>
{Array.isArray(chaptersBySubject[subject]) &&
- chaptersBySubject[subject].length > 0 ? (
-
- {chaptersBySubject[subject].map((chapter) => (
- -
-
- {visibleChapters[chapter._id] && ( // Use chapter._id here as well
-
- {loadingTopics[chapter._id] ? ( // Loading state using chapter._id
-
- ) : (
- <>
- {Array.isArray(topicsByChapter[chapter._id]) && topicsByChapter[chapter._id].length > 0 ? (
-
- {topicsByChapter[chapter._id].map((topic) => (
- -
-
- {visibleTopics[topic._id] && (
-
- {loadingSubtopics[topic._id] ? (
-
- ) : (
- <>
- {subTopicByTopics[topic._id] && subTopicByTopics[topic._id].length > 0 ? (
-
- {subTopicByTopics[topic._id].map((subtopic) => (
- -
- {subtopic.name}
-
+ chaptersBySubject[subject].length > 0 ? (
+
+ {chaptersBySubject[subject].map((chapter) => (
+ -
+
+
{chapter.name}
+
-
- ))}
-
- ) : (
-
No subtopics available
- )}
- >
- )}
-
- )}
-
- ))}
-
-) : (
-
No topics available
-)}
-
- >
- )}
-
- )}
-
- ))}
-
-) : (
-
No chapters available
-)}
+
+ {visibleChapters[chapter._id] && (
+
+ {loadingTopics[chapter._id] ? (
+
+ ) : (
+ <>
+ {Array.isArray(topicsByChapter[chapter._id]) && topicsByChapter[chapter._id].length > 0 ? (
+
+ {topicsByChapter[chapter._id].map((topic) => (
+ -
+
+ {visibleTopics[topic._id] && (
+
+ {loadingSubtopics[topic._id] ? (
+
+ ) : (
+ <>
+ {subTopicByTopics[topic._id] && subTopicByTopics[topic._id].length > 0 ? (
+
+ ) : (
+
No subtopics available
+ )}
+ >
+ )}
+
+ )}
+
+ ))}
+
+ ) : (
+
No topics available
+ )}
+
+ >
+ )}
+
+ )}
+
+ ))}
+
+ ) : (
+
No chapters available
+ )}
))
) : (
@@ -550,7 +576,7 @@ const EditDetails = () => {
)}