Skip to content

Commit

Permalink
update to mutate
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaJ2305 committed Jan 3, 2025
1 parent d2cfca6 commit c170f70
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions src/components/Users/UserResetPassword.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { zodResolver } from "@hookform/resolvers/zod";
import { useMutation } from "@tanstack/react-query";
import { useState } from "react";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
Expand All @@ -22,7 +23,7 @@ import { UpdatePasswordForm } from "@/components/Users/models";

import * as Notification from "@/Utils/Notifications";
import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";
import mutate from "@/Utils/request/mutate";
import { UserBase } from "@/types/user/user";

const PasswordSchema = z
Expand Down Expand Up @@ -55,7 +56,6 @@ export default function UserResetPassword({
userData: UserBase;
}) {
const { t } = useTranslation();
const [isSubmitting, setIsSubmitting] = useState(false);
const [isEditing, setIsEditing] = useState(false);
const [isPasswordFieldFocused, setIsPasswordFieldFocused] = useState(false);

Expand All @@ -67,29 +67,37 @@ export default function UserResetPassword({
new_password_2: "",
},
});
const { mutate: resetUserPasswordMutate } = useMutation({
mutationFn: async (formData: UpdatePasswordForm) => {
const response = await mutate(routes.updatePassword, { silent: true })(
formData,
);
if ("errors" in response) {
throw response;
}
return response;
},
onSuccess: (data: any) => {
Notification.Success({ msg: data?.message as string });
resetPasswordForm.reset();
},
onError: (error: any) => {
const errorMessage =
error?.response?.data?.message || t("password_update_error");
Notification.Error({ msg: errorMessage });
resetPasswordForm.reset();
},
});

const handleSubmitPassword = async (
formData: z.infer<typeof PasswordSchema>,
) => {
setIsSubmitting(true);

const form: UpdatePasswordForm = {
old_password: formData.current_password,
username: userData.username,
new_password: formData.new_password_1,
};

const { res, data, error } = await request(routes.updatePassword, {
body: form,
});

if (res?.ok) {
Notification.Success({ msg: data?.message as string });
} else {
Notification.Error({
msg: error?.message ?? t("password_update_error"),
});
}
setIsSubmitting(false);
resetUserPasswordMutate(form);
};

const renderPasswordForm = () => (
Expand All @@ -105,6 +113,7 @@ export default function UserResetPassword({
<FormControl>
<TextFormField
{...field}
type="password"
onChange={(value) => {
field.onChange(value.value);
}}
Expand All @@ -125,6 +134,7 @@ export default function UserResetPassword({
<FormControl>
<TextFormField
{...field}
type="password"
onChange={(value) => {
field.onChange(value.value);
}}
Expand Down Expand Up @@ -172,6 +182,7 @@ export default function UserResetPassword({
<FormControl>
<TextFormField
{...field}
type="password"
onChange={(value) => {
field.onChange(value.value);
}}
Expand All @@ -186,15 +197,16 @@ export default function UserResetPassword({

<Button
type="submit"
disabled={isSubmitting}
disabled={!resetPasswordForm.formState.isDirty}
variant="primary"
className="mt-6 w-full"
>
{isSubmitting ? t("submitting") : t("submit")}
{resetPasswordForm.formState.isDirty ? t("submitting") : t("submit")}
</Button>
</form>
</Form>
);

const editButton = () => (
<div className="mb-4 flex justify-start">
<Button
Expand Down

0 comments on commit c170f70

Please sign in to comment.