Skip to content

Commit da61c2c

Browse files
author
Aishwarya Nair
committed
resolve merge conflict
2 parents 7b66169 + 2d10aa6 commit da61c2c

File tree

7 files changed

+197
-10
lines changed

7 files changed

+197
-10
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

peer-prep.zip

-78.6 MB
Binary file not shown.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const Question = ({question, i}) => {
3333
<span> {selected === i ? "-" : "+"}</span>
3434
</div>
3535
<div className= {selected === i ? "q-content-show": "q-content"}>
36+
<<<<<<< HEAD
3637
<div className="q-tag-container">
3738
<div className="q-tags">
3839
<div className= {tagClass}>{question.difficulty}</div>
@@ -41,6 +42,11 @@ export const Question = ({question, i}) => {
4142
<div className="q-edit">
4243
<img src = {edit_icon} alt=""/>
4344
</div>
45+
=======
46+
<div className="q-tags">
47+
<div className="q-tag">{question.complexity}</div>
48+
<div className="q-tag">{question.category}</div>
49+
>>>>>>> 2d10aa60338d872e3fe12c04076ef7f7dbda4179
4450
</div>
4551

4652
<div className="q-description">{question.description}</div>
Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,69 @@
1-
import React, { useState, useEffect} from 'react'
1+
import React, { useState, useEffect } from 'react'
22
import { QuestionForm } from './QuestionForm'
3-
import {Question} from './Question'
3+
import { Question } from './Question'
44
import './QuestionList.css'
55

6+
import QuestionModel from '../../DataModel/Question'
7+
import { setupQuestionDatabase, retrieveQuestionDatabase, updateLocalQuestionDatabase } from "../../Utility/localStorage"
8+
69
export const Questions = () => {
10+
// Initializing Database
11+
setupQuestionDatabase();
12+
13+
const [database, setDatabase] = useState(retrieveQuestionDatabase());
714
const [qId, setQId] = useState(0);
815
const [isAddQ, setAddQ] = useState(false);
916
const [qs, setQs] = useState([])
1017
const toggle = (i) => {
1118
return i
1219
}
1320

21+
// Add a new Question
1422
const addQuestion = (qTitle, qDifficulty, qTopic, qDescription) => {
15-
setQs([...qs, {id: qId, title: qTitle, difficulty: qDifficulty,
16-
topic: qTopic, description: qDescription}]);
23+
// Add in Database and Upload into Storage
24+
let newQuestion = new QuestionModel(qId, qTitle, qDescription, qDifficulty, qTopic);
25+
database.addQuestion(newQuestion);
26+
27+
updateLocalDatabase();
28+
updateStates();
29+
30+
// setQs([...qs, {id: qId, title: qTitle, difficulty: qDifficulty,
31+
// topic: qTopic, description: qDescription}]);
1732
}
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ß
53+
updateLocalQuestionDatabase(database);
54+
}
55+
56+
const updateStates = () => {
57+
// Update States
58+
setQs(Array.from(database.database));
59+
setQId(Array.from(database.database).length);
60+
}
61+
62+
// To load data on mount
63+
useEffect(() => {
64+
setQs(Array.from(database.database));
65+
setQId(Array.from(database.database).length);
66+
}, [])
1867

1968
useEffect(() => console.log(qs), [qs]);
2069

@@ -23,21 +72,18 @@ export const Questions = () => {
2372
<div className="q-wrapper">
2473
<div className="accordion">
2574
{qs.map((q, i) => (
26-
<Question question = {q} key = {q.id} i = {i}/>
75+
<Question key = {q[0]} question = {q[1]} i = {i}/>
2776
))}
2877
</div>
2978

30-
{isAddQ === false ? <div></div> : <QuestionForm qId = {qId} addQuestion = {addQuestion}
31-
setAddQ = {setAddQ}/>}
79+
{isAddQ === false ? <div></div> :
80+
<QuestionForm qId = {qId} addQuestion = {addQuestion} setAddQ = {setAddQ}/>}
3281
<div className="add-q-btn" onClick = {() => {
3382
if (!isAddQ) {
3483
setQId(qId + 1);
3584
setAddQ(true);
3685
}
3786
}}> Add question</div>
3887
</div>
39-
40-
41-
4288
)
4389
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Class that stores and encapsulates the Question and its related fields
2+
3+
class Question {
4+
constructor(id, title, description, complexity, category) {
5+
this.id = id;
6+
this.title = title;
7+
this.description = description;
8+
this.complexity = complexity;
9+
this.category= category;
10+
}
11+
12+
updateQuestionTitle(newQuestionTitle) {
13+
this.title = newQuestionTitle;
14+
}
15+
16+
updateQuestionDescription(newQuestionDescription) {
17+
this.description = newQuestionDescription;
18+
}
19+
20+
updateQuestionComplexity(newQuestionComplexity) {
21+
this.complexity = newQuestionComplexity;
22+
}
23+
24+
updateQuestionCategory(newQuestionCategory) {
25+
this.category = newQuestionCategory;
26+
}
27+
28+
toJSON() {
29+
return {
30+
id: this.id,
31+
title: this.title,
32+
description: this.description,
33+
complexity: this.complexity,
34+
category: this.category,
35+
};
36+
}
37+
}
38+
39+
export default Question;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// A Class that stores and encapsulates Database Features
2+
3+
class QuestionDatabase {
4+
constructor() {
5+
this.database = new Map();
6+
}
7+
8+
setDatabase(database) {
9+
this.database = database;
10+
}
11+
12+
clearDatabase() {
13+
this.database = new Map();
14+
}
15+
16+
addQuestion(questionToAdd) {
17+
if (!this.isDuplicateQuestion(questionToAdd)) {
18+
this.database.set(questionToAdd.id, questionToAdd);
19+
}
20+
}
21+
22+
deleteQuestion(questionId) {
23+
if (this.database.has(questionId)) {
24+
this.database.delete(questionId);
25+
}
26+
}
27+
28+
updateQuestion(questionToUpdate) {
29+
this.database.set(questionToUpdate.id, questionToUpdate);
30+
}
31+
32+
getAllQuestions() {
33+
var questionList = [];
34+
35+
for (const [key, value] of this.database) {
36+
questionList.push(value);
37+
}
38+
39+
return questionList;
40+
}
41+
42+
isDuplicateQuestion(questionToCheck) {
43+
if (this.database.has(questionToCheck.id)) {
44+
return true;
45+
}
46+
47+
var questionList = this.getAllQuestions();
48+
49+
for (var i = 0; i < questionList.length; i++) {
50+
if (questionToCheck.title === questionList[i].title) {
51+
return true;
52+
}
53+
54+
if (questionToCheck.description === questionList[i].description) {
55+
return true;
56+
}
57+
}
58+
59+
return false;
60+
}
61+
62+
toJSON() {
63+
return this.database;
64+
}
65+
}
66+
67+
export default QuestionDatabase;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import QuestionDatabase from "../DataModel/QuestionDatabase";
2+
3+
function updateLocalQuestionDatabase(updatedDatabase) {
4+
const data = JSON.stringify(Array.from(updatedDatabase.database.entries()));
5+
localStorage.setItem("questions", data);
6+
}
7+
8+
function retrieveQuestionDatabase() {
9+
let questionDatabase = new QuestionDatabase();
10+
questionDatabase.setDatabase(new Map(JSON.parse(localStorage.getItem("questions"))))
11+
12+
return questionDatabase;
13+
}
14+
15+
function setupQuestionDatabase() {
16+
var data = localStorage.getItem("questions");
17+
18+
if(!data) {
19+
localStorage.setItem("questions", new QuestionDatabase())
20+
};
21+
}
22+
23+
export { setupQuestionDatabase, retrieveQuestionDatabase, updateLocalQuestionDatabase };

0 commit comments

Comments
 (0)