mirror of
https://github.com/makeplane/plane.git
synced 2026-07-14 06:25:58 +02:00
178 lines
6.2 KiB
TypeScript
178 lines
6.2 KiB
TypeScript
"use client";
|
|
|
|
import { FC, useEffect, useRef, useState } from "react";
|
|
import { observer } from "mobx-react";
|
|
import { CircleCheck, XCircle } from "lucide-react";
|
|
import { Button, Input, Spinner } from "@plane/ui";
|
|
// helpers
|
|
import { EAuthSteps } 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";
|
|
|
|
const authService = new AuthService();
|
|
|
|
type TMobileAuthUniqueCodeForm = {
|
|
email: string;
|
|
handleEmail: (value: string) => void;
|
|
handleAuthStep: (value: EAuthSteps) => void;
|
|
generateEmailUniqueCode: (email: string) => Promise<{ code: string } | undefined>;
|
|
};
|
|
|
|
type TFormValues = {
|
|
email: string;
|
|
code: string;
|
|
};
|
|
|
|
const defaultFormValues: TFormValues = {
|
|
email: "",
|
|
code: "",
|
|
};
|
|
|
|
const defaultResetTimerValue = 5;
|
|
|
|
export const MobileAuthUniqueCodeForm: FC<TMobileAuthUniqueCodeForm> = observer((props) => {
|
|
const { email, handleEmail, handleAuthStep, generateEmailUniqueCode } = props;
|
|
// ref
|
|
const authFormRef = useRef<HTMLFormElement>(null);
|
|
// hooks
|
|
const { timer: resendTimerCode, setTimer: setResendCodeTimer } = useTimer(0);
|
|
// states
|
|
const [csrfPromise, setCsrfPromise] = useState<Promise<{ csrf_token: string }> | undefined>(undefined);
|
|
const [formData, setFormData] = useState<TFormValues>({ ...defaultFormValues, email });
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
const [isRequestingNewCode, setIsRequestingNewCode] = useState(false);
|
|
// derived values
|
|
const isRequestNewCodeDisabled = isRequestingNewCode || resendTimerCode > 0;
|
|
const isButtonDisabled = isRequestingNewCode || !formData.code || isSubmitting;
|
|
|
|
useEffect(() => {
|
|
if (csrfPromise === undefined) {
|
|
const promise = authService.requestCSRFToken();
|
|
setCsrfPromise(promise);
|
|
}
|
|
}, [csrfPromise]);
|
|
|
|
const handleFormChange = (key: keyof TFormValues, value: string) =>
|
|
setFormData((prev) => ({ ...prev, [key]: value }));
|
|
|
|
const handleCSRFToken = async () => {
|
|
if (!authFormRef || !authFormRef.current) return;
|
|
const token = await csrfPromise;
|
|
if (!token?.csrf_token) return;
|
|
const csrfElement = authFormRef.current.querySelector("input[name=csrfmiddlewaretoken]");
|
|
csrfElement?.setAttribute("value", token?.csrf_token);
|
|
};
|
|
|
|
const generateNewEmailUniqueCode = 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);
|
|
}
|
|
};
|
|
|
|
const handleEmailClear = () => {
|
|
handleEmail("");
|
|
handleAuthStep(EAuthSteps.EMAIL);
|
|
};
|
|
|
|
return (
|
|
<form
|
|
ref={authFormRef}
|
|
className="mt-5 space-y-4"
|
|
method="POST"
|
|
action={`${API_BASE_URL}/auth/mobile/magic-sign-in/`}
|
|
onSubmit={async (event) => {
|
|
event.preventDefault(); // Prevent form from submitting by default
|
|
setIsSubmitting(true);
|
|
await handleCSRFToken();
|
|
if (authFormRef.current) authFormRef.current.submit();
|
|
}}
|
|
>
|
|
<input type="hidden" name="csrfmiddlewaretoken" />
|
|
<input type="hidden" value={formData.email} name="email" />
|
|
|
|
<div className="space-y-1">
|
|
<label className="text-sm font-medium text-onboarding-text-300" htmlFor="email">
|
|
Email
|
|
</label>
|
|
<div
|
|
className={`relative flex items-center rounded-md bg-onboarding-background-200 border border-onboarding-border-100`}
|
|
>
|
|
<Input
|
|
id="email"
|
|
name="email"
|
|
type="email"
|
|
value={formData.email}
|
|
onChange={(e) => handleFormChange("email", e.target.value)}
|
|
placeholder="name@company.com"
|
|
className={`disable-autofill-style h-[46px] w-full placeholder:text-onboarding-text-400 border-0`}
|
|
disabled
|
|
/>
|
|
{formData.email.length > 0 && (
|
|
<XCircle
|
|
className="absolute right-3 h-5 w-5 stroke-custom-text-400 hover:cursor-pointer"
|
|
onClick={handleEmailClear}
|
|
/>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<div className="space-y-1">
|
|
<label className="text-sm text-onboarding-text-300 font-medium" htmlFor="code">
|
|
Unique code
|
|
</label>
|
|
<div className="relative flex items-center rounded-md bg-onboarding-background-200">
|
|
<Input
|
|
type={"text"}
|
|
name="code"
|
|
value={formData.code}
|
|
onChange={(e) => handleFormChange("code", e.target.value)}
|
|
placeholder="gets-sets-flys"
|
|
className="disable-autofill-style h-[46px] w-full border border-onboarding-border-100 !bg-onboarding-background-200 pr-12 placeholder:text-onboarding-text-400"
|
|
autoComplete="on"
|
|
autoFocus
|
|
/>
|
|
</div>
|
|
<div className="flex w-full items-center justify-between px-1 text-xs pt-1">
|
|
<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"
|
|
onClick={() => generateNewEmailUniqueCode(email)}
|
|
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>
|
|
|
|
<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>
|
|
</form>
|
|
);
|
|
});
|