settings.jsx
2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { useState, useEffect } from "react";
import { Navigate, useNavigate } from "react-router-dom";
import authService from "../service/auth";
// components
import Topbar from "../components/Topbar";
import Bottombar from "../components/Bottombar";
// styles
import "../styles/layout.scss";
function Settings() {
const token = sessionStorage.getItem("user-token");
const navigate = useNavigate();
const [logoutSuccess, setLogoutSuccess] = useState(0);
const [userinfo, setUserinfo] = useState({
username: "",
country: "",
city: "",
email: "",
});
useEffect(() => {
if (logoutSuccess === "logout") {
sessionStorage.clear();
navigate("/login", { replace: true });
}
}, [logoutSuccess, setLogoutSuccess, navigate]);
const onChange = (e) => {
setUserinfo((orgState) => ({
...orgState,
[e.target.id]: e.target.value,
}));
};
const handleSubmit = async (e) => {
e.preventDefault();
if (userinfo.country.length > 2) {
alert("Please use alpha-2 country code");
return;
}
const response = await authService.handleUserEdit(userinfo);
if (response === "success") {
alert("User info successfully changed");
}
};
if (!token) {
return <Navigate to="/login" />;
}
return (
<div>
<Topbar />
<div className="mainBox">
<h3>Edit Your information</h3>
<form className="authForm" onSubmit={(e) => handleSubmit(e)}>
<label htmlFor="username">
<input
placeholder="Username"
onChange={(e) => onChange(e)}
value={userinfo.username}
type="text"
id="username"
/>
</label>
<label htmlFor="country">
<input
placeholder="Alpha-2 Country Code"
onChange={(e) => onChange(e)}
value={userinfo.country.toUpperCase()}
type="text"
id="country"
/>
</label>
<label htmlFor="city">
<input
placeholder="City Name"
onChange={(e) => onChange(e)}
value={userinfo.city.toLowerCase()}
type="text"
id="city"
/>
</label>
<label htmlFor="email">
<input
placeholder="email"
onChange={(e) => onChange(e)}
value={userinfo.email}
type="email"
id="email"
/>
</label>
<label className="submitBtn" htmlFor="submit">
<input type="submit" id="submit" />
</label>
</form>
<br />
<div>
Want to logout?{" "}
<button
className="logoutBtn"
onClick={() => setLogoutSuccess("logout")}
>
Logout
</button>
</div>
</div>
<Bottombar />
</div>
);
}
export default Settings;