Skip to content

Commit 4ff9287

Browse files
Merge pull request #685 from userlocalhost/fixed/new-ui/edit-user-password
(WIP) changed to use TextField instad of Input component at EditUserPage
2 parents dc7aec8 + 1b70b59 commit 4ff9287

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

frontend/src/components/user/UserPasswordFormModal.tsx

+45-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
import { Box, Button, Input, Modal, Theme, Typography } from "@mui/material";
1+
import {
2+
Box,
3+
Button,
4+
TextField,
5+
Modal,
6+
Theme,
7+
Typography,
8+
} from "@mui/material";
29
import { makeStyles } from "@mui/styles";
310
import React, { FC, useMemo, useState } from "react";
411
import { useHistory } from "react-router-dom";
512

613
import { DjangoContext } from "../../utils/DjangoContext";
714

8-
import { topPath, usersPath } from "Routes";
15+
import { loginPath, topPath, usersPath } from "Routes";
916
import {
1017
updateUserPassword,
1118
updateUserPasswordAsSuperuser,
@@ -32,6 +39,7 @@ const useStyles = makeStyles<Theme>((theme) => ({
3239
color: "#90A4AE",
3340
},
3441
passwordFieldInput: {
42+
width: "100%",
3543
marginTop: theme.spacing(2),
3644
marginBottom: theme.spacing(2),
3745
},
@@ -60,12 +68,20 @@ export const UserPasswordFormModal: FC<Props> = ({
6068
const [oldPassword, setOldPassword] = useState("");
6169
const [newPassword, setNewPassword] = useState("");
6270
const [checkPassword, setCheckPassword] = useState("");
71+
const [isUnmatch, setIsUnmatch] = useState(false);
6372

6473
const asSuperuser = useMemo(() => {
6574
return DjangoContext.getInstance().user.isSuperuser;
6675
}, []);
6776

6877
const handleSubmit = async () => {
78+
if (newPassword != checkPassword) {
79+
// abort to submit password
80+
setIsUnmatch(true);
81+
82+
return;
83+
}
84+
6985
if (asSuperuser) {
7086
await updateUserPasswordAsSuperuser(userId, newPassword, checkPassword);
7187
} else {
@@ -75,8 +91,12 @@ export const UserPasswordFormModal: FC<Props> = ({
7591
// This calls event handler, which is specified by caller component
7692
onSubmit();
7793

78-
history.replace(topPath());
79-
history.replace(usersPath());
94+
if (DjangoContext.getInstance().user.id == userId) {
95+
history.replace(loginPath());
96+
} else {
97+
history.replace(topPath());
98+
history.replace(usersPath());
99+
}
80100
};
81101

82102
return (
@@ -91,8 +111,9 @@ export const UserPasswordFormModal: FC<Props> = ({
91111
今まで使用していたパスワードをご入力ください。
92112
</label>
93113
</Box>
94-
<Input
114+
<TextField
95115
className={classes.passwordFieldInput}
116+
variant={"standard"}
96117
type="password"
97118
placeholder="Old password"
98119
value={oldPassword}
@@ -107,8 +128,9 @@ export const UserPasswordFormModal: FC<Props> = ({
107128
新しいパスワードをご入力ください。
108129
</label>
109130
</Box>
110-
<Input
131+
<TextField
111132
className={classes.passwordFieldInput}
133+
variant={"standard"}
112134
type="password"
113135
placeholder="New password"
114136
value={newPassword}
@@ -121,17 +143,32 @@ export const UserPasswordFormModal: FC<Props> = ({
121143
確認のためもう一度、新しいパスワードをご入力ください。
122144
</label>
123145
</Box>
124-
<Input
146+
<TextField
147+
error={isUnmatch}
125148
className={classes.passwordFieldInput}
149+
variant={"standard"}
126150
type="password"
127151
placeholder="Confirm new password"
128152
value={checkPassword}
129-
onChange={(e) => setCheckPassword(e.target.value)}
153+
helperText={
154+
isUnmatch ? "新しいパスワードと、入力内容が一致しません" : ""
155+
}
156+
onChange={(e) => {
157+
setCheckPassword(e.target.value);
158+
setIsUnmatch(false);
159+
}}
130160
/>
131161
</Box>
132162

133163
<Box className={classes.buttons}>
134164
<Button
165+
disabled={
166+
(asSuperuser && (!newPassword.length || !checkPassword.length)) ||
167+
(!asSuperuser &&
168+
(!oldPassword.length ||
169+
!newPassword.length ||
170+
!checkPassword.length))
171+
}
135172
type="submit"
136173
variant="contained"
137174
color="secondary"

0 commit comments

Comments
 (0)