2023-12-06 11:12:57 +00:00
|
|
|
import React, { useEffect } from "react";
|
2023-12-01 10:20:01 +00:00
|
|
|
import Link from "next/link";
|
2023-11-29 13:37:33 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
// services
|
|
|
|
import { AuthService } from "services/auth.service";
|
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
|
|
|
// ui
|
|
|
|
import { Button, Input } from "@plane/ui";
|
|
|
|
// helpers
|
|
|
|
import { checkEmailValidity } from "helpers/string.helper";
|
|
|
|
// constants
|
|
|
|
import { ESignInSteps } from "components/account";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
email: string;
|
|
|
|
handleStepChange: (step: ESignInSteps) => void;
|
|
|
|
handleSignInRedirection: () => Promise<void>;
|
2023-12-06 08:52:59 +00:00
|
|
|
isOnboarded: boolean;
|
2023-11-29 13:37:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type TCreatePasswordFormValues = {
|
|
|
|
email: string;
|
|
|
|
password: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues: TCreatePasswordFormValues = {
|
|
|
|
email: "",
|
|
|
|
password: "",
|
|
|
|
};
|
|
|
|
|
|
|
|
// services
|
|
|
|
const authService = new AuthService();
|
|
|
|
|
|
|
|
export const CreatePasswordForm: React.FC<Props> = (props) => {
|
2023-12-06 08:52:59 +00:00
|
|
|
const { email, handleSignInRedirection, isOnboarded } = props;
|
2023-11-29 13:37:33 +00:00
|
|
|
// toast alert
|
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
// form info
|
|
|
|
const {
|
|
|
|
control,
|
|
|
|
formState: { errors, isSubmitting, isValid },
|
|
|
|
handleSubmit,
|
2023-12-06 11:12:57 +00:00
|
|
|
setFocus,
|
2023-11-29 13:37:33 +00:00
|
|
|
} = useForm<TCreatePasswordFormValues>({
|
|
|
|
defaultValues: {
|
|
|
|
...defaultValues,
|
|
|
|
email,
|
|
|
|
},
|
|
|
|
mode: "onChange",
|
|
|
|
reValidateMode: "onChange",
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleCreatePassword = async (formData: TCreatePasswordFormValues) => {
|
|
|
|
const payload = {
|
|
|
|
password: formData.password,
|
|
|
|
};
|
|
|
|
|
|
|
|
await authService
|
|
|
|
.setPassword(payload)
|
|
|
|
.then(async () => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
|
|
|
title: "Success!",
|
|
|
|
message: "Password created successfully.",
|
|
|
|
});
|
|
|
|
await handleSignInRedirection();
|
|
|
|
})
|
|
|
|
.catch((err) =>
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: err?.error ?? "Something went wrong. Please try again.",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-12-06 11:12:57 +00:00
|
|
|
useEffect(() => {
|
|
|
|
setFocus("password");
|
|
|
|
}, [setFocus]);
|
|
|
|
|
2023-11-29 13:37:33 +00:00
|
|
|
return (
|
|
|
|
<>
|
2023-12-01 10:20:01 +00:00
|
|
|
<h1 className="text-center text-2xl sm:text-2.5xl font-medium text-onboarding-text-100">
|
2023-12-06 08:52:59 +00:00
|
|
|
Get on your flight deck
|
2023-11-29 13:37:33 +00:00
|
|
|
</h1>
|
|
|
|
<form onSubmit={handleSubmit(handleCreatePassword)} className="mt-11 sm:w-96 mx-auto space-y-4">
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="email"
|
|
|
|
rules={{
|
|
|
|
required: "Email is required",
|
|
|
|
validate: (value) => checkEmailValidity(value) || "Email is invalid",
|
|
|
|
}}
|
|
|
|
render={({ field: { value, onChange, ref } }) => (
|
|
|
|
<Input
|
|
|
|
id="email"
|
|
|
|
name="email"
|
|
|
|
type="email"
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
ref={ref}
|
|
|
|
hasError={Boolean(errors.email)}
|
|
|
|
placeholder="orville.wright@firstflight.com"
|
2023-12-06 08:52:59 +00:00
|
|
|
className="w-full h-[46px] text-onboarding-text-400 border border-onboarding-border-100 pr-12 !bg-onboarding-background-200"
|
2023-11-29 13:37:33 +00:00
|
|
|
disabled
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
2023-12-06 08:52:59 +00:00
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="password"
|
|
|
|
rules={{
|
|
|
|
required: "Password is required",
|
|
|
|
}}
|
|
|
|
render={({ field: { value, onChange, ref } }) => (
|
|
|
|
<Input
|
|
|
|
type="password"
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
ref={ref}
|
|
|
|
hasError={Boolean(errors.password)}
|
|
|
|
placeholder="Choose password"
|
|
|
|
className="w-full h-[46px] placeholder:text-onboarding-text-400 border border-onboarding-border-100 pr-12 !bg-onboarding-background-200"
|
|
|
|
minLength={8}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
2023-11-29 13:37:33 +00:00
|
|
|
<Button type="submit" variant="primary" className="w-full" size="xl" disabled={!isValid} loading={isSubmitting}>
|
2023-12-06 08:52:59 +00:00
|
|
|
{isOnboarded ? "Go to workspace" : "Set up workspace"}
|
2023-11-29 13:37:33 +00:00
|
|
|
</Button>
|
2023-12-01 10:20:01 +00:00
|
|
|
<p className="text-xs text-onboarding-text-200">
|
|
|
|
When you click the button above, you agree with our{" "}
|
|
|
|
<Link href="https://plane.so/terms-and-conditions" target="_blank" rel="noopener noreferrer">
|
|
|
|
<span className="font-semibold underline">terms and conditions of service.</span>
|
|
|
|
</Link>
|
|
|
|
</p>
|
2023-11-29 13:37:33 +00:00
|
|
|
</form>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|