Skip to content

Commit 5c2c575

Browse files
committed
Fix bug in category autocomplete, add isAdmin to user token
1 parent 0c5f684 commit 5c2c575

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
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: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { useState } from "react";
12
import { Autocomplete, Chip, TextField } from "@mui/material";
23
import { createFilterOptions } from "@mui/material/Autocomplete";
34
import { categoryList } from "../../utils/constants";
45

56
interface QuestionCategoryAutoCompleteProps {
67
selectedCategories?: string[];
7-
setSelectedCategories: (value: string[]) => void;
8+
setSelectedCategories: React.Dispatch<React.SetStateAction<string[]>>;
89
}
910

1011
const QuestionCategoryAutoComplete: React.FC<QuestionCategoryAutoCompleteProps> = ({
@@ -14,6 +15,10 @@ const QuestionCategoryAutoComplete: React.FC<QuestionCategoryAutoCompleteProps>
1415
// TODO
1516
// Fetch category list from the server
1617

18+
// copy is created to ensure that Autocomplete rerenders when the selectedCategories change
19+
const [selectedCategoriesCopy, setSelectedCategoriesCopy] = useState<string[]>(
20+
selectedCategories || []
21+
);
1722
const filter = createFilterOptions<string>();
1823

1924
return (
@@ -23,13 +28,16 @@ const QuestionCategoryAutoComplete: React.FC<QuestionCategoryAutoCompleteProps>
2328
options={categoryList}
2429
size="small"
2530
sx={{ marginTop: 2 }}
26-
value={selectedCategories}
31+
value={selectedCategoriesCopy}
2732
onChange={(e, newCategoriesSelected) => {
2833
const newValue = newCategoriesSelected[newCategoriesSelected.length - 1];
2934
if (typeof newValue === "string" && newValue.startsWith(`Add: "`)) {
30-
categoryList.push(newValue.slice(6, -1));
31-
setSelectedCategories([...newCategoriesSelected.slice(0, -1), newValue.slice(6, -1)]);
35+
const newCategory = newValue.slice(6, -1);
36+
categoryList.push(newCategory);
37+
setSelectedCategoriesCopy((prev) => [...prev, newCategory]);
38+
setSelectedCategories((prev) => [...prev, newCategory]);
3239
} else {
40+
setSelectedCategoriesCopy(newCategoriesSelected);
3341
setSelectedCategories(newCategoriesSelected);
3442
}
3543
}}

0 commit comments

Comments
 (0)