Skip to content

Commit ed4a46b

Browse files
authored
Ax/scrum 131 Fix Text Highlight Bug for Edit Username Flow (#133)
Co-authored-by: Austin-X <[email protected]>
1 parent dcc4eda commit ed4a46b

File tree

3 files changed

+92
-55
lines changed

3 files changed

+92
-55
lines changed

course-matrix/frontend/src/components/UserMenu.tsx

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ import { useEffect, useState, useRef } from "react";
5959
* @returns {JSX.Element} The rendered user menu dropdown with account options.
6060
*/
6161

62-
export function UserMenu() {
62+
interface UserMenuProps {
63+
setOpen: (open: boolean) => void;
64+
}
65+
66+
export function UserMenu({ setOpen }: UserMenuProps) {
6367
const dispatch = useDispatch();
6468
const [logout] = useLogoutMutation();
6569
const navigate = useNavigate();
@@ -100,24 +104,6 @@ export function UserMenu() {
100104
}
101105
};
102106

103-
const handleUsernameUpdate = async () => {
104-
try {
105-
const username = usernameRef?.current?.value;
106-
if (!username || !username.trim()) {
107-
return;
108-
}
109-
user_metadata.user.user_metadata.username =
110-
usernameRef.current?.value.trimEnd();
111-
localStorage.setItem("userInfo", JSON.stringify(user_metadata));
112-
await usernameUpdate({
113-
userId: userId,
114-
username: user_metadata.user.user_metadata.username,
115-
});
116-
} catch (err) {
117-
console.error("Update username failed: ", err);
118-
}
119-
};
120-
121107
return (
122108
<DropdownMenu>
123109
<DropdownMenuTrigger>
@@ -138,41 +124,10 @@ export function UserMenu() {
138124
{user_metadata?.user?.user_metadata?.email}
139125
</p>
140126
</div>
141-
<DropdownMenuItem onSelect={(e) => e.preventDefault()}>
142-
<Dialog>
143-
<DialogTrigger asChild>
144-
<button className="w-full text-left">Edit Account</button>
145-
</DialogTrigger>
146-
<DialogContent className="gap-5">
147-
<DialogHeader>
148-
<DialogTitle>Edit Account</DialogTitle>
149-
<DialogDescription>
150-
Edit your account details.
151-
</DialogDescription>
152-
</DialogHeader>
153-
<Label htmlFor="username">New User Name</Label>
154-
{/* Disable this email input box for now until we have the backend for accounts set up */}
155-
<Input
156-
id="username"
157-
type="text"
158-
placeholder={user_metadata.user.user_metadata.username}
159-
ref={usernameRef}
160-
onKeyDown={(e) => {
161-
if (e.key === " ") {
162-
e.stopPropagation(); // Allows space input
163-
}
164-
}}
165-
/>
166-
<DialogFooter>
167-
<DialogClose asChild>
168-
<Button variant="secondary">Cancel</Button>
169-
</DialogClose>
170-
<DialogClose asChild>
171-
<Button onClick={handleUsernameUpdate}>Save</Button>
172-
</DialogClose>
173-
</DialogFooter>
174-
</DialogContent>
175-
</Dialog>
127+
<DropdownMenuItem>
128+
<button className="w-full text-left" onClick={() => setOpen(true)}>
129+
Edit Account
130+
</button>
176131
</DropdownMenuItem>
177132
<DropdownMenuItem>
178133
<button className="w-full text-left" onClick={handleLogout}>

course-matrix/frontend/src/pages/Dashboard/Dashboard.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import AssistantPage from "../Assistant/AssistantPage";
2020
import { RuntimeProvider } from "../Assistant/runtime-provider";
2121
import Home from "../Home/Home";
2222
import { CompareTimetables } from "../Compare/CompareTimetables";
23+
import EditAccountDialog from "./EditAccountDialog";
24+
import { useState } from "react";
2325

2426
/**
2527
* Dashboard Component
@@ -38,6 +40,7 @@ import { CompareTimetables } from "../Compare/CompareTimetables";
3840
*/
3941
const Dashboard = () => {
4042
const location = useLocation();
43+
const [openEditAccountDialog, setOpenEditAccountDialog] = useState(false);
4144

4245
return (
4346
<>
@@ -79,7 +82,13 @@ const Dashboard = () => {
7982
</BreadcrumbList>
8083
</Breadcrumb>
8184
</div>
82-
<UserMenu />
85+
<div>
86+
<UserMenu setOpen={setOpenEditAccountDialog} />
87+
<EditAccountDialog
88+
open={openEditAccountDialog}
89+
setOpen={setOpenEditAccountDialog}
90+
/>
91+
</div>
8392
</header>
8493
<div>
8594
<Routes>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import {
2+
Dialog,
3+
DialogContent,
4+
DialogHeader,
5+
DialogFooter,
6+
DialogTitle,
7+
DialogDescription,
8+
DialogClose,
9+
} from "@/components/ui/dialog";
10+
import { Button } from "@/components/ui/button";
11+
import { Input } from "@/components/ui/input";
12+
import { Label } from "@/components/ui/label";
13+
import { useUpdateUsernameMutation } from "@/api/authApiSlice";
14+
import { useRef } from "react";
15+
16+
interface EditAccountDialogProps {
17+
open: boolean;
18+
setOpen: (open: boolean) => void;
19+
}
20+
21+
const EditAccountDialog = ({ open, setOpen }: EditAccountDialogProps) => {
22+
const [updateUsername] = useUpdateUsernameMutation();
23+
24+
const usernameRef = useRef<HTMLInputElement>(null);
25+
const user_metadata = JSON.parse(localStorage.getItem("userInfo") ?? "{}"); //User Data
26+
const userId = user_metadata.user.id;
27+
28+
const handleUsernameUpdate = async () => {
29+
try {
30+
const username = usernameRef?.current?.value;
31+
if (!username || !username.trim()) {
32+
return;
33+
}
34+
user_metadata.user.user_metadata.username =
35+
usernameRef.current?.value.trimEnd();
36+
localStorage.setItem("userInfo", JSON.stringify(user_metadata));
37+
await updateUsername({
38+
userId: userId,
39+
username: user_metadata.user.user_metadata.username,
40+
});
41+
} catch (err) {
42+
console.error("Update username failed: ", err);
43+
}
44+
};
45+
46+
return (
47+
<Dialog open={open} onOpenChange={setOpen}>
48+
<DialogContent className="gap-5">
49+
<DialogHeader>
50+
<DialogTitle>Edit Account</DialogTitle>
51+
<DialogDescription>Edit your account details.</DialogDescription>
52+
</DialogHeader>
53+
<Label htmlFor="username">New User Name</Label>
54+
<Input
55+
id="username"
56+
type="text"
57+
placeholder={user_metadata.user.user_metadata.username}
58+
ref={usernameRef}
59+
/>
60+
<DialogFooter>
61+
<DialogClose asChild>
62+
<Button variant="secondary">Cancel</Button>
63+
</DialogClose>
64+
<DialogClose asChild>
65+
<Button onClick={handleUsernameUpdate}>Save</Button>
66+
</DialogClose>
67+
</DialogFooter>
68+
</DialogContent>
69+
</Dialog>
70+
);
71+
};
72+
73+
export default EditAccountDialog;

0 commit comments

Comments
 (0)