Skip to content

Commit 823ec82

Browse files
authored
Merge pull request #17 from guanquann/admin-question-view
Fix bugs
2 parents 0c5f684 + fc75bab commit 823ec82

File tree

3 files changed

+16
-46
lines changed

3 files changed

+16
-46
lines changed

backend/user-service/controller/auth-controller.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ export async function handleLogin(req: AuthenticatedRequest, res: Response): Pro
2020
}
2121

2222
const accessToken = jwt.sign(
23-
{ id: user.id },
23+
{ id: user.id, admin: user.isAdmin },
2424
process.env.JWT_SECRET as string,
2525
{ expiresIn: "7d" }
2626
);
27-
console.log(accessToken);
2827
return res.status(200).json({
2928
message: "User logged in",
3029
data: { accessToken, user: formatUserResponse(user) },

frontend/src/components/QuestionCategoryAutoComplete/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { createFilterOptions } from "@mui/material/Autocomplete";
33
import { categoryList } from "../../utils/constants";
44

55
interface QuestionCategoryAutoCompleteProps {
6-
selectedCategories?: string[];
7-
setSelectedCategories: (value: string[]) => void;
6+
selectedCategories: string[];
7+
setSelectedCategories: React.Dispatch<React.SetStateAction<string[]>>;
88
}
99

1010
const QuestionCategoryAutoComplete: React.FC<QuestionCategoryAutoCompleteProps> = ({
@@ -27,8 +27,9 @@ const QuestionCategoryAutoComplete: React.FC<QuestionCategoryAutoCompleteProps>
2727
onChange={(e, newCategoriesSelected) => {
2828
const newValue = newCategoriesSelected[newCategoriesSelected.length - 1];
2929
if (typeof newValue === "string" && newValue.startsWith(`Add: "`)) {
30-
categoryList.push(newValue.slice(6, -1));
31-
setSelectedCategories([...newCategoriesSelected.slice(0, -1), newValue.slice(6, -1)]);
30+
const newCategory = newValue.slice(6, -1);
31+
categoryList.push(newCategory);
32+
setSelectedCategories((prev) => [...prev, newCategory]);
3233
} else {
3334
setSelectedCategories(newCategoriesSelected);
3435
}

frontend/src/pages/NewQuestion/index.tsx

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { useState } from "react";
22
import { useNavigate } from "react-router-dom";
3-
import {
4-
Autocomplete,
5-
Button,
6-
IconButton,
7-
Stack,
8-
TextField,
9-
} from "@mui/material";
3+
import { Autocomplete, Button, IconButton, Stack, TextField } from "@mui/material";
104
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
115
import axios from "axios";
126
import { ToastContainer, toast } from "react-toastify";
@@ -25,38 +19,22 @@ const NewQuestion = () => {
2519

2620
const [title, setTitle] = useState<string>("");
2721
const [markdownText, setMarkdownText] = useState<string>("");
28-
const [selectedComplexity, setselectedComplexity] = useState<string | null>(
29-
null
30-
);
22+
const [selectedComplexity, setselectedComplexity] = useState<string | null>(null);
3123
const [selectedCategories, setSelectedCategories] = useState<string[]>([]);
3224
const [uploadedImagesUrl, setUploadedImagesUrl] = useState<string[]>([]);
3325
const [isPreviewQuestion, setIsPreviewQuestion] = useState<boolean>(false);
3426

3527
const handleBack = () => {
36-
if (
37-
title ||
38-
markdownText ||
39-
selectedComplexity ||
40-
selectedCategories.length > 0
41-
) {
42-
if (
43-
!confirm(
44-
"Are you sure you want to leave this page? All process will be lost."
45-
)
46-
) {
28+
if (title || markdownText || selectedComplexity || selectedCategories.length > 0) {
29+
if (!confirm("Are you sure you want to leave this page? All process will be lost.")) {
4730
return;
4831
}
4932
}
5033
navigate("/questions");
5134
};
5235

5336
const handleSubmit = async () => {
54-
if (
55-
!title ||
56-
!markdownText ||
57-
!selectedComplexity ||
58-
selectedCategories.length === 0
59-
) {
37+
if (!title || !markdownText || !selectedComplexity || selectedCategories.length === 0) {
6038
toast.error("Please fill in all fields");
6139
return;
6240
}
@@ -80,8 +58,7 @@ const NewQuestion = () => {
8058
navigate("/questions");
8159
} catch (error) {
8260
if (axios.isAxiosError(error)) {
83-
const message =
84-
error.response?.data.message || "Failed to create question";
61+
const message = error.response?.data.message || "Failed to create question";
8562
toast.error(message);
8663
} else {
8764
toast.error("Failed to create question");
@@ -122,12 +99,11 @@ const NewQuestion = () => {
12299
onChange={(e, newcomplexitySelected) => {
123100
setselectedComplexity(newcomplexitySelected);
124101
}}
125-
renderInput={(params) => (
126-
<TextField {...params} label="Complexity" />
127-
)}
102+
renderInput={(params) => <TextField {...params} label="Complexity" />}
128103
/>
129104

130105
<QuestionCategoryAutoComplete
106+
selectedCategories={selectedCategories}
131107
setSelectedCategories={setSelectedCategories}
132108
/>
133109

@@ -136,10 +112,7 @@ const NewQuestion = () => {
136112
setUploadedImagesUrl={setUploadedImagesUrl}
137113
/>
138114

139-
<QuestionMarkdown
140-
markdownText={markdownText}
141-
setMarkdownText={setMarkdownText}
142-
/>
115+
<QuestionMarkdown markdownText={markdownText} setMarkdownText={setMarkdownText} />
143116
</>
144117
)}
145118

@@ -152,10 +125,7 @@ const NewQuestion = () => {
152125
color="secondary"
153126
fullWidth
154127
disabled={
155-
!title &&
156-
!markdownText &&
157-
!selectedComplexity &&
158-
selectedCategories.length === 0
128+
!title && !markdownText && !selectedComplexity && selectedCategories.length === 0
159129
}
160130
onClick={() => setIsPreviewQuestion((prev) => !prev)}
161131
>

0 commit comments

Comments
 (0)