2024-05-08 17:31:20 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import { CircleCheck, XCircle } from "lucide-react";
|
2024-05-10 12:00:38 +00:00
|
|
|
import { Button, Input, Spinner } from "@plane/ui";
|
2024-05-08 17:31:20 +00:00
|
|
|
// helpers
|
|
|
|
import { EAuthModes } from "@/helpers/authentication.helper";
|
|
|
|
import { API_BASE_URL } from "@/helpers/common.helper";
|
|
|
|
// hooks
|
|
|
|
import useTimer from "@/hooks/use-timer";
|
|
|
|
// services
|
|
|
|
import { AuthService } from "@/services/auth.service";
|
|
|
|
|
2024-05-10 12:00:38 +00:00
|
|
|
// services
|
|
|
|
const authService = new AuthService();
|
|
|
|
|
|
|
|
type TAuthUniqueCodeForm = {
|
|
|
|
mode: EAuthModes;
|
2024-05-08 17:31:20 +00:00
|
|
|
email: string;
|
|
|
|
handleEmailClear: () => void;
|
2024-05-10 12:00:38 +00:00
|
|
|
generateEmailUniqueCode: (email: string) => Promise<{ code: string } | undefined>;
|
2024-05-08 17:31:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type TUniqueCodeFormValues = {
|
|
|
|
email: string;
|
|
|
|
code: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues: TUniqueCodeFormValues = {
|
|
|
|
email: "",
|
|
|
|
code: "",
|
|
|
|
};
|
|
|
|
|
2024-05-10 12:00:38 +00:00
|
|
|
export const AuthUniqueCodeForm: React.FC<TAuthUniqueCodeForm> = (props) => {
|
|
|
|
const { mode, email, handleEmailClear, generateEmailUniqueCode } = props;
|
|
|
|
// hooks
|
|
|
|
// const { captureEvent } = useEventTracker();
|
|
|
|
// derived values
|
|
|
|
const defaultResetTimerValue = 5;
|
2024-05-08 17:31:20 +00:00
|
|
|
// states
|
|
|
|
const [uniqueCodeFormData, setUniqueCodeFormData] = useState<TUniqueCodeFormValues>({ ...defaultValues, email });
|
|
|
|
const [isRequestingNewCode, setIsRequestingNewCode] = useState(false);
|
|
|
|
const [csrfToken, setCsrfToken] = useState<string | undefined>(undefined);
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
// timer
|
2024-05-10 12:00:38 +00:00
|
|
|
const { timer: resendTimerCode, setTimer: setResendCodeTimer } = useTimer(0);
|
2024-05-08 17:31:20 +00:00
|
|
|
|
|
|
|
const handleFormChange = (key: keyof TUniqueCodeFormValues, value: string) =>
|
|
|
|
setUniqueCodeFormData((prev) => ({ ...prev, [key]: value }));
|
|
|
|
|
2024-05-10 12:00:38 +00:00
|
|
|
const generateNewCode = async (email: string) => {
|
|
|
|
try {
|
|
|
|
setIsRequestingNewCode(true);
|
|
|
|
const uniqueCode = await generateEmailUniqueCode(email);
|
|
|
|
setResendCodeTimer(defaultResetTimerValue);
|
|
|
|
handleFormChange("code", uniqueCode?.code || "");
|
|
|
|
setIsRequestingNewCode(false);
|
|
|
|
} catch {
|
|
|
|
setResendCodeTimer(0);
|
|
|
|
console.error("Error while requesting new code");
|
|
|
|
setIsRequestingNewCode(false);
|
|
|
|
}
|
2024-05-08 17:31:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (csrfToken === undefined)
|
|
|
|
authService.requestCSRFToken().then((data) => data?.csrf_token && setCsrfToken(data.csrf_token));
|
|
|
|
}, [csrfToken]);
|
|
|
|
|
|
|
|
const isRequestNewCodeDisabled = isRequestingNewCode || resendTimerCode > 0;
|
|
|
|
const isButtonDisabled = isRequestingNewCode || !uniqueCodeFormData.code || isSubmitting;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<form
|
|
|
|
className="mt-5 space-y-4"
|
|
|
|
method="POST"
|
|
|
|
action={`${API_BASE_URL}/auth/${mode === EAuthModes.SIGN_IN ? "magic-sign-in" : "magic-sign-up"}/`}
|
|
|
|
onSubmit={() => setIsSubmitting(true)}
|
|
|
|
onError={() => setIsSubmitting(false)}
|
|
|
|
>
|
|
|
|
<input type="hidden" name="csrfmiddlewaretoken" value={csrfToken} />
|
2024-05-10 12:00:38 +00:00
|
|
|
<input type="hidden" value={uniqueCodeFormData.email} name="email" />
|
2024-05-08 17:31:20 +00:00
|
|
|
<div className="space-y-1">
|
|
|
|
<label className="text-sm font-medium text-onboarding-text-300" htmlFor="email">
|
|
|
|
Email
|
|
|
|
</label>
|
2024-05-10 12:00:38 +00:00
|
|
|
<div
|
|
|
|
className={`relative flex items-center rounded-md bg-onboarding-background-200 border border-onboarding-border-100`}
|
|
|
|
>
|
2024-05-08 17:31:20 +00:00
|
|
|
<Input
|
|
|
|
id="email"
|
|
|
|
name="email"
|
|
|
|
type="email"
|
|
|
|
value={uniqueCodeFormData.email}
|
|
|
|
onChange={(e) => handleFormChange("email", e.target.value)}
|
|
|
|
placeholder="name@company.com"
|
2024-05-10 12:00:38 +00:00
|
|
|
className={`disable-autofill-style h-[46px] w-full placeholder:text-onboarding-text-400 border-0`}
|
2024-05-09 12:16:31 +00:00
|
|
|
disabled
|
2024-05-08 17:31:20 +00:00
|
|
|
/>
|
|
|
|
{uniqueCodeFormData.email.length > 0 && (
|
2024-05-10 12:00:38 +00:00
|
|
|
<div className="flex-shrink-0 h-5 w-5 mr-2 bg-onboarding-background-200 hover:cursor-pointer">
|
|
|
|
<XCircle className="h-5 w-5 stroke-custom-text-400" onClick={handleEmailClear} />
|
|
|
|
</div>
|
2024-05-08 17:31:20 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-05-10 12:00:38 +00:00
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
<div className="space-y-1">
|
|
|
|
<label className="text-sm font-medium text-onboarding-text-300" htmlFor="code">
|
|
|
|
Unique code
|
|
|
|
</label>
|
|
|
|
<Input
|
|
|
|
name="code"
|
|
|
|
value={uniqueCodeFormData.code}
|
|
|
|
onChange={(e) => handleFormChange("code", e.target.value)}
|
|
|
|
placeholder="gets-sets-flys"
|
2024-05-10 12:00:38 +00:00
|
|
|
className="disable-autofill-style h-[46px] w-full border border-onboarding-border-100 !bg-onboarding-background-200 pr-12 placeholder:text-onboarding-text-400"
|
2024-05-08 17:31:20 +00:00
|
|
|
autoFocus
|
|
|
|
/>
|
2024-05-10 12:00:38 +00:00
|
|
|
<div className="flex w-full items-center justify-between px-1 text-xs pt-1">
|
2024-05-08 17:31:20 +00:00
|
|
|
<p className="flex items-center gap-1 font-medium text-green-700">
|
|
|
|
<CircleCheck height={12} width={12} />
|
|
|
|
Paste the code sent to your email
|
|
|
|
</p>
|
|
|
|
<button
|
|
|
|
type="button"
|
2024-05-10 12:00:38 +00:00
|
|
|
onClick={() => generateNewCode(uniqueCodeFormData.email)}
|
2024-05-08 17:31:20 +00:00
|
|
|
className={`${
|
|
|
|
isRequestNewCodeDisabled
|
|
|
|
? "text-onboarding-text-400"
|
|
|
|
: "font-medium text-custom-primary-300 hover:text-custom-primary-200"
|
|
|
|
}`}
|
|
|
|
disabled={isRequestNewCodeDisabled}
|
|
|
|
>
|
|
|
|
{resendTimerCode > 0
|
|
|
|
? `Resend in ${resendTimerCode}s`
|
|
|
|
: isRequestingNewCode
|
|
|
|
? "Requesting new code"
|
|
|
|
: "Resend"}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-05-10 12:00:38 +00:00
|
|
|
|
|
|
|
<div className="space-y-2.5">
|
|
|
|
<Button type="submit" variant="primary" className="w-full" size="lg" disabled={isButtonDisabled}>
|
|
|
|
{isRequestingNewCode ? "Sending code" : isSubmitting ? <Spinner height="20px" width="20px" /> : "Continue"}
|
|
|
|
</Button>
|
|
|
|
</div>
|
2024-05-08 17:31:20 +00:00
|
|
|
</form>
|
|
|
|
);
|
|
|
|
};
|