Skip to content

Commit 421884e

Browse files
committed
496: Add updateUsers
1 parent 6787bf4 commit 421884e

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { buildNameValidation, buildPasswordValidation, buildRoleValidation, buil
99

1010
export default function NewUserDialog(props) {
1111
const [responseError, setResponseError] = React.useState(undefined);
12-
const { onClose, notifyResult, token } = props;
12+
const { onClose, notifyResult, token, updateUsers } = props;
1313

1414
const validationSchema = Yup.object().shape({
1515
name: buildNameValidation(),
@@ -26,19 +26,20 @@ export default function NewUserDialog(props) {
2626
const onSubmitHandler = (data) => {
2727
setResponseError(null);
2828

29-
const { username, name: full_name, role, password } = data;
29+
const newUser = {
30+
username: data.username,
31+
full_name: data.name,
32+
role: data.role,
33+
password: data.password
34+
};
3035

31-
createUser({
32-
username,
33-
full_name,
34-
role,
35-
password
36-
}, token)
36+
createUser(newUser, token)
3737
.then((res) => {
3838
if (res.indexOf("duplicate key") > -1) {
39-
setResponseError(`User with username ${username} already exists`)
39+
setResponseError(`User with username ${data.username} already exists`)
4040
} else {
4141
notifyResult({ success: true, message: `New user ${res} created successfully` });
42+
updateUsers(newUser);
4243
onClose();
4344
}
4445
})

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const DialogTypes = {
66
}
77

88
export default function UserDialog(props) {
9-
const { onClose, type, notifyResult, token } = props;
9+
const { notifyResult, onClose, type, token, updateUsers } = props;
1010

1111
switch (type) {
1212
case DialogTypes.NewUser:
@@ -15,7 +15,7 @@ export default function UserDialog(props) {
1515
notifyResult={notifyResult}
1616
onClose={onClose}
1717
token={token}
18-
open
18+
updateUsers={updateUsers}
1919
>
2020
"Hello"
2121
</NewUserDialog>

src/client/src/pages/UserManagement/UserManagement.jsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ export default function UserManagement(props) {
4646
}
4747
}
4848

49+
const updateUsers = (newOrUpdatedUser) => {
50+
setUsers(prevUsers => {
51+
const existingUserIndex = _.findIndex(users, existingUser => {
52+
return existingUser.username === newOrUpdatedUser.username;
53+
})
54+
55+
if (existingUserIndex >= 0) {
56+
const updatedUsers = [...prevUsers];
57+
updatedUsers[existingUserIndex] = newOrUpdatedUser;
58+
return updatedUsers;
59+
} else {
60+
return [...users, newOrUpdatedUser];
61+
}
62+
})
63+
}
64+
4965
const openDialog = (opts) => {
5066
setDialogType(opts.type);
5167
setDialogOpen(true);
@@ -112,6 +128,7 @@ export default function UserManagement(props) {
112128
onClose={closeDialog}
113129
token={token}
114130
type={dialogType}
131+
updateUsers={updateUsers}
115132
/>
116133
}
117134
</Container >

0 commit comments

Comments
 (0)