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" ;
2
9
import { makeStyles } from "@mui/styles" ;
3
10
import React , { FC , useMemo , useState } from "react" ;
4
11
import { useHistory } from "react-router-dom" ;
5
12
6
13
import { DjangoContext } from "../../utils/DjangoContext" ;
7
14
8
- import { topPath , usersPath } from "Routes" ;
15
+ import { loginPath , topPath , usersPath } from "Routes" ;
9
16
import {
10
17
updateUserPassword ,
11
18
updateUserPasswordAsSuperuser ,
@@ -32,6 +39,7 @@ const useStyles = makeStyles<Theme>((theme) => ({
32
39
color : "#90A4AE" ,
33
40
} ,
34
41
passwordFieldInput : {
42
+ width : "100%" ,
35
43
marginTop : theme . spacing ( 2 ) ,
36
44
marginBottom : theme . spacing ( 2 ) ,
37
45
} ,
@@ -60,12 +68,20 @@ export const UserPasswordFormModal: FC<Props> = ({
60
68
const [ oldPassword , setOldPassword ] = useState ( "" ) ;
61
69
const [ newPassword , setNewPassword ] = useState ( "" ) ;
62
70
const [ checkPassword , setCheckPassword ] = useState ( "" ) ;
71
+ const [ isUnmatch , setIsUnmatch ] = useState ( false ) ;
63
72
64
73
const asSuperuser = useMemo ( ( ) => {
65
74
return DjangoContext . getInstance ( ) . user . isSuperuser ;
66
75
} , [ ] ) ;
67
76
68
77
const handleSubmit = async ( ) => {
78
+ if ( newPassword != checkPassword ) {
79
+ // abort to submit password
80
+ setIsUnmatch ( true ) ;
81
+
82
+ return ;
83
+ }
84
+
69
85
if ( asSuperuser ) {
70
86
await updateUserPasswordAsSuperuser ( userId , newPassword , checkPassword ) ;
71
87
} else {
@@ -75,8 +91,12 @@ export const UserPasswordFormModal: FC<Props> = ({
75
91
// This calls event handler, which is specified by caller component
76
92
onSubmit ( ) ;
77
93
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
+ }
80
100
} ;
81
101
82
102
return (
@@ -91,8 +111,9 @@ export const UserPasswordFormModal: FC<Props> = ({
91
111
今まで使用していたパスワードをご入力ください。
92
112
</ label >
93
113
</ Box >
94
- < Input
114
+ < TextField
95
115
className = { classes . passwordFieldInput }
116
+ variant = { "standard" }
96
117
type = "password"
97
118
placeholder = "Old password"
98
119
value = { oldPassword }
@@ -107,8 +128,9 @@ export const UserPasswordFormModal: FC<Props> = ({
107
128
新しいパスワードをご入力ください。
108
129
</ label >
109
130
</ Box >
110
- < Input
131
+ < TextField
111
132
className = { classes . passwordFieldInput }
133
+ variant = { "standard" }
112
134
type = "password"
113
135
placeholder = "New password"
114
136
value = { newPassword }
@@ -121,17 +143,32 @@ export const UserPasswordFormModal: FC<Props> = ({
121
143
確認のためもう一度、新しいパスワードをご入力ください。
122
144
</ label >
123
145
</ Box >
124
- < Input
146
+ < TextField
147
+ error = { isUnmatch }
125
148
className = { classes . passwordFieldInput }
149
+ variant = { "standard" }
126
150
type = "password"
127
151
placeholder = "Confirm new password"
128
152
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
+ } }
130
160
/>
131
161
</ Box >
132
162
133
163
< Box className = { classes . buttons } >
134
164
< Button
165
+ disabled = {
166
+ ( asSuperuser && ( ! newPassword . length || ! checkPassword . length ) ) ||
167
+ ( ! asSuperuser &&
168
+ ( ! oldPassword . length ||
169
+ ! newPassword . length ||
170
+ ! checkPassword . length ) )
171
+ }
135
172
type = "submit"
136
173
variant = "contained"
137
174
color = "secondary"
0 commit comments