Skip to content

Commit 5014312

Browse files
committed
modal for question table and auto refresh when edit question
1 parent 2b84914 commit 5014312

File tree

7 files changed

+165
-99
lines changed

7 files changed

+165
-99
lines changed

frontend/src/components/ProblemSolverLeft.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const ProblemSolverLeft = () => {
4040
}
4141

4242
return (
43-
<Paper elevation={3} sx={{ flex: 1, display: 'flex', flexDirection: 'column', padding: 2 }}>
43+
<Paper elevation={3} sx={{ flex: 1, display: 'flex', flexDirection: 'column', padding: 2}}>
4444
<Typography variant="h4" gutterBottom sx={{ fontSize: '24px' }}>
4545
{question.title}
4646
</Typography>

frontend/src/components/ProblemSolverRight.tsx

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
import React, { useState } from 'react';
2-
import { Paper, Typography, TextField, MenuItem, Button } from '@mui/material';
1+
import React, { useState } from "react";
2+
import { Paper, Typography, TextField, MenuItem, Button } from "@mui/material";
3+
import { Link } from "react-router-dom";
4+
35
import Editor from "@monaco-editor/react";
46

5-
const languages: string[] = ['C++', 'Java', 'JavaScript', 'Python'];
7+
const languages: string[] = ["C++", "Java", "JavaScript", "Python"];
68

79
function ProblemSolverRight() {
8-
const [selectedLanguage, setSelectedLanguage] = useState<string>('JavaScript');
10+
const [selectedLanguage, setSelectedLanguage] =
11+
useState<string>("JavaScript");
912
// const [code, setCode] = useState<string>('class Solution:');
10-
const code = "class Solution:"
11-
const handleLanguageChange = (event: React.ChangeEvent<{ value: unknown }>) => {
13+
const code = "class Solution:";
14+
const handleLanguageChange = (
15+
event: React.ChangeEvent<{ value: unknown }>
16+
) => {
1217
setSelectedLanguage(event.target.value as string);
1318
};
1419

@@ -22,7 +27,10 @@ function ProblemSolverRight() {
2227
};
2328

2429
return (
25-
<Paper elevation={3} style={{ flex: 1, padding: 16, display: 'flex', flexDirection: 'column' }}>
30+
<Paper
31+
elevation={3}
32+
style={{ flex: 1, padding: 16, display: "flex", flexDirection: "column" }}
33+
>
2634
<Typography variant="h6" gutterBottom>
2735
Select a Language:
2836
</Typography>
@@ -31,33 +39,34 @@ function ProblemSolverRight() {
3139
fullWidth
3240
value={selectedLanguage}
3341
onChange={handleLanguageChange}
34-
style={{ width: '50%' }}
42+
style={{ width: "50%" }}
3543
>
3644
{languages.map((language, index) => (
3745
<MenuItem key={index} value={language}>
3846
{language}
3947
</MenuItem>
4048
))}
4149
</TextField>
42-
<Typography variant="h6" gutterBottom style={{ marginTop: '16px' }}>
50+
<Typography variant="h6" gutterBottom style={{ marginTop: "16px" }}>
4351
Code Editor:
4452
</Typography>
4553
<Editor
46-
height="900px"
47-
language={selectedLanguage.toLowerCase()}
48-
theme="vs-dark"
49-
value={code}
50-
51-
/>
52-
<Button
53-
variant="contained"
54-
color="primary"
55-
fullWidth
56-
onClick={handleSubmit}
57-
style={{ marginTop: '16px' }}
58-
>
59-
Submit
60-
</Button>
54+
height="900px"
55+
language={selectedLanguage.toLowerCase()}
56+
theme="vs-dark"
57+
value={code}
58+
/>
59+
<Link to={`/home`} style={{ textDecoration: "none" }}>
60+
<Button
61+
variant="contained"
62+
color="primary"
63+
fullWidth
64+
onClick={handleSubmit}
65+
style={{ marginTop: "16px" }}
66+
>
67+
Submit
68+
</Button>
69+
</Link>
6170
</Paper>
6271
);
6372
}
Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,61 @@
1-
import React from "react";
1+
import React, { useState } from "react";
22
import QuestionForm from "./QuestionForm";
3-
import {Box, Button} from "@mui/material";
3+
import { Box, Button } from "@mui/material";
44
import Question from "./Question";
55
import Typography from "@mui/material/Typography";
6-
import {addQuestion} from "../../api/questions/data";
7-
import {AxiosError} from "axios";
8-
6+
import { addQuestion } from "../../api/questions/data";
7+
import { AxiosError } from "axios";
98

109
const AddQuestionTab: React.FC = () => {
11-
const [addQuestions, setAddQuestions] = React.useState(false);
12-
13-
const handleAddQuestionClick = () => {
14-
setAddQuestions(true);
15-
}
16-
const onSubmit = async (question: Question) => {
17-
console.log(question);
18-
let questionToAdd = new Question(question);
19-
20-
try {
21-
const questionAdded = await addQuestion(questionToAdd);
22-
console.log(questionAdded);
23-
setAddQuestions(false);
24-
} catch (e) {
25-
if (e instanceof AxiosError && e.response) {
26-
console.log(e.response.data.code);
27-
} else if (e instanceof Error) {
28-
console.log(e.message);
29-
}
30-
}
31-
};
32-
33-
const onCancel = () => {
34-
setAddQuestions(false);
10+
const [addQuestions, setAddQuestions] = useState(false);
11+
12+
13+
const handleAddQuestionClick = () => {
14+
setAddQuestions(true);
15+
};
16+
17+
const onSubmit = async (question: Question) => {
18+
console.log(question);
19+
let questionToAdd = new Question(question);
20+
21+
try {
22+
const questionAdded = await addQuestion(questionToAdd);
23+
console.log(questionAdded);
24+
setAddQuestions(false);
25+
26+
// Navigate to the current location to refresh the page
27+
window.location.reload();
28+
} catch (e) {
29+
if (e instanceof AxiosError && e.response) {
30+
console.log(e.response.data.code);
31+
} else if (e instanceof Error) {
32+
console.log(e.message);
33+
}
3534
}
36-
37-
38-
return (
39-
<React.Fragment>
40-
<Box component="span" padding={2} width='80%'>
41-
{addQuestions ?
42-
<React.Fragment>
43-
<Typography variant="h5" gutterBottom>
44-
Add Question
45-
</Typography>
46-
<QuestionForm onSubmit={onSubmit} onCancel={onCancel} />
47-
</React.Fragment>
48-
:
49-
<Button variant="contained" onClick={handleAddQuestionClick}>
50-
Add Question
51-
</Button>
52-
}
53-
</Box>
54-
</React.Fragment>
55-
);
56-
}
57-
58-
export default AddQuestionTab;
35+
};
36+
37+
const onCancel = () => {
38+
setAddQuestions(false);
39+
};
40+
41+
return (
42+
<React.Fragment>
43+
<Box component="span" padding={2} width="80%">
44+
{addQuestions ? (
45+
<React.Fragment>
46+
<Typography variant="h5" gutterBottom>
47+
Add Question
48+
</Typography>
49+
<QuestionForm onSubmit={onSubmit} onCancel={onCancel} />
50+
</React.Fragment>
51+
) : (
52+
<Button variant="contained" onClick={handleAddQuestionClick}>
53+
Add Question
54+
</Button>
55+
)}
56+
</Box>
57+
</React.Fragment>
58+
);
59+
};
60+
61+
export default AddQuestionTab;

frontend/src/components/Questions/EditQuestionsTab.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const EditQuestionsTab: React.FC = () => {
2525
await updateQuestion(editedQuestion.id, editedQuestion);
2626
console.log(`Question ${editedQuestion.id} updated: ${editedQuestion.title}`);
2727
setEditQuestions(false);
28+
window.location.reload();
2829
} catch (e) {
2930
if (e instanceof AxiosError && e.response) {
3031
console.log(e.response.data.code);
@@ -41,6 +42,7 @@ const EditQuestionsTab: React.FC = () => {
4142
await deleteQuestion(questionToDelete.id);
4243
console.log(`Question ${questionToDelete.id} deleted: ${questionToDelete.title}`);
4344
setEditQuestions(false);
45+
window.location.reload();
4446
} catch (e) {
4547
if (e instanceof AxiosError && e.response) {
4648
console.log(e.response.data.code);

frontend/src/components/Questions/QuestionsTable.tsx

Lines changed: 70 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// InterviewQuestionsTable.tsx
2-
31
import React, { useEffect, useState } from "react";
42
import { Link } from "react-router-dom";
53
import {
@@ -14,6 +12,12 @@ import {
1412
MenuItem,
1513
TablePagination,
1614
SelectChangeEvent,
15+
Button,
16+
Dialog,
17+
DialogTitle,
18+
DialogContent,
19+
DialogActions,
20+
Typography,
1721
} from "@mui/material";
1822
import { useData } from "../../data/data.context";
1923

@@ -37,32 +41,36 @@ const InterviewQuestionsTable: React.FC = () => {
3741
const [itemsPerPage, setItemsPerPage] = useState<number>(
3842
ITEMS_PER_PAGE_OPTIONS[0]
3943
);
44+
const [selectedQuestion, setSelectedQuestion] = useState<Question | null>(
45+
null
46+
);
47+
const [isQuestionModalOpen, setIsQuestionModalOpen] = useState(false);
4048
const { questions, getQuestions } = useData();
4149

4250
useEffect(() => {
4351
async function getInterviewQuestions() {
4452
getQuestions();
4553
}
4654
getInterviewQuestions();
47-
// console.log("here");
48-
// eslint-disable-next-line react-hooks/exhaustive-deps
4955
}, []);
5056

5157
useEffect(() => {
5258
setQuestions(questions);
53-
//console.log(questionsData);
5459
}, [questions]);
5560

5661
const handleCategoriesChange = (event: SelectChangeEvent<unknown>) => {
5762
setSelectedCategories(event.target.value as string[]);
58-
};
63+
};
5964

60-
const uniqueCategories = Array.from(new Set(questionsData.flatMap(question => question.categories)));
65+
const uniqueCategories = Array.from(
66+
new Set(questionsData.flatMap((question) => question.categories))
67+
);
6168

62-
const filteredQuestions = questionsData.filter(question =>
63-
selectedCategories.length === 0 ||
64-
selectedCategories.some(cat => question.categories.includes(cat))
65-
);
69+
const filteredQuestions = questionsData.filter((question) =>
70+
selectedCategories.length === 0
71+
? true
72+
: selectedCategories.some((cat) => question.categories.includes(cat))
73+
);
6674

6775
const handlePageChange = (event: unknown, newPage: number) => {
6876
setCurrentPage(newPage);
@@ -80,6 +88,16 @@ const filteredQuestions = questionsData.filter(question =>
8088
indexOfLastQuestion
8189
);
8290

91+
const openQuestionModal = (question: Question) => {
92+
setSelectedQuestion(question);
93+
setIsQuestionModalOpen(true);
94+
};
95+
96+
const closeQuestionModal = () => {
97+
setSelectedQuestion(null);
98+
setIsQuestionModalOpen(false);
99+
};
100+
83101
return (
84102
<>
85103
{/* Category Filter */}
@@ -107,32 +125,29 @@ const filteredQuestions = questionsData.filter(question =>
107125
<TableCell>Title</TableCell>
108126
<TableCell>Tags</TableCell>
109127
<TableCell>Categories</TableCell>
110-
{/* <TableCell>Constraints</TableCell> */}
111128
<TableCell>Difficulty</TableCell>
112-
{/* <TableCell>Description</TableCell> */}
113129
</TableRow>
114130
</TableHead>
115131
<TableBody>
116132
{currentQuestions.map((question: Question, index: number) => (
117133
<TableRow key={index}>
118134
<TableCell>
119-
<Link
120-
to={`/question/${question.id}`}
135+
<Button
136+
onClick={() => openQuestionModal(question)}
121137
style={{
122138
textDecoration: "none",
123139
color: "inherit",
124-
fontSize: "16px", // Adjust to your preference
140+
fontSize: "16px",
125141
fontWeight: "bold",
142+
textTransform: "initial"
126143
}}
127144
>
128145
{question.title}
129-
</Link>
146+
</Button>
130147
</TableCell>
131148
<TableCell>{question.tags.join(", ")}</TableCell>
132149
<TableCell>{question.categories.join(", ")}</TableCell>
133-
{/* <TableCell>{question.constraints.join(', ')}</TableCell> */}
134150
<TableCell>{question.difficulty}</TableCell>
135-
{/* <TableCell>{question.description}</TableCell> */}
136151
</TableRow>
137152
))}
138153
</TableBody>
@@ -161,6 +176,42 @@ const filteredQuestions = questionsData.filter(question =>
161176
onPageChange={handlePageChange}
162177
/>
163178
</div>
179+
<Dialog
180+
open={isQuestionModalOpen}
181+
onClose={closeQuestionModal}
182+
fullWidth
183+
maxWidth="md"
184+
>
185+
{selectedQuestion && (
186+
<>
187+
<DialogTitle>{selectedQuestion.title}</DialogTitle>
188+
<DialogContent >
189+
<Typography variant="body2" style={{padding: "5px"}}>
190+
<b>Categories:</b> {selectedQuestion.categories.join(", ")}
191+
</Typography>
192+
<Typography variant="body2" style={{padding: "5px"}}>
193+
<b>Difficulty:</b> {selectedQuestion.difficulty}
194+
</Typography>
195+
<Typography variant="body2" style={{padding: "5px"}}>
196+
<b>Description</b>: {selectedQuestion.description}
197+
</Typography>
198+
</DialogContent>
199+
<DialogActions>
200+
<Button onClick={closeQuestionModal} color="primary">
201+
Close
202+
</Button>
203+
<Link
204+
to={`/question/${selectedQuestion.id}`}
205+
style={{ textDecoration: "none" }}
206+
>
207+
<Button color="primary" variant="contained">
208+
Solve Question
209+
</Button>
210+
</Link>
211+
</DialogActions>
212+
</>
213+
)}
214+
</Dialog>
164215
</>
165216
);
166217
};

0 commit comments

Comments
 (0)