Skip to content

Commit 2d10aa6

Browse files
committed
Add Update and Delete Functionality
Questions can now be added and deleted
1 parent 48d6b84 commit 2d10aa6

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

peer-prep/src/Components/QuestionList/QuestionList.jsx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,45 @@ export const Questions = () => {
1818
return i
1919
}
2020

21+
// Add a new Question
2122
const addQuestion = (qTitle, qDifficulty, qTopic, qDescription) => {
2223
// Add in Database and Upload into Storage
2324
let newQuestion = new QuestionModel(qId, qTitle, qDescription, qDifficulty, qTopic);
2425
database.addQuestion(newQuestion);
26+
27+
updateLocalDatabase();
28+
updateStates();
29+
30+
// setQs([...qs, {id: qId, title: qTitle, difficulty: qDifficulty,
31+
// topic: qTopic, description: qDescription}]);
32+
}
33+
34+
// Delete a Question
35+
const deleteQuestion = (questionId) => {
36+
database.deleteQuestion(questionId);
37+
38+
updateLocalDatabase();
39+
updateStates();
40+
}
41+
42+
// Update an existing Question
43+
const updateQuestion = (qId, qTitle, qDescription, qDifficulty, qTopic) => {
44+
let updatedQuestion = new QuestionModel(qId, qTitle, qDescription, qDifficulty, qTopic);
45+
database.updateQuestion(updatedQuestion);
46+
47+
updateLocalDatabase();
48+
updateStates();
49+
}
50+
51+
const updateLocalDatabase = () => {
52+
// Updating Database in Local Storageß
2553
updateLocalQuestionDatabase(database);
54+
}
2655

56+
const updateStates = () => {
2757
// Update States
2858
setQs(Array.from(database.database));
2959
setQId(Array.from(database.database).length);
30-
31-
// setQs([...qs, {id: qId, title: qTitle, difficulty: qDifficulty,
32-
// topic: qTopic, description: qDescription}]);
3360
}
3461

3562
// To load data on mount

peer-prep/src/DataModel/QuestionDatabase.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ class QuestionDatabase {
99
this.database = database;
1010
}
1111

12+
clearDatabase() {
13+
this.database = new Map();
14+
}
15+
1216
addQuestion(questionToAdd) {
1317
if (!this.isDuplicateQuestion(questionToAdd)) {
1418
this.database.set(questionToAdd.id, questionToAdd);

0 commit comments

Comments
 (0)