Skip to content

Commit 2c50dd0

Browse files
committed
Fix: Type safety when accessing userInfo from localstorage & mistake in merge
1 parent 2a09543 commit 2c50dd0

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ export function UserMenu() {
5858
const dispatch = useDispatch();
5959
const [logout] = useLogoutMutation();
6060
const navigate = useNavigate();
61-
const user_metadata = JSON.parse(localStorage.getItem("userInfo")); //User Data
62-
const initials = user_metadata.user.user_metadata.username //Gets User Initials
61+
const user_metadata = JSON.parse(localStorage.getItem("userInfo") ?? "{}"); //User Data
62+
const username = (user_metadata?.user?.user_metadata?.username as string) ?? "John Doe"
63+
const initials = username//Gets User Initials
6364
.split(" ") // Split the string by spaces
6465
.map(word => word[0]) // Take the first letter of each word
6566
.join("") // Join them back into a string
@@ -79,7 +80,7 @@ export function UserMenu() {
7980
<DropdownMenu>
8081
<DropdownMenuTrigger>
8182
<div className="flex flex-row items-center gap-4 px-4 text-sm">
82-
{user_metadata.user.user_metadata.username}
83+
{initials}
8384
<Avatar>
8485
{/* Avatar Image is the profile picture of the user. The default avatar is used as a placeholder for now. */}
8586
<AvatarImage src="../../public/img/default-avatar.png" />

course-matrix/frontend/src/models/signup-form.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type SignupForm = {
88
}
99

1010
export const SignupFormSchema: ZodType<SignupForm> = z.object({
11-
username: z.string().min(1, "Please enter a Username"),
11+
username: z.string().min(1, "Please enter a Username").max(50, "Username must be 50 characters or less"),
1212
email: z.string().min(1, "Please enter an email").email("Invalid email"),
1313
password: z.string()
1414
.min(8, "Password must be at least 8 characters")

course-matrix/frontend/src/pages/Signup/SignUpPage.tsx

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,23 @@ const SignupPage = () => {
9797
setIsSubmitting(false);
9898
};
9999

100-
return <div className="w-screen flex flex-col gap-4 justify-center -translate-y-12 items-center h-screen bg-gray-100">
100+
return (
101+
<div className="w-screen flex flex-col gap-4 justify-center -translate-y-12 items-center h-screen bg-gray-100">
101102
<Logo />
102103
<Card className="flex flex-col items-center text-center p-8">
103104
<div className="flex flex-col gap-4">
104105
<h1 className="text-xl font-semibold">Sign up</h1>
105106
<h2 className="text-sm text-slate-500">Sign up to Course Matrix with your email</h2>
106107
<Form {...signupForm}>
107108
<form onSubmit={signupForm.handleSubmit(onSubmit)} className="space-y-4">
108-
<FormField
109+
<FormField
109110
control={signupForm.control}
110111
name="username"
111112
render={({ field }) => (
112113
<FormItem className="text-left w-[400px]">
113114
<FormLabel>Username</FormLabel>
114115
<FormControl>
115-
<Input {...field} id="UsernameInput" placeholder="User123"/>
116+
<Input {...field} id="UsernameInput"/>
116117
</FormControl>
117118
<FormMessage/>
118119
</FormItem>
@@ -130,18 +131,29 @@ const SignupPage = () => {
130131
<FormMessage/>
131132
</FormItem>
132133
)}
133-
<div className="w-full flex flex-row justify-center">
134-
<Button
135-
id="LoginBtn"
136-
className="w-full"
137-
type="submit"
138-
variant={isSubmitting ? "ghost" : "default"}
139-
>
140-
Sign up
141-
</Button>
142-
</div>
143-
</form>
144-
</Form>
134+
/>
135+
<PasswordInput
136+
form={signupForm}
137+
name="password"
138+
label="Create Password"
139+
placeholder="Enter password"
140+
className="w-full"
141+
/>
142+
<PasswordInput
143+
form={signupForm}
144+
name="confirmPassword"
145+
label="Confirm Password"
146+
placeholder="Confirm password"
147+
className="w-full"
148+
/>
149+
{showCheckEmail && <p className="text-sm text-slate-500 w-[400px]">Registration successful! Please check {signupForm.getValues("email")} for a confirmation link</p>}
150+
{showUserExists && <p className="text-sm text-red-500 w-[400px]">User with email {signupForm.getValues("email")} already exists!</p>}
151+
{showSignupError && <p className="text-sm text-red-500 w-[400px]">An unknown error occured. Please try again.</p>}
152+
<div className="w-full flex flex-row justify-center">
153+
<Button id="LoginBtn" className="w-full" type="submit" variant={isSubmitting ? "ghost" : "default"}>Sign up</Button>
154+
</div>
155+
</form>
156+
</Form>
145157

146158
<Label
147159
id="GogtoSignup"

0 commit comments

Comments
 (0)