Skip to content

Commit d3328d6

Browse files
committed
Proper data handling for account view
1 parent f69e133 commit d3328d6

File tree

1 file changed

+48
-16
lines changed

1 file changed

+48
-16
lines changed

Diff for: apps/template/src/views/Account.tsx

+48-16
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,46 @@
1-
import { useContext } from "react";
1+
import { useContext, useState } from "react";
22
import { SessionContext } from "../Session";
33
import { Header, Input, Spacer } from "../components";
44

5+
export type ProfileData = {
6+
name: string;
7+
email: string;
8+
};
9+
export const DEFAULT_PROFILE_DATA: ProfileData = {
10+
name: "",
11+
email: "",
12+
};
13+
14+
export type PasswordData = {
15+
current_password: string;
16+
new_password1: string;
17+
new_password2: string;
18+
};
19+
20+
export const DEFAULT_PASSWORD_DATA: PasswordData = {
21+
current_password: "",
22+
new_password1: "",
23+
new_password2: "",
24+
};
25+
526
export function Account() {
627
const { session } = useContext(SessionContext);
7-
const setValue = (value: string) => {
8-
console.info(value);
28+
const [profile, setProfile] = useState<ProfileData>(DEFAULT_PROFILE_DATA);
29+
const [password, setPassword] = useState<PasswordData>(DEFAULT_PASSWORD_DATA);
30+
31+
const storeProfile = (key: keyof ProfileData, value: string) => {
32+
setProfile({ ...profile, [key]: value });
33+
};
34+
const saveProfile = () => {
35+
console.log(profile);
36+
};
37+
38+
const storePassword = (key: keyof PasswordData, value: string) => {
39+
setPassword({ ...password, [key]: value });
40+
};
41+
const savePassword = () => {
42+
console.log(password);
943
};
10-
const saveProfile = () => {};
11-
const updatePassword = () => {};
1244

1345
return (
1446
<>
@@ -17,14 +49,14 @@ export function Account() {
1749
<Input
1850
name="name"
1951
label="Name"
20-
value={session.user?.name ?? ""}
21-
onChange={(value) => setValue(value)}
52+
value={profile.name}
53+
onChange={(value) => storeProfile("name", value)}
2254
/>
2355
<Input
2456
name="email"
2557
label="Email"
26-
value={session.user?.email ?? ""}
27-
onChange={(value) => setValue(value)}
58+
value={profile.email}
59+
onChange={(value) => storeProfile("email", value)}
2860
/>
2961
<button onClick={saveProfile}>Save</button>
3062

@@ -34,25 +66,25 @@ export function Account() {
3466
name="current_password"
3567
type="password"
3668
label="Current Password"
37-
value={""}
38-
onChange={(value) => setValue(value)}
69+
value={password.current_password ?? ""}
70+
onChange={(value) => storePassword("current_password", value)}
3971
/>
4072
<Input
4173
name="new_password1"
4274
type="password"
4375
label="New Password"
44-
value={""}
45-
onChange={(value) => setValue(value)}
76+
value={password.new_password1 ?? ""}
77+
onChange={(value) => storePassword("new_password1", value)}
4678
/>
4779
<Input
4880
name="new_password2"
4981
type="password"
5082
label="Repeat New Password"
51-
value={""}
52-
onChange={(value) => setValue(value)}
83+
value={password.new_password2 ?? ""}
84+
onChange={(value) => storePassword("new_password2", value)}
5385
/>
5486

55-
<button onClick={updatePassword}>Update</button>
87+
<button onClick={savePassword}>Update</button>
5688
</>
5789
);
5890
}

0 commit comments

Comments
 (0)