|
| 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 | +} |
0 commit comments