2024-03-08 14:58:36 +01:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Checkbox,
|
|
|
|
Flex,
|
|
|
|
FormControl,
|
|
|
|
FormErrorMessage,
|
|
|
|
FormLabel,
|
|
|
|
Input,
|
|
|
|
Modal,
|
|
|
|
ModalBody,
|
|
|
|
ModalCloseButton,
|
|
|
|
ModalContent,
|
|
|
|
ModalFooter,
|
|
|
|
ModalHeader,
|
|
|
|
ModalOverlay,
|
2024-03-17 17:28:45 +01:00
|
|
|
} from "@chakra-ui/react"
|
2024-03-28 21:15:11 -05:00
|
|
|
import { type SubmitHandler, useForm } from "react-hook-form"
|
2024-03-17 17:28:45 +01:00
|
|
|
import { useMutation, useQueryClient } from "react-query"
|
2024-02-26 09:39:09 -05:00
|
|
|
|
2024-03-17 17:28:45 +01:00
|
|
|
import {
|
|
|
|
type ApiError,
|
|
|
|
type UserOut,
|
|
|
|
type UserUpdate,
|
2024-03-28 21:15:11 -05:00
|
|
|
UsersService,
|
2024-03-17 17:28:45 +01:00
|
|
|
} from "../../client"
|
|
|
|
import useCustomToast from "../../hooks/useCustomToast"
|
2024-03-28 19:49:02 -05:00
|
|
|
import { emailPattern } from "../../utils"
|
2024-02-26 09:39:09 -05:00
|
|
|
|
|
|
|
interface EditUserProps {
|
2024-03-08 14:58:36 +01:00
|
|
|
user: UserOut
|
|
|
|
isOpen: boolean
|
|
|
|
onClose: () => void
|
2024-02-26 09:39:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
interface UserUpdateForm extends UserUpdate {
|
2024-03-08 14:58:36 +01:00
|
|
|
confirm_password: string
|
2024-02-26 09:39:09 -05:00
|
|
|
}
|
|
|
|
|
2024-03-28 20:22:28 -05:00
|
|
|
const EditUser = ({ user, isOpen, onClose }: EditUserProps) => {
|
2024-03-08 14:58:36 +01:00
|
|
|
const queryClient = useQueryClient()
|
|
|
|
const showToast = useCustomToast()
|
2024-03-07 19:16:23 +01:00
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
|
|
|
reset,
|
|
|
|
getValues,
|
|
|
|
formState: { errors, isSubmitting, isDirty },
|
|
|
|
} = useForm<UserUpdateForm>({
|
2024-03-17 17:28:45 +01:00
|
|
|
mode: "onBlur",
|
|
|
|
criteriaMode: "all",
|
2024-03-08 14:58:36 +01:00
|
|
|
defaultValues: user,
|
|
|
|
})
|
2024-02-28 18:30:59 -05:00
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
const updateUser = async (data: UserUpdateForm) => {
|
|
|
|
await UsersService.updateUser({ userId: user.id, requestBody: data })
|
|
|
|
}
|
2024-02-26 09:39:09 -05:00
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
const mutation = useMutation(updateUser, {
|
|
|
|
onSuccess: () => {
|
2024-03-17 17:28:45 +01:00
|
|
|
showToast("Success!", "User updated successfully.", "success")
|
2024-03-08 14:58:36 +01:00
|
|
|
onClose()
|
|
|
|
},
|
|
|
|
onError: (err: ApiError) => {
|
2024-03-13 22:59:28 +01:00
|
|
|
const errDetail = err.body?.detail
|
2024-03-17 17:28:45 +01:00
|
|
|
showToast("Something went wrong.", `${errDetail}`, "error")
|
2024-03-08 14:58:36 +01:00
|
|
|
},
|
|
|
|
onSettled: () => {
|
2024-03-17 17:28:45 +01:00
|
|
|
queryClient.invalidateQueries("users")
|
2024-03-08 14:58:36 +01:00
|
|
|
},
|
|
|
|
})
|
2024-03-07 19:16:23 +01:00
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
const onSubmit: SubmitHandler<UserUpdateForm> = async (data) => {
|
2024-03-17 17:28:45 +01:00
|
|
|
if (data.password === "") {
|
2024-03-13 22:59:28 +01:00
|
|
|
data.password = undefined
|
2024-02-26 09:39:09 -05:00
|
|
|
}
|
2024-03-08 14:58:36 +01:00
|
|
|
mutation.mutate(data)
|
|
|
|
}
|
2024-02-26 09:39:09 -05:00
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
const onCancel = () => {
|
|
|
|
reset()
|
|
|
|
onClose()
|
|
|
|
}
|
2024-02-26 09:39:09 -05:00
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Modal
|
|
|
|
isOpen={isOpen}
|
|
|
|
onClose={onClose}
|
2024-03-17 17:28:45 +01:00
|
|
|
size={{ base: "sm", md: "md" }}
|
2024-03-08 14:58:36 +01:00
|
|
|
isCentered
|
|
|
|
>
|
|
|
|
<ModalOverlay />
|
|
|
|
<ModalContent as="form" onSubmit={handleSubmit(onSubmit)}>
|
|
|
|
<ModalHeader>Edit User</ModalHeader>
|
|
|
|
<ModalCloseButton />
|
|
|
|
<ModalBody pb={6}>
|
|
|
|
<FormControl isInvalid={!!errors.email}>
|
|
|
|
<FormLabel htmlFor="email">Email</FormLabel>
|
|
|
|
<Input
|
|
|
|
id="email"
|
2024-03-17 17:28:45 +01:00
|
|
|
{...register("email", {
|
|
|
|
required: "Email is required",
|
2024-03-28 19:49:02 -05:00
|
|
|
pattern: emailPattern,
|
2024-03-08 14:58:36 +01:00
|
|
|
})}
|
|
|
|
placeholder="Email"
|
|
|
|
type="email"
|
|
|
|
/>
|
|
|
|
{errors.email && (
|
|
|
|
<FormErrorMessage>{errors.email.message}</FormErrorMessage>
|
|
|
|
)}
|
|
|
|
</FormControl>
|
|
|
|
<FormControl mt={4}>
|
|
|
|
<FormLabel htmlFor="name">Full name</FormLabel>
|
2024-03-17 17:28:45 +01:00
|
|
|
<Input id="name" {...register("full_name")} type="text" />
|
2024-03-08 14:58:36 +01:00
|
|
|
</FormControl>
|
|
|
|
<FormControl mt={4} isInvalid={!!errors.password}>
|
|
|
|
<FormLabel htmlFor="password">Set Password</FormLabel>
|
|
|
|
<Input
|
|
|
|
id="password"
|
2024-03-17 17:28:45 +01:00
|
|
|
{...register("password", {
|
2024-03-08 14:58:36 +01:00
|
|
|
minLength: {
|
|
|
|
value: 8,
|
2024-03-17 17:28:45 +01:00
|
|
|
message: "Password must be at least 8 characters",
|
2024-03-08 14:58:36 +01:00
|
|
|
},
|
|
|
|
})}
|
|
|
|
placeholder="Password"
|
|
|
|
type="password"
|
|
|
|
/>
|
|
|
|
{errors.password && (
|
|
|
|
<FormErrorMessage>{errors.password.message}</FormErrorMessage>
|
|
|
|
)}
|
|
|
|
</FormControl>
|
|
|
|
<FormControl mt={4} isInvalid={!!errors.confirm_password}>
|
|
|
|
<FormLabel htmlFor="confirm_password">Confirm Password</FormLabel>
|
|
|
|
<Input
|
|
|
|
id="confirm_password"
|
2024-03-17 17:28:45 +01:00
|
|
|
{...register("confirm_password", {
|
2024-03-08 14:58:36 +01:00
|
|
|
validate: (value) =>
|
|
|
|
value === getValues().password ||
|
2024-03-17 17:28:45 +01:00
|
|
|
"The passwords do not match",
|
2024-03-08 14:58:36 +01:00
|
|
|
})}
|
|
|
|
placeholder="Password"
|
|
|
|
type="password"
|
|
|
|
/>
|
|
|
|
{errors.confirm_password && (
|
|
|
|
<FormErrorMessage>
|
|
|
|
{errors.confirm_password.message}
|
|
|
|
</FormErrorMessage>
|
|
|
|
)}
|
|
|
|
</FormControl>
|
|
|
|
<Flex>
|
|
|
|
<FormControl mt={4}>
|
2024-03-17 17:28:45 +01:00
|
|
|
<Checkbox {...register("is_superuser")} colorScheme="teal">
|
2024-03-08 14:58:36 +01:00
|
|
|
Is superuser?
|
|
|
|
</Checkbox>
|
|
|
|
</FormControl>
|
|
|
|
<FormControl mt={4}>
|
2024-03-17 17:28:45 +01:00
|
|
|
<Checkbox {...register("is_active")} colorScheme="teal">
|
2024-03-08 14:58:36 +01:00
|
|
|
Is active?
|
|
|
|
</Checkbox>
|
|
|
|
</FormControl>
|
|
|
|
</Flex>
|
|
|
|
</ModalBody>
|
2024-02-26 09:39:09 -05:00
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
<ModalFooter gap={3}>
|
|
|
|
<Button
|
2024-03-11 16:50:46 +01:00
|
|
|
variant="primary"
|
2024-03-08 14:58:36 +01:00
|
|
|
type="submit"
|
|
|
|
isLoading={isSubmitting}
|
|
|
|
isDisabled={!isDirty}
|
|
|
|
>
|
|
|
|
Save
|
|
|
|
</Button>
|
|
|
|
<Button onClick={onCancel}>Cancel</Button>
|
|
|
|
</ModalFooter>
|
|
|
|
</ModalContent>
|
|
|
|
</Modal>
|
|
|
|
</>
|
|
|
|
)
|
2024-02-26 09:39:09 -05:00
|
|
|
}
|
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
export default EditUser
|