2024-01-19 15:25:03 +00:00
|
|
|
import React, { useState } from "react";
|
2023-11-29 13:37:33 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
// services
|
|
|
|
import { AuthService } from "services/auth.service";
|
|
|
|
// hooks
|
2024-02-09 10:52:08 +00:00
|
|
|
import { useEventTracker } from "hooks/store";
|
2023-11-29 13:37:33 +00:00
|
|
|
// ui
|
2024-03-06 08:48:41 +00:00
|
|
|
import { Button, Input, TOAST_TYPE, setToast } from "@plane/ui";
|
2023-11-29 13:37:33 +00:00
|
|
|
// helpers
|
|
|
|
import { checkEmailValidity } from "helpers/string.helper";
|
2024-03-06 08:48:41 +00:00
|
|
|
// components
|
2024-01-19 15:25:03 +00:00
|
|
|
import { ESignUpSteps } from "components/account";
|
2024-03-06 08:48:41 +00:00
|
|
|
// constants
|
2024-02-09 10:52:08 +00:00
|
|
|
import { PASSWORD_CREATE_SELECTED, PASSWORD_CREATE_SKIPPED, SETUP_PASSWORD } from "constants/event-tracker";
|
2024-01-30 14:41:16 +00:00
|
|
|
// icons
|
|
|
|
import { Eye, EyeOff } from "lucide-react";
|
2023-11-29 13:37:33 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
email: string;
|
2024-01-19 15:25:03 +00:00
|
|
|
handleStepChange: (step: ESignUpSteps) => void;
|
2023-11-29 13:37:33 +00:00
|
|
|
handleSignInRedirection: () => Promise<void>;
|
|
|
|
};
|
|
|
|
|
|
|
|
type TCreatePasswordFormValues = {
|
|
|
|
email: string;
|
|
|
|
password: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues: TCreatePasswordFormValues = {
|
|
|
|
email: "",
|
|
|
|
password: "",
|
|
|
|
};
|
|
|
|
|
|
|
|
// services
|
|
|
|
const authService = new AuthService();
|
|
|
|
|
2024-01-19 15:25:03 +00:00
|
|
|
export const SignUpOptionalSetPasswordForm: React.FC<Props> = (props) => {
|
|
|
|
const { email, handleSignInRedirection } = props;
|
|
|
|
// 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();
|
2023-11-29 13:37:33 +00:00
|
|
|
// form info
|
|
|
|
const {
|
|
|
|
control,
|
|
|
|
formState: { errors, isSubmitting, isValid },
|
|
|
|
handleSubmit,
|
|
|
|
} = 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 () => {
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.SUCCESS,
|
2023-11-29 13:37:33 +00:00
|
|
|
title: "Success!",
|
|
|
|
message: "Password created successfully.",
|
|
|
|
});
|
2024-02-09 10:52:08 +00:00
|
|
|
captureEvent(SETUP_PASSWORD, {
|
|
|
|
state: "SUCCESS",
|
|
|
|
first_time: true,
|
|
|
|
});
|
2023-11-29 13:37:33 +00:00
|
|
|
await handleSignInRedirection();
|
|
|
|
})
|
2024-02-09 10:52:08 +00:00
|
|
|
.catch((err) => {
|
|
|
|
captureEvent(SETUP_PASSWORD, {
|
|
|
|
state: "FAILED",
|
|
|
|
first_time: true,
|
|
|
|
});
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
2023-11-29 13:37:33 +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
|
|
|
};
|
|
|
|
|
2024-01-19 15:25:03 +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: true,
|
|
|
|
});
|
|
|
|
setIsGoingToWorkspace(false);
|
|
|
|
});
|
2024-01-19 15:25:03 +00:00
|
|
|
};
|
2023-12-06 11:12:57 +00:00
|
|
|
|
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">Moving to the runway</h1>
|
|
|
|
<p className="mt-2.5 text-center text-sm text-onboarding-text-200">
|
|
|
|
Let{"'"}s set a password so
|
|
|
|
<br />
|
|
|
|
you can do away with codes.
|
|
|
|
</p>
|
|
|
|
<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"
|
2023-12-10 10:18:10 +00:00
|
|
|
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 } }) => (
|
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}
|
|
|
|
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">
|
|
|
|
This password will continue to be your account{"'"}s password.
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div className="space-y-2.5">
|
|
|
|
<Button
|
|
|
|
type="submit"
|
|
|
|
variant="primary"
|
|
|
|
className="w-full"
|
|
|
|
size="xl"
|
|
|
|
disabled={!isValid}
|
|
|
|
loading={isSubmitting}
|
|
|
|
>
|
|
|
|
Set password
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
variant="outline-primary"
|
|
|
|
className="w-full"
|
|
|
|
size="xl"
|
|
|
|
onClick={handleGoToWorkspace}
|
|
|
|
loading={isGoingToWorkspace}
|
|
|
|
>
|
|
|
|
Skip to setup
|
|
|
|
</Button>
|
|
|
|
</div>
|
2023-11-29 13:37:33 +00:00
|
|
|
</form>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|