2024-01-30 14:41:16 +00:00
|
|
|
import { ReactElement, useState } from "react";
|
2023-11-29 13:37:33 +00:00
|
|
|
import Image from "next/image";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
// services
|
2024-03-19 14:38:35 +00:00
|
|
|
import { Eye, EyeOff } from "lucide-react";
|
|
|
|
import { Button, Input, TOAST_TYPE, setToast } from "@plane/ui";
|
|
|
|
import { LatestFeatureBlock } from "@/components/common";
|
|
|
|
import { PageHead } from "@/components/core";
|
|
|
|
import { NEW_PASS_CREATED } from "@/constants/event-tracker";
|
|
|
|
import { checkEmailValidity } from "@/helpers/string.helper";
|
|
|
|
import { useEventTracker } from "@/hooks/store";
|
|
|
|
import useSignInRedirection from "@/hooks/use-sign-in-redirection";
|
|
|
|
import DefaultLayout from "@/layouts/default-layout";
|
|
|
|
import { NextPageWithLayout } from "@/lib/types";
|
|
|
|
import { AuthService } from "@/services/auth.service";
|
2023-11-29 13:37:33 +00:00
|
|
|
// hooks
|
|
|
|
// layouts
|
2024-01-19 15:25:03 +00:00
|
|
|
// components
|
2023-11-29 13:37:33 +00:00
|
|
|
// ui
|
|
|
|
// images
|
|
|
|
import BluePlaneLogoWithoutText from "public/plane-logos/blue-without-text.png";
|
|
|
|
// helpers
|
|
|
|
// type
|
2024-01-30 14:41:16 +00:00
|
|
|
// icons
|
2024-02-09 10:52:08 +00:00
|
|
|
// constants
|
2023-11-29 13:37:33 +00:00
|
|
|
|
|
|
|
type TResetPasswordFormValues = {
|
|
|
|
email: string;
|
|
|
|
password: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues: TResetPasswordFormValues = {
|
|
|
|
email: "",
|
|
|
|
password: "",
|
|
|
|
};
|
|
|
|
|
|
|
|
// services
|
|
|
|
const authService = new AuthService();
|
|
|
|
|
2024-01-19 15:25:03 +00:00
|
|
|
const ResetPasswordPage: NextPageWithLayout = () => {
|
2023-11-29 13:37:33 +00:00
|
|
|
// router
|
|
|
|
const router = useRouter();
|
|
|
|
const { uidb64, token, email } = router.query;
|
2024-01-30 14:41:16 +00:00
|
|
|
// states
|
|
|
|
const [showPassword, setShowPassword] = useState(false);
|
2024-02-09 10:52:08 +00:00
|
|
|
// store hooks
|
|
|
|
const { captureEvent } = useEventTracker();
|
2023-12-04 09:34:04 +00:00
|
|
|
// sign in redirection hook
|
|
|
|
const { handleRedirection } = useSignInRedirection();
|
2023-11-29 13:37:33 +00:00
|
|
|
// form info
|
|
|
|
const {
|
|
|
|
control,
|
|
|
|
formState: { errors, isSubmitting, isValid },
|
|
|
|
handleSubmit,
|
|
|
|
} = useForm<TResetPasswordFormValues>({
|
|
|
|
defaultValues: {
|
|
|
|
...defaultValues,
|
|
|
|
email: email?.toString() ?? "",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleResetPassword = async (formData: TResetPasswordFormValues) => {
|
|
|
|
if (!uidb64 || !token || !email) return;
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
new_password: formData.password,
|
|
|
|
};
|
|
|
|
|
2023-12-01 10:20:01 +00:00
|
|
|
await authService
|
|
|
|
.resetPassword(uidb64.toString(), token.toString(), payload)
|
2024-02-09 10:52:08 +00:00
|
|
|
.then(() => {
|
|
|
|
captureEvent(NEW_PASS_CREATED, {
|
|
|
|
state: "SUCCESS",
|
|
|
|
});
|
|
|
|
handleRedirection();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
captureEvent(NEW_PASS_CREATED, {
|
|
|
|
state: "FAILED",
|
|
|
|
});
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
2023-12-01 10:20:01 +00:00
|
|
|
title: "Error!",
|
|
|
|
message: err?.error ?? "Something went wrong. Please try again.",
|
2024-02-09 10:52:08 +00:00
|
|
|
});
|
|
|
|
});
|
2023-11-29 13:37:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2024-02-20 08:06:38 +00:00
|
|
|
<>
|
|
|
|
<PageHead title="Reset Password" />
|
|
|
|
<div className="h-full w-full bg-onboarding-gradient-100">
|
|
|
|
<div className="flex items-center justify-between px-8 pb-4 sm:px-16 sm:py-5 lg:px-28 ">
|
|
|
|
<div className="flex items-center gap-x-2 py-10">
|
|
|
|
<Image src={BluePlaneLogoWithoutText} height={30} width={30} alt="Plane Logo" className="mr-2" />
|
|
|
|
<span className="text-2xl font-semibold sm:text-3xl">Plane</span>
|
|
|
|
</div>
|
2023-11-29 13:37:33 +00:00
|
|
|
</div>
|
|
|
|
|
2024-02-20 08:06:38 +00:00
|
|
|
<div className="mx-auto h-full rounded-t-md border-x border-t border-custom-border-200 bg-onboarding-gradient-100 px-4 pt-4 shadow-sm sm:w-4/5 md:w-2/3 ">
|
|
|
|
<div className="h-full overflow-auto rounded-t-md bg-onboarding-gradient-200 px-7 pb-56 pt-24 sm:px-0">
|
|
|
|
<div className="mx-auto flex flex-col divide-y divide-custom-border-200 sm:w-96">
|
|
|
|
<h1 className="sm:text-2.5xl text-center text-2xl font-medium text-onboarding-text-100">
|
|
|
|
Let{"'"}s get a new password
|
|
|
|
</h1>
|
|
|
|
<form onSubmit={handleSubmit(handleResetPassword)} className="mx-auto mt-11 space-y-4 sm:w-96">
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="email"
|
|
|
|
rules={{
|
|
|
|
required: "Email is required",
|
|
|
|
validate: (value) => checkEmailValidity(value) || "Email is invalid",
|
|
|
|
}}
|
|
|
|
render={({ field: { value, onChange, ref } }) => (
|
2024-01-30 14:41:16 +00:00
|
|
|
<Input
|
2024-02-20 08:06:38 +00:00
|
|
|
id="email"
|
|
|
|
name="email"
|
|
|
|
type="email"
|
2024-01-30 14:41:16 +00:00
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
2024-02-20 08:06:38 +00:00
|
|
|
ref={ref}
|
|
|
|
hasError={Boolean(errors.email)}
|
|
|
|
placeholder="name@company.com"
|
|
|
|
className="h-[46px] w-full border border-onboarding-border-100 !bg-onboarding-background-200 pr-12 text-onboarding-text-400"
|
|
|
|
disabled
|
2024-01-30 14:41:16 +00:00
|
|
|
/>
|
2024-02-20 08:06:38 +00:00
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="password"
|
|
|
|
rules={{
|
|
|
|
required: "Password is required",
|
|
|
|
}}
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<div className="relative flex items-center rounded-md bg-onboarding-background-200">
|
|
|
|
<Input
|
|
|
|
type={showPassword ? "text" : "password"}
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
hasError={Boolean(errors.password)}
|
|
|
|
placeholder="Enter password"
|
|
|
|
className="h-[46px] w-full border border-onboarding-border-100 !bg-onboarding-background-200 pr-12 placeholder:text-onboarding-text-400"
|
|
|
|
minLength={8}
|
2024-01-30 14:41:16 +00:00
|
|
|
/>
|
2024-02-20 08:06:38 +00:00
|
|
|
{showPassword ? (
|
|
|
|
<EyeOff
|
|
|
|
className="absolute right-3 h-5 w-5 stroke-custom-text-400 hover:cursor-pointer"
|
|
|
|
onClick={() => setShowPassword(false)}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Eye
|
|
|
|
className="absolute right-3 h-5 w-5 stroke-custom-text-400 hover:cursor-pointer"
|
|
|
|
onClick={() => setShowPassword(true)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
type="submit"
|
|
|
|
variant="primary"
|
|
|
|
className="w-full"
|
|
|
|
size="xl"
|
|
|
|
disabled={!isValid}
|
|
|
|
loading={isSubmitting}
|
|
|
|
>
|
|
|
|
Set password
|
|
|
|
</Button>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
<LatestFeatureBlock />
|
2023-11-29 13:37:33 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-02-20 08:06:38 +00:00
|
|
|
</>
|
2023-11-29 13:37:33 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-01-19 15:25:03 +00:00
|
|
|
ResetPasswordPage.getLayout = function getLayout(page: ReactElement) {
|
2023-11-29 13:37:33 +00:00
|
|
|
return <DefaultLayout>{page}</DefaultLayout>;
|
|
|
|
};
|
|
|
|
|
2024-01-19 15:25:03 +00:00
|
|
|
export default ResetPasswordPage;
|