Skip to content

Commit d092328

Browse files
committed
2 parents 79c366d + e77ad2e commit d092328

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

Frontend/src/components/Sidebar.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ function Sidebar() {
5656

5757
fetchUser();
5858

59+
// Custom event listener for category updates
60+
const handleCategoryChange = () => {
61+
fetchCategories();
62+
};
63+
window.addEventListener("categoryChange", handleCategoryChange);
64+
65+
5966
console.log("categories: ", categories)
6067

6168
const matchingServiceHost = '34.126.114.137' || 'localhost';
@@ -87,6 +94,7 @@ function Sidebar() {
8794

8895
return () => {
8996
websocket.close();
97+
window.removeEventListener("categoryChange", handleCategoryChange);
9098
};
9199
}, []);
92100

Frontend/src/components/question/EditQn.jsx

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ function EditQn({ question, handleClose, editQuestion }) {
6161
console.log(`newCategories are ${newCategories}`);
6262

6363
categoryService.createCategories(newCategories)
64-
.then(result => console.log(result.data))
64+
.then(result => {
65+
console.log(result.data)
66+
window.dispatchEvent(new Event("categoryChange"));
67+
})
6568
.catch(e => {
6669
if (e.response && e.response.status === 400) {
6770
setError(e.response.data.error);
@@ -71,7 +74,26 @@ function EditQn({ question, handleClose, editQuestion }) {
7174

7275
questionService.updateQuestion(question._id, updatedQuestion)
7376
.then(result => {
74-
77+
const initialCategories = question.category;
78+
const categoriesRemoved = initialCategories.filter(item => !categories.includes(item));
79+
80+
// check each category to see if it should be deleted
81+
categoriesRemoved.forEach(async (category) => {
82+
try {
83+
const remainingQuestions = await questionService.getQuestionsByCategory(category);
84+
85+
// if no other questions have this category, delete the category
86+
if (remainingQuestions.length === 0) {
87+
await categoryService.deleteCategory(category);
88+
console.log(`Category ${category} deleted.`);
89+
// dispatch the event here
90+
window.dispatchEvent(new Event("categoryChange"));
91+
}
92+
} catch (err) {
93+
console.error(`Error processing category ${category}:`, err);
94+
}
95+
});
96+
7597
editQuestion(question._id, updatedQuestion);
7698
console.log('Question edited successfully:', result)
7799
handleClose();

Frontend/src/components/question/Question.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function Question() {
2929
.then(result => {
3030
setQuestions(result.data);
3131
})
32+
3233
.catch(err => console.log(err));
3334
}, []);
3435

@@ -199,7 +200,7 @@ function Question() {
199200
return (
200201
<div
201202
className="container-fluid ag-theme-quartz"
202-
style={{ height: 500 }}
203+
style={{ height: '80vh' }}
203204
>
204205
<AgGridReact
205206
rowData={questions}

0 commit comments

Comments
 (0)