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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
import React, { useState } from "react";
import { useHistory } from "react-router";
import { useTranslation } from "react-i18next";
import { Container, Form, Row, Col, Button, Modal } from "react-bootstrap";
import { useUser, userService } from "@/services/user";
import OperationDialog from "../common/OperationDialog";
interface ChangePasswordDialogProps {
open: boolean;
close: () => void;
}
const ChangePasswordDialog: React.FC<ChangePasswordDialogProps> = (props) => {
const history = useHistory();
const [redirect, setRedirect] = useState<boolean>(false);
return (
<OperationDialog
open={props.open}
title="settings.dialogChangePassword.title"
themeColor="danger"
inputPrompt="settings.dialogChangePassword.prompt"
inputScheme={[
{
type: "text",
label: "settings.dialogChangePassword.inputOldPassword",
password: true,
},
{
type: "text",
label: "settings.dialogChangePassword.inputNewPassword",
password: true,
},
{
type: "text",
label: "settings.dialogChangePassword.inputRetypeNewPassword",
password: true,
},
]}
inputValidator={([oldPassword, newPassword, retypedNewPassword]) => {
const result: Record<number, string> = {};
if (oldPassword === "") {
result[0] = "settings.dialogChangePassword.errorEmptyOldPassword";
}
if (newPassword === "") {
result[1] = "settings.dialogChangePassword.errorEmptyNewPassword";
}
if (retypedNewPassword !== newPassword) {
result[2] = "settings.dialogChangePassword.errorRetypeNotMatch";
}
return result;
}}
onProcess={async ([oldPassword, newPassword]) => {
await userService.changePassword(oldPassword, newPassword);
setRedirect(true);
}}
close={() => {
props.close();
if (redirect) {
history.push("/login");
}
}}
/>
);
};
const ConfirmLogoutDialog: React.FC<{
onClose: () => void;
onConfirm: () => void;
}> = ({ onClose, onConfirm }) => {
const { t } = useTranslation();
return (
<Modal show centered onHide={onClose}>
<Modal.Header>
<Modal.Title className="text-danger">
{t("settings.dialogConfirmLogout.title")}
</Modal.Title>
</Modal.Header>
<Modal.Body>{t("settings.dialogConfirmLogout.prompt")}</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={onClose}>
{t("operationDialog.cancel")}
</Button>
<Button variant="danger" onClick={onConfirm}>
{t("operationDialog.confirm")}
</Button>
</Modal.Footer>
</Modal>
);
};
const SettingsPage: React.FC = (_) => {
const { i18n, t } = useTranslation();
const user = useUser();
const history = useHistory();
const [dialog, setDialog] = useState<null | "changepassword" | "logout">(
null
);
const language = i18n.language.slice(0, 2);
return (
<>
<Container>
{user ? (
<div className="cru-card my-3 py-3">
<h3 className="px-3 mb-3 text-primary">
{t("settings.subheaders.account")}
</h3>
<div
className="settings-item clickable first"
onClick={() => {
history.push(`/users/${user.username}`);
}}
>
{t("settings.gotoSelf")}
</div>
<div
className="settings-item clickable text-danger"
onClick={() => setDialog("changepassword")}
>
{t("settings.changePassword")}
</div>
<div
className="settings-item clickable text-danger"
onClick={() => {
setDialog("logout");
}}
>
{t("settings.logout")}
</div>
</div>
) : null}
<div className="cru-card my-3 py-3">
<h3 className="px-3 mb-3 text-primary">
{t("settings.subheaders.customization")}
</h3>
<Row className="settings-item first mx-0">
<Col xs="12" sm="auto">
<div>{t("settings.languagePrimary")}</div>
<small className="d-block text-secondary">
{t("settings.languageSecondary")}
</small>
</Col>
<Col xs="auto" className="ml-auto">
<Form.Control
as="select"
value={language}
onChange={(e) => {
void i18n.changeLanguage(e.target.value);
}}
>
<option value="zh">中文</option>
<option value="en">English</option>
</Form.Control>
</Col>
</Row>
</div>
</Container>
{(() => {
switch (dialog) {
case "changepassword":
return (
<ChangePasswordDialog
open
close={() => {
setDialog(null);
}}
/>
);
case "logout":
return (
<ConfirmLogoutDialog
onClose={() => setDialog(null)}
onConfirm={() => {
void userService.logout().then(() => {
history.push("/");
});
}}
/>
);
default:
return null;
}
})()}
</>
);
};
export default SettingsPage;
|