Skip to content

Commit 9336c5b

Browse files
committed
496: Add UpdateUserDialog
1 parent 456ca1d commit 9336c5b

File tree

4 files changed

+137
-4
lines changed

4 files changed

+137
-4
lines changed

src/client/src/pages/UserManagement/Components/Dialog/NewUserDialog.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,4 @@ export default function NewUserDialog(props) {
132132
</form>
133133
</Dialog>
134134
)
135-
136-
}
135+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { yupResolver } from "@hookform/resolvers/yup";
2+
import { Button, Checkbox, Dialog, DialogActions, DialogContent, DialogTitle, FormControlLabel, TextField, Typography } from '@material-ui/core';
3+
import React from 'react';
4+
import { useForm } from 'react-hook-form';
5+
import * as Yup from 'yup';
6+
import { updateUser } from "../../../../utils/api";
7+
import { buildNameValidation, buildRoleValidation } from '../../Validations';
8+
9+
10+
export default function UpdateUserDialog(props) {
11+
const { onClose, notifyResult, token, updateUsers, user } = props;
12+
const { username, full_name: name, role, active } = user;
13+
14+
const validationSchema = Yup.object().shape({
15+
name: buildNameValidation(),
16+
role: buildRoleValidation(),
17+
active: Yup.boolean(),
18+
});
19+
20+
const { register, handleSubmit, formState: { errors }, trigger } = useForm({
21+
resolver: yupResolver(validationSchema),
22+
});
23+
24+
const onSubmitHandler = (data) => {
25+
const newUser = {
26+
username,
27+
full_name: data.name,
28+
role: data.role,
29+
active: data.active ? "Y" : "N"
30+
};
31+
32+
updateUser(newUser, token)
33+
.then((res) => {
34+
if (res === "Updated") {
35+
notifyResult({ success: true, message: `User ${username} updated successfully` });
36+
updateUsers(newUser);
37+
onClose();
38+
} else {
39+
notifyResult({ success: false, message: res })
40+
}
41+
})
42+
.catch(e => {
43+
console.warn(e)
44+
notifyResult({ success: false, message: e })
45+
})
46+
}
47+
48+
return (
49+
<Dialog
50+
hideBackdrop
51+
fullWidth
52+
open
53+
>
54+
<DialogTitle style={{ fontSize: "20px" }}>Update user</DialogTitle>
55+
<form onSubmit={handleSubmit(onSubmitHandler)}>
56+
<DialogContent>
57+
<TextField
58+
{...register("name")}
59+
defaultValue={name}
60+
margin="dense"
61+
id="name-input"
62+
label="Name"
63+
onBlur={() => trigger("name")}
64+
variant="standard"
65+
autoFocus
66+
fullWidth
67+
/>
68+
{errors.name &&
69+
<Typography color="error">{errors.name.message}</Typography>
70+
}
71+
<TextField
72+
defaultValue={username}
73+
margin="dense"
74+
id="username-input"
75+
label="Username"
76+
onBlur={() => trigger("username")}
77+
variant="standard"
78+
disabled
79+
fullWidth
80+
/>
81+
{(errors.username) &&
82+
<Typography color="error">{errors.username.message}</Typography>
83+
}
84+
<TextField
85+
{...register("role")}
86+
defaultValue={role}
87+
margin="dense"
88+
id="role-input"
89+
label="Role - user/editor/admin"
90+
onBlur={() => trigger("role")}
91+
variant="standard"
92+
fullWidth
93+
/>
94+
{errors.role &&
95+
<Typography color="error">{errors.role.message}</Typography>
96+
}
97+
<FormControlLabel
98+
control={
99+
<Checkbox
100+
{...register("active")}
101+
defaultChecked={active === "Y" ? true : false}
102+
/>
103+
}
104+
label="Active"
105+
/>
106+
<DialogActions>
107+
<Button
108+
onClick={onClose}
109+
>
110+
Cancel
111+
</Button>
112+
<Button
113+
type="submit"
114+
>
115+
Submit
116+
</Button>
117+
</DialogActions>
118+
</DialogContent>
119+
</form>
120+
</Dialog>
121+
)
122+
}

src/client/src/pages/UserManagement/Components/Dialog/UserDialog.jsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import React from 'react';
22
import ChangePasswordDialog from './ChangePasswordDialog';
33
import NewUserDialog from './NewUserDialog';
4+
import UpdateUserDialog from './UpdateUserDialog';
45

56
export const DialogTypes = {
67
NewUser: 'new-user',
8+
UpdateUser: 'update-user',
79
ChangePassword: 'change-password',
810
}
911

@@ -27,13 +29,23 @@ export default function UserDialog(props) {
2729
updateUsers={updateUsers}
2830
/>
2931
)
32+
case DialogTypes.UpdateUser:
33+
return (
34+
<UpdateUserDialog
35+
notifyResult={notifyResult}
36+
onClose={onClose}
37+
token={token}
38+
updateUsers={updateUsers}
39+
user={selectedUser}
40+
/>
41+
)
3042
case DialogTypes.ChangePassword:
3143
return (
3244
<ChangePasswordDialog
3345
notifyResult={notifyResult}
3446
onClose={onClose}
35-
user={selectedUser}
3647
token={token}
48+
user={selectedUser}
3749
/>
3850
)
3951
default:

src/client/src/pages/UserManagement/Components/UserRow.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default function UserRow(props) {
1717
<TableCell>{role}</TableCell>
1818
<TableCell>{active === 'Y' ? 'Yes' : 'No'}</TableCell>
1919
<TableCell>
20-
<Button>
20+
<Button onClick={() => openDialog({ type: DialogTypes.UpdateUser, user: props.user })}>
2121
Update User
2222
</Button>
2323
</TableCell>

0 commit comments

Comments
 (0)