|
|
@@ -1,28 +1,39 @@
|
|
|
"use client";
|
|
|
|
|
|
import { useForm, SubmitHandler } from "react-hook-form";
|
|
|
+import {RESET_PASSWORD} from "@/graphql";
|
|
|
+import { useRouter } from "next/navigation";
|
|
|
+import { useApolloClient } from "@apollo/client/react";
|
|
|
+import { useCustomToast } from '@/utils/hooks/useToast';
|
|
|
+import { confirmDialog } from "@/components/theme/ui/kernel/confirm/api";
|
|
|
import { overlayLoading } from "@/components/theme/ui/kernel/loading/api";
|
|
|
import PasswordInput from "@/components/theme/ui/PasswordInput";
|
|
|
|
|
|
type ResetPasswordFormData= {
|
|
|
password: string;
|
|
|
- password_confirmation: string;
|
|
|
+ passwordConfirmation: string;
|
|
|
};
|
|
|
|
|
|
-export default function ResetPasswordWrapper() {
|
|
|
- // const { showToast } = useCustomToast();
|
|
|
- // const [emailValue, setEmailValue] = useState('');
|
|
|
-
|
|
|
+export default function ResetPasswordWrapper({
|
|
|
+ token,
|
|
|
+ email
|
|
|
+}: {
|
|
|
+ token: string;
|
|
|
+ email: string;
|
|
|
+}) {
|
|
|
+ const router = useRouter();
|
|
|
+ const { showToast } = useCustomToast();
|
|
|
+ const apolloClient = useApolloClient();
|
|
|
const {
|
|
|
register,
|
|
|
handleSubmit,
|
|
|
formState: { errors },
|
|
|
} = useForm<ResetPasswordFormData>({
|
|
|
- mode: "onSubmit",
|
|
|
+ mode: "onChange",
|
|
|
reValidateMode: "onChange",
|
|
|
defaultValues: {
|
|
|
"password": '',
|
|
|
- "password_confirmation": ''
|
|
|
+ "passwordConfirmation": ''
|
|
|
}
|
|
|
});
|
|
|
|
|
|
@@ -31,24 +42,40 @@ export default function ResetPasswordWrapper() {
|
|
|
overlayLoading.start();
|
|
|
console.log(data);
|
|
|
|
|
|
- // try {
|
|
|
- // const result = await recoverPasswordAction({
|
|
|
- // password: data.password,
|
|
|
- // password_confirmation: data.password_confirmation
|
|
|
- // });
|
|
|
+ apolloClient.mutate({
|
|
|
+ mutation: RESET_PASSWORD,
|
|
|
+ variables: {
|
|
|
+ token: token,
|
|
|
+ email: email,
|
|
|
+ password: data.password,
|
|
|
+ passwordConfirmation: data.passwordConfirmation
|
|
|
+ }
|
|
|
+ }).then((res) => {
|
|
|
+ overlayLoading.stop();
|
|
|
+ const resData = res.data?.createResetPassword?.resetPassword ?? null;
|
|
|
+ if(resData?.success) {
|
|
|
|
|
|
- // // Show success/error API response
|
|
|
- // if (result.success) {
|
|
|
- // showToast(result.msg, "success");
|
|
|
- // setEmailValue(data.email);
|
|
|
- // } else {
|
|
|
- // showToast(result.msg, "danger");
|
|
|
- // }
|
|
|
- // } catch {
|
|
|
- // showToast("Something went wrong. Please try again later.", "danger");
|
|
|
- // } finally {
|
|
|
- // overlayLoading.stop();
|
|
|
- // }
|
|
|
+ confirmDialog({
|
|
|
+ title: "Notice",
|
|
|
+ content: "Reset password successfully. Please to login.",
|
|
|
+ noCancel: true,
|
|
|
+ }).then(() => {
|
|
|
+ router.replace('/customer/login');
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ // const msg = (resData?.message ?? 'Reset password failed.') + ' You can try again.'
|
|
|
+ // confirmDialog({
|
|
|
+ // title: "Notice",
|
|
|
+ // content: msg,
|
|
|
+ // }).then(() => {
|
|
|
+ // router.replace('/customer/forget-password');
|
|
|
+ // }).catch(() => {/**关闭 */});
|
|
|
+ showToast(resData?.message ?? 'Reset password failed.', "danger");
|
|
|
+ }
|
|
|
+ }).catch((err) => {
|
|
|
+ overlayLoading.stop();
|
|
|
+ showToast(err.message, "danger");
|
|
|
+ });
|
|
|
};
|
|
|
|
|
|
return (
|
|
|
@@ -80,7 +107,7 @@ export default function ResetPasswordWrapper() {
|
|
|
return true;
|
|
|
},
|
|
|
})}
|
|
|
- error={errors.password_confirmation && errors.password_confirmation.message}
|
|
|
+ error={errors.password && errors.password.message}
|
|
|
/>
|
|
|
</div>
|
|
|
<div className="w-full mt-4">
|
|
|
@@ -90,7 +117,7 @@ export default function ResetPasswordWrapper() {
|
|
|
|
|
|
<PasswordInput
|
|
|
placeholder="Confirm password"
|
|
|
- {...register("password_confirmation", {
|
|
|
+ {...register("passwordConfirmation", {
|
|
|
required: "Confirm Password is required",
|
|
|
minLength: {
|
|
|
value: 6,
|
|
|
@@ -108,7 +135,7 @@ export default function ResetPasswordWrapper() {
|
|
|
return true;
|
|
|
},
|
|
|
})}
|
|
|
- error={errors.password_confirmation && errors.password_confirmation.message}
|
|
|
+ error={errors.passwordConfirmation && errors.passwordConfirmation.message}
|
|
|
/>
|
|
|
</div>
|
|
|
|