♻️ Refactor and tweaks, rename UserCreateOpen to UserRegister and others (#1143)

This commit is contained in:
Alejandra
2024-04-01 22:53:33 -05:00
committed by GitHub
parent aed4db7521
commit 4239d93ea6
45 changed files with 537 additions and 518 deletions

View File

@@ -14,13 +14,14 @@ import { useMutation } from "react-query"
import { type ApiError, type UpdatePassword, UsersService } from "../../client"
import useCustomToast from "../../hooks/useCustomToast"
import { confirmPasswordRules, passwordRules } from "../../utils"
interface UpdatePasswordForm extends UpdatePassword {
confirm_password: string
}
const ChangePassword = () => {
const color = useColorModeValue("inherit", "ui.white")
const color = useColorModeValue("inherit", "ui.light")
const showToast = useCustomToast()
const {
register,
@@ -33,20 +34,20 @@ const ChangePassword = () => {
criteriaMode: "all",
})
const UpdatePassword = async (data: UpdatePassword) => {
await UsersService.updatePasswordMe({ requestBody: data })
}
const mutation = useMutation(UpdatePassword, {
onSuccess: () => {
showToast("Success!", "Password updated.", "success")
reset()
const mutation = useMutation(
(data: UpdatePassword) =>
UsersService.updatePasswordMe({ requestBody: data }),
{
onSuccess: () => {
showToast("Success!", "Password updated.", "success")
reset()
},
onError: (err: ApiError) => {
const errDetail = err.body?.detail
showToast("Something went wrong.", `${errDetail}`, "error")
},
},
onError: (err: ApiError) => {
const errDetail = err.body?.detail
showToast("Something went wrong.", `${errDetail}`, "error")
},
})
)
const onSubmit: SubmitHandler<UpdatePasswordForm> = async (data) => {
mutation.mutate(data)
@@ -79,13 +80,7 @@ const ChangePassword = () => {
<FormLabel htmlFor="password">Set Password</FormLabel>
<Input
id="password"
{...register("new_password", {
required: "Password is required",
minLength: {
value: 8,
message: "Password must be at least 8 characters",
},
})}
{...register("new_password", passwordRules())}
placeholder="Password"
type="password"
/>
@@ -97,12 +92,7 @@ const ChangePassword = () => {
<FormLabel htmlFor="confirm_password">Confirm Password</FormLabel>
<Input
id="confirm_password"
{...register("confirm_password", {
required: "Please confirm your password",
validate: (value) =>
value === getValues().new_password ||
"The passwords do not match",
})}
{...register("confirm_password", confirmPasswordRules(getValues))}
placeholder="Password"
type="password"
/>

View File

@@ -31,28 +31,27 @@ const DeleteConfirmation = ({ isOpen, onClose }: DeleteProps) => {
const currentUser = queryClient.getQueryData<UserOut>("currentUser")
const { logout } = useAuth()
const deleteCurrentUser = async (id: number) => {
await UsersService.deleteUser({ userId: id })
}
const mutation = useMutation(deleteCurrentUser, {
onSuccess: () => {
showToast(
"Success",
"Your account has been successfully deleted.",
"success",
)
logout()
onClose()
const mutation = useMutation(
(id: number) => UsersService.deleteUser({ userId: id }),
{
onSuccess: () => {
showToast(
"Success",
"Your account has been successfully deleted.",
"success",
)
logout()
onClose()
},
onError: (err: ApiError) => {
const errDetail = err.body?.detail
showToast("Something went wrong.", `${errDetail}`, "error")
},
onSettled: () => {
queryClient.invalidateQueries("currentUser")
},
},
onError: (err: ApiError) => {
const errDetail = err.body?.detail
showToast("Something went wrong.", `${errDetail}`, "error")
},
onSettled: () => {
queryClient.invalidateQueries("currentUser")
},
})
)
const onSubmit = async () => {
mutation.mutate(currentUser!.id)

View File

@@ -27,7 +27,7 @@ import { emailPattern } from "../../utils"
const UserInformation = () => {
const queryClient = useQueryClient()
const color = useColorModeValue("inherit", "ui.white")
const color = useColorModeValue("inherit", "ui.light")
const showToast = useCustomToast()
const [editMode, setEditMode] = useState(false)
const { user: currentUser } = useAuth()
@@ -50,23 +50,22 @@ const UserInformation = () => {
setEditMode(!editMode)
}
const updateInfo = async (data: UserUpdateMe) => {
await UsersService.updateUserMe({ requestBody: data })
}
const mutation = useMutation(updateInfo, {
onSuccess: () => {
showToast("Success!", "User updated successfully.", "success")
const mutation = useMutation(
(data: UserUpdateMe) => UsersService.updateUserMe({ requestBody: data }),
{
onSuccess: () => {
showToast("Success!", "User updated successfully.", "success")
},
onError: (err: ApiError) => {
const errDetail = err.body?.detail
showToast("Something went wrong.", `${errDetail}`, "error")
},
onSettled: () => {
queryClient.invalidateQueries("users")
queryClient.invalidateQueries("currentUser")
},
},
onError: (err: ApiError) => {
const errDetail = err.body?.detail
showToast("Something went wrong.", `${errDetail}`, "error")
},
onSettled: () => {
queryClient.invalidateQueries("users")
queryClient.invalidateQueries("currentUser")
},
})
)
const onSubmit: SubmitHandler<UserUpdateMe> = async (data) => {
mutation.mutate(data)
@@ -99,7 +98,7 @@ const UserInformation = () => {
<Text
size="md"
py={2}
color={!currentUser?.full_name ? "gray.400" : "inherit"}
color={!currentUser?.full_name ? "ui.dim" : "inherit"}
>
{currentUser?.full_name || "N/A"}
</Text>