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";
|
2024-02-09 10:52:08 +00:00
|
|
|
import { useEventTracker } from "hooks/store";
|
2023-11-29 13:37:33 +00:00
|
|
|
// ui
|
|
|
|
import { Button, Input } from "@plane/ui";
|
|
|
|
// helpers
|
|
|
|
import { checkEmailValidity } from "helpers/string.helper";
|
2024-01-30 14:41:16 +00:00
|
|
|
// icons
|
|
|
|
import { Eye, EyeOff } from "lucide-react";
|
2024-02-09 10:52:08 +00:00
|
|
|
import { PASSWORD_CREATE_SELECTED, PASSWORD_CREATE_SKIPPED } from "constants/event-tracker";
|
2023-11-29 13:37:33 +00:00
|
|
|
|
|
|
|
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-30 14:41:16 +00:00
|
|
|
const [showPassword, setShowPassword] = useState(false);
|
2024-02-09 10:52:08 +00:00
|
|
|
// store hooks
|
|
|
|
const { captureEvent } = useEventTracker();
|
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.",
|
|
|
|
});
|
2024-02-09 10:52:08 +00:00
|
|
|
captureEvent(PASSWORD_CREATE_SELECTED, {
|
|
|
|
state: "SUCCESS",
|
|
|
|
first_time: false,
|
|
|
|
});
|
2024-01-19 15:25:03 +00:00
|
|
|
await handleSignInRedirection();
|
|
|
|
})
|
2024-02-09 10:52:08 +00:00
|
|
|
.catch((err) => {
|
|
|
|
captureEvent(PASSWORD_CREATE_SELECTED, {
|
|
|
|
state: "FAILED",
|
|
|
|
first_time: false,
|
|
|
|
});
|
2024-01-19 15:25:03 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: err?.error ?? "Something went wrong. Please try again.",
|
2024-02-09 10:52:08 +00:00
|
|
|
});
|
|
|
|
});
|
2024-01-19 15:25:03 +00:00
|
|
|
};
|
|
|
|
|
2023-11-29 13:37:33 +00:00
|
|
|
const handleGoToWorkspace = async () => {
|
|
|
|
setIsGoingToWorkspace(true);
|
2024-02-09 10:52:08 +00:00
|
|
|
await handleSignInRedirection().finally(() => {
|
|
|
|
captureEvent(PASSWORD_CREATE_SKIPPED, {
|
|
|
|
state: "SUCCESS",
|
|
|
|
first_time: false,
|
|
|
|
});
|
|
|
|
setIsGoingToWorkspace(false);
|
|
|
|
});
|
2023-11-29 13:37:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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 } }) => (
|
2024-01-30 14:41:16 +00:00
|
|
|
<div className="relative flex items-center rounded-md bg-onboarding-background-200">
|
|
|
|
<Input
|
|
|
|
type={showPassword ? "text" : "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
|
|
|
|
/>
|
|
|
|
{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>
|
2024-01-19 15:25:03 +00:00
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<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>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|