2023-11-29 13:37:33 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import { Controller, useForm } from "react-hook-form";
|
2024-01-19 15:25:03 +00:00
|
|
|
// services
|
|
|
|
import { AuthService } from "services/auth.service";
|
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
2023-11-29 13:37:33 +00:00
|
|
|
// ui
|
|
|
|
import { Button, Input } from "@plane/ui";
|
|
|
|
// helpers
|
|
|
|
import { checkEmailValidity } from "helpers/string.helper";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
email: string;
|
|
|
|
handleSignInRedirection: () => Promise<void>;
|
|
|
|
};
|
|
|
|
|
2024-01-19 15:25:03 +00:00
|
|
|
type TCreatePasswordFormValues = {
|
|
|
|
email: string;
|
|
|
|
password: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues: TCreatePasswordFormValues = {
|
|
|
|
email: "",
|
|
|
|
password: "",
|
|
|
|
};
|
|
|
|
|
|
|
|
// services
|
|
|
|
const authService = new AuthService();
|
|
|
|
|
|
|
|
export const SignInOptionalSetPasswordForm: React.FC<Props> = (props) => {
|
|
|
|
const { email, handleSignInRedirection } = props;
|
2023-11-29 13:37:33 +00:00
|
|
|
// states
|
|
|
|
const [isGoingToWorkspace, setIsGoingToWorkspace] = useState(false);
|
2024-01-19 15:25:03 +00:00
|
|
|
// toast alert
|
|
|
|
const { setToastAlert } = useToast();
|
2023-12-06 08:52:59 +00:00
|
|
|
// form info
|
2023-11-29 13:37:33 +00:00
|
|
|
const {
|
|
|
|
control,
|
2024-01-19 15:25:03 +00:00
|
|
|
formState: { errors, isSubmitting, isValid },
|
|
|
|
handleSubmit,
|
|
|
|
} = useForm<TCreatePasswordFormValues>({
|
2023-11-29 13:37:33 +00:00
|
|
|
defaultValues: {
|
2024-01-19 15:25:03 +00:00
|
|
|
...defaultValues,
|
2023-11-29 13:37:33 +00:00
|
|
|
email,
|
|
|
|
},
|
|
|
|
mode: "onChange",
|
|
|
|
reValidateMode: "onChange",
|
|
|
|
});
|
|
|
|
|
2024-01-19 15:25:03 +00:00
|
|
|
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-11-29 13:37:33 +00:00
|
|
|
const handleGoToWorkspace = async () => {
|
|
|
|
setIsGoingToWorkspace(true);
|
|
|
|
|
|
|
|
await handleSignInRedirection().finally(() => setIsGoingToWorkspace(false));
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2024-01-19 15:25:03 +00:00
|
|
|
<h1 className="sm:text-2.5xl text-center text-2xl font-medium text-onboarding-text-100">Set your password</h1>
|
|
|
|
<p className="mt-2.5 text-center text-sm text-onboarding-text-200">
|
2023-12-06 08:52:59 +00:00
|
|
|
If you{"'"}d like to do away with codes, set a password here.
|
2023-11-29 13:37:33 +00:00
|
|
|
</p>
|
2024-01-19 15:25:03 +00:00
|
|
|
<form onSubmit={handleSubmit(handleCreatePassword)} className="mx-auto mt-5 space-y-4 sm:w-96">
|
2023-11-29 13:37:33 +00:00
|
|
|
<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)}
|
2024-01-19 15:25:03 +00:00
|
|
|
placeholder="name@company.com"
|
|
|
|
className="h-[46px] w-full border border-onboarding-border-100 !bg-onboarding-background-200 pr-12 text-onboarding-text-400"
|
2023-11-29 13:37:33 +00:00
|
|
|
disabled
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
2024-01-19 15:25:03 +00:00
|
|
|
<div>
|
|
|
|
<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="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}
|
|
|
|
autoFocus
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<p className="text-onboarding-text-200 text-xs mt-2 pb-3">
|
|
|
|
Whatever you choose now will be your account{"'"}s password until you change it.
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div className="space-y-2.5">
|
2023-11-29 13:37:33 +00:00
|
|
|
<Button
|
2024-01-19 15:25:03 +00:00
|
|
|
type="submit"
|
2023-11-29 13:37:33 +00:00
|
|
|
variant="primary"
|
|
|
|
className="w-full"
|
|
|
|
size="xl"
|
|
|
|
disabled={!isValid}
|
2024-01-19 15:25:03 +00:00
|
|
|
loading={isSubmitting}
|
2023-11-29 13:37:33 +00:00
|
|
|
>
|
2024-01-19 15:25:03 +00:00
|
|
|
Set password
|
2023-11-29 13:37:33 +00:00
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
variant="outline-primary"
|
|
|
|
className="w-full"
|
|
|
|
size="xl"
|
|
|
|
onClick={handleGoToWorkspace}
|
|
|
|
loading={isGoingToWorkspace}
|
|
|
|
>
|
2024-01-19 15:25:03 +00:00
|
|
|
Skip to workspace
|
2023-11-29 13:37:33 +00:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|