plane/web/components/account/auth-forms/sign-up-root.tsx

134 lines
4.1 KiB
TypeScript
Raw Normal View History

2024-04-30 14:00:42 +00:00
import { FC, useEffect, useState } from "react";
2024-04-30 11:34:49 +00:00
import { observer } from "mobx-react";
import { useRouter } from "next/router";
// types
import { IEmailCheckData } from "@plane/types";
// ui
import { Spinner, TOAST_TYPE, setToast } from "@plane/ui";
// components
import {
AuthHeader,
AuthBanner,
AuthEmailForm,
2024-05-01 08:29:11 +00:00
AuthUniqueCodeForm,
2024-04-30 11:34:49 +00:00
AuthPasswordForm,
OAuthOptions,
TermsAndConditions,
} from "@/components/account";
// helpers
2024-04-30 14:00:42 +00:00
import {
EAuthModes,
EAuthSteps,
EAuthenticationErrorCodes,
EErrorAlertType,
TAuthErrorInfo,
authErrorHandler,
} from "@/helpers/authentication.helper";
2024-04-30 11:34:49 +00:00
// hooks
import { useInstance } from "@/hooks/store";
// services
import { AuthService } from "@/services/auth.service";
// service initialization
const authService = new AuthService();
export const SignUpAuthRoot: FC = observer(() => {
//router
const router = useRouter();
2024-04-30 14:00:42 +00:00
const { email: emailParam, invitation_id, slug: workspaceSlug, error_code, error_message } = router.query;
2024-04-30 11:34:49 +00:00
// states
const [authStep, setAuthStep] = useState<EAuthSteps>(EAuthSteps.EMAIL);
const [email, setEmail] = useState(emailParam ? emailParam.toString() : "");
const [isLoading, setIsLoading] = useState(false);
const [errorInfo, setErrorInfo] = useState<TAuthErrorInfo | undefined>(undefined);
// hooks
const { instance } = useInstance();
// derived values
const authMode = EAuthModes.SIGN_UP;
const isSmtpConfigured = instance?.config?.is_smtp_configured;
2024-04-30 14:00:42 +00:00
useEffect(() => {
if (error_code && error_message) {
const errorhandler = authErrorHandler(
error_code?.toString() as EAuthenticationErrorCodes,
error_message?.toString()
);
if (errorhandler) setErrorInfo(errorhandler);
}
}, [error_code, error_message]);
2024-04-30 11:34:49 +00:00
2024-04-30 14:00:42 +00:00
// email verification
2024-04-30 11:34:49 +00:00
const handleEmailVerification = async (data: IEmailCheckData) => {
setEmail(data.email);
await authService
.signUpEmailCheck(data)
.then(() => {
if (isSmtpConfigured) setAuthStep(EAuthSteps.UNIQUE_CODE);
2024-04-30 11:34:49 +00:00
else setAuthStep(EAuthSteps.PASSWORD);
})
2024-04-30 14:00:42 +00:00
.catch((error) => {
const errorhandler = authErrorHandler(error?.error_code, error?.error_message);
if (errorhandler) {
setErrorInfo(errorhandler);
return;
}
2024-04-30 11:34:49 +00:00
setToast({
type: TOAST_TYPE.ERROR,
title: "Error!",
2024-04-30 14:00:42 +00:00
message: error?.error_message ?? "Something went wrong. Please try again.",
2024-04-30 11:34:49 +00:00
});
});
};
const isOAuthEnabled =
instance?.config && (instance?.config?.is_google_enabled || instance?.config?.is_github_enabled);
if (isLoading)
return (
<div className="flex h-full w-full items-center justify-center">
<Spinner />
</div>
);
return (
<div className="relative max-w-lg px-5 mx-auto flex flex-col space-y-6">
2024-04-30 14:00:42 +00:00
<AuthHeader
workspaceSlug={workspaceSlug?.toString() || undefined}
invitationId={invitation_id?.toString() || undefined}
invitationEmail={email || undefined}
authMode={EAuthModes.SIGN_UP}
currentAuthStep={authStep}
handleLoader={setIsLoading}
/>
{errorInfo && errorInfo?.type === EErrorAlertType.BANNER_ALERT && (
<AuthBanner bannerData={errorInfo} handleBannerData={(value) => setErrorInfo(value)} />
)}
{authStep === EAuthSteps.EMAIL && <AuthEmailForm defaultEmail={email} onSubmit={handleEmailVerification} />}
{authStep === EAuthSteps.UNIQUE_CODE && (
2024-05-01 08:29:11 +00:00
<AuthUniqueCodeForm
2024-04-30 14:00:42 +00:00
email={email}
handleEmailClear={() => {
setEmail("");
setAuthStep(EAuthSteps.EMAIL);
}}
submitButtonText="Continue"
mode={authMode}
2024-04-30 11:34:49 +00:00
/>
2024-04-30 14:00:42 +00:00
)}
{authStep === EAuthSteps.PASSWORD && (
<AuthPasswordForm
email={email}
handleEmailClear={() => {
setEmail("");
setAuthStep(EAuthSteps.EMAIL);
}}
handleStepChange={(step) => setAuthStep(step)}
mode={authMode}
/>
)}
{isOAuthEnabled && <OAuthOptions />}
<TermsAndConditions isSignUp={authMode === EAuthModes.SIGN_UP} />
</div>
2024-04-30 11:34:49 +00:00
);
});