Show Login and Ask an organizer sent and error states.
Connect the live modals to the drawn sent, expired, invalid-email, and send-failed screens, pin the phone footer above the keyboard, and send the light login email.
This commit is contained in:
1 parent
44010c7b73
commit
27d74e510c
29 files changed
+771
-281
No files matched your search
@@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { useCallback, useId, useState } from "react";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { usePathname, useRouter, useSearchParams } from "next/navigation";
|
||||
import { useTranslation } from "../../../contexts/MessagesContext";
|
||||
import Button from "../../buttons/Button";
|
||||
@@ -11,6 +11,7 @@ import Alert from "../Alert";
|
||||
import { requestMagicLink } from "../../../../lib/create/api";
|
||||
import { EMAIL_MAX_LEN } from "../../../../lib/create/isValidCreateFlowSaveEmail";
|
||||
import { buildCreateFlowDraftPayload } from "../../../../lib/create/buildCreateFlowDraftPayload";
|
||||
import { MAGIC_LINK_TTL_MINUTES } from "../../../../lib/privacyRetention";
|
||||
import { safeInternalPath } from "../../../../lib/safeInternalPath";
|
||||
import {
|
||||
readAnonymousCreateFlowState,
|
||||
@@ -43,6 +44,7 @@ function MailIconInline() {
|
||||
}
|
||||
|
||||
const EMAIL_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
const LOGIN_FORM_ID = "login-magic-link-form";
|
||||
|
||||
export type LoginFormVariant =
|
||||
| "default"
|
||||
@@ -50,13 +52,27 @@ export type LoginFormVariant =
|
||||
| "keepRule"
|
||||
| "invite";
|
||||
|
||||
function isExpiredLinkError(errorParam: string | null): boolean {
|
||||
return errorParam === "expired_link" || errorParam === "invalid_link";
|
||||
}
|
||||
|
||||
function loginFormHeading(
|
||||
variant: LoginFormVariant,
|
||||
sent: boolean,
|
||||
linkExpired: boolean,
|
||||
email: string,
|
||||
t: (_key: string) => string,
|
||||
): { title: string; description: string } {
|
||||
if (sent) {
|
||||
return { title: t("successTitle"), description: t("successBody") };
|
||||
return {
|
||||
title: t("successTitle"),
|
||||
description: t("sentBody")
|
||||
.replace("{email}", email)
|
||||
.replace("{minutes}", String(MAGIC_LINK_TTL_MINUTES)),
|
||||
};
|
||||
}
|
||||
if (linkExpired) {
|
||||
return { title: t("expiredTitle"), description: t("expiredBody") };
|
||||
}
|
||||
if (variant === "saveProgress") {
|
||||
return {
|
||||
@@ -97,30 +113,35 @@ export type LoginFormProps = {
|
||||
magicLinkNextPath?: string;
|
||||
/** `keepRule`: Continue without saving. */
|
||||
onDismiss?: () => void;
|
||||
/** Sent-state Close, and the dialog chrome close. */
|
||||
onClose?: () => void;
|
||||
};
|
||||
|
||||
export default function LoginForm({
|
||||
variant = "default",
|
||||
magicLinkNextPath,
|
||||
onDismiss,
|
||||
onClose,
|
||||
}: LoginFormProps) {
|
||||
const t = useTranslation("pages.login");
|
||||
const tFooter = useTranslation("footer");
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const searchParams = useSearchParams();
|
||||
const emailErrorId = useId();
|
||||
|
||||
const [email, setEmail] = useState("");
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [emailError, setEmailError] = useState("");
|
||||
const [formError, setFormError] = useState("");
|
||||
const [sent, setSent] = useState(false);
|
||||
const [linkExpired, setLinkExpired] = useState(() =>
|
||||
isExpiredLinkError(searchParams.get("error")),
|
||||
);
|
||||
|
||||
const nextParam = searchParams.get("next");
|
||||
const errorParam = searchParams.get("error");
|
||||
|
||||
const heading = loginFormHeading(variant, sent, t);
|
||||
const heading = loginFormHeading(variant, sent, linkExpired, email, t);
|
||||
|
||||
/** Drop `error` from the URL so URL-driven messages don’t linger after a new attempt. */
|
||||
const stripErrorQuery = useCallback(() => {
|
||||
@@ -166,6 +187,7 @@ export default function LoginForm({
|
||||
setTransferPendingFlag();
|
||||
}
|
||||
setEmail(trimmed);
|
||||
setLinkExpired(false);
|
||||
setSent(true);
|
||||
} catch {
|
||||
setFormError(t("errors.network"));
|
||||
@@ -181,115 +203,143 @@ export default function LoginForm({
|
||||
variant,
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
if (isExpiredLinkError(errorParam)) {
|
||||
setLinkExpired(true);
|
||||
}
|
||||
}, [errorParam]);
|
||||
|
||||
const urlErrorMessage =
|
||||
errorParam === "expired_link"
|
||||
? t("errors.expiredLink")
|
||||
: errorParam === "stakeholder_wrong_account"
|
||||
? t("errors.stakeholderWrongAccount")
|
||||
: errorParam === "invalid_link" || errorParam === "server"
|
||||
? errorParam === "server"
|
||||
? t("errors.serverError")
|
||||
: t("errors.invalidLink")
|
||||
: "";
|
||||
errorParam === "stakeholder_wrong_account"
|
||||
? t("errors.stakeholderWrongAccount")
|
||||
: errorParam === "server"
|
||||
? t("errors.serverError")
|
||||
: "";
|
||||
|
||||
const titleId = "login-modal-heading";
|
||||
const submitLabel = linkExpired ? t("sendNewLink") : t("sendMagicLink");
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-6 pt-2">
|
||||
<div className="flex flex-col gap-3">
|
||||
<div className="relative flex h-12 w-12 shrink-0 items-center justify-center rounded-full bg-[var(--color-surface-invert-brand-primary)]">
|
||||
<MailIconInline />
|
||||
<div className="flex min-h-0 flex-1 flex-col gap-6 pt-2">
|
||||
<div className="flex min-h-0 flex-1 flex-col gap-6 overflow-y-auto">
|
||||
<div className="flex flex-col gap-3">
|
||||
<div className="relative flex h-12 w-12 shrink-0 items-center justify-center rounded-full bg-[var(--color-surface-invert-brand-primary)]">
|
||||
<MailIconInline />
|
||||
</div>
|
||||
<ContentLockup
|
||||
titleId={titleId}
|
||||
title={heading.title}
|
||||
description={heading.description}
|
||||
variant="login"
|
||||
alignment="left"
|
||||
/>
|
||||
</div>
|
||||
<ContentLockup
|
||||
titleId={titleId}
|
||||
title={heading.title}
|
||||
description={heading.description}
|
||||
variant="login"
|
||||
alignment="left"
|
||||
/>
|
||||
|
||||
{(urlErrorMessage || formError) && (
|
||||
<div className="pointer-events-none fixed inset-x-0 top-4 z-[10000] flex justify-center px-4 md:top-6">
|
||||
<div className="pointer-events-auto flex w-full max-w-[560px] flex-col gap-2">
|
||||
{urlErrorMessage ? (
|
||||
<Alert
|
||||
type="banner"
|
||||
status="danger"
|
||||
size="s"
|
||||
title={urlErrorMessage}
|
||||
hasBodyText={false}
|
||||
hasLeadingIcon
|
||||
onClose={() => {
|
||||
stripErrorQuery();
|
||||
}}
|
||||
className="w-full"
|
||||
/>
|
||||
) : null}
|
||||
{formError ? (
|
||||
<Alert
|
||||
type="banner"
|
||||
status="danger"
|
||||
size="s"
|
||||
title={formError}
|
||||
hasBodyText={false}
|
||||
hasLeadingIcon
|
||||
onClose={() => {
|
||||
setFormError("");
|
||||
}}
|
||||
className="w-full"
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!sent ? (
|
||||
<form
|
||||
id={LOGIN_FORM_ID}
|
||||
className="flex flex-col gap-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
void sendLink();
|
||||
}}
|
||||
noValidate
|
||||
>
|
||||
<TextInput
|
||||
label={t("emailLabel")}
|
||||
placeholder={t("emailPlaceholder")}
|
||||
type="email"
|
||||
name="email"
|
||||
autoComplete="email"
|
||||
inputMode="email"
|
||||
value={email}
|
||||
onChange={(e) => {
|
||||
setEmail(e.target.value);
|
||||
setEmailError("");
|
||||
stripErrorQuery();
|
||||
}}
|
||||
disabled={submitting}
|
||||
error={Boolean(emailError)}
|
||||
textHint={emailError || false}
|
||||
showHelpIcon={false}
|
||||
maxLength={EMAIL_MAX_LEN}
|
||||
/>
|
||||
</form>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
{(urlErrorMessage || formError) && (
|
||||
<div className="pointer-events-none fixed inset-x-0 top-4 z-[10000] flex justify-center px-4 md:top-6">
|
||||
<div className="pointer-events-auto flex w-full max-w-[560px] flex-col gap-2">
|
||||
{urlErrorMessage ? (
|
||||
<Alert
|
||||
type="banner"
|
||||
status="danger"
|
||||
size="s"
|
||||
title={urlErrorMessage}
|
||||
hasBodyText={false}
|
||||
hasLeadingIcon
|
||||
onClose={() => {
|
||||
stripErrorQuery();
|
||||
}}
|
||||
className="w-full"
|
||||
/>
|
||||
) : null}
|
||||
{formError ? (
|
||||
<Alert
|
||||
type="banner"
|
||||
status="danger"
|
||||
size="s"
|
||||
title={formError}
|
||||
hasBodyText={false}
|
||||
hasLeadingIcon
|
||||
onClose={() => {
|
||||
setFormError("");
|
||||
}}
|
||||
className="w-full"
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!sent ? (
|
||||
<form
|
||||
className="flex flex-col gap-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
void sendLink();
|
||||
}}
|
||||
noValidate
|
||||
>
|
||||
<TextInput
|
||||
label={t("emailLabel")}
|
||||
placeholder={t("emailPlaceholder")}
|
||||
type="email"
|
||||
name="email"
|
||||
autoComplete="email"
|
||||
inputMode="email"
|
||||
value={email}
|
||||
onChange={(e) => {
|
||||
setEmail(e.target.value);
|
||||
stripErrorQuery();
|
||||
}}
|
||||
disabled={submitting}
|
||||
error={Boolean(emailError)}
|
||||
showHelpIcon
|
||||
maxLength={EMAIL_MAX_LEN}
|
||||
/>
|
||||
{emailError ? (
|
||||
<p
|
||||
id={emailErrorId}
|
||||
role="alert"
|
||||
aria-live="polite"
|
||||
className="text-[14px] text-[var(--color-border-default-utility-negative)]"
|
||||
<div className="flex shrink-0 flex-col gap-4">
|
||||
{sent ? (
|
||||
<>
|
||||
<Button
|
||||
type="button"
|
||||
size="large"
|
||||
buttonType="filled"
|
||||
palette="default"
|
||||
autoFocus
|
||||
onClick={() => onClose?.()}
|
||||
className="w-full !justify-center text-center px-[var(--space-400)] py-[var(--space-300)]"
|
||||
>
|
||||
{emailError}
|
||||
</p>
|
||||
) : null}
|
||||
<div className="flex flex-col gap-4 max-md:sticky max-md:bottom-0 max-md:bg-[var(--color-surface-default-primary)] max-md:pt-2">
|
||||
{t("close")}
|
||||
</Button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
void sendLink();
|
||||
}}
|
||||
disabled={submitting}
|
||||
className="text-center text-small-paragraph text-[var(--color-content-default-tertiary)] underline decoration-solid underline-offset-2"
|
||||
>
|
||||
{t("sendAgain")}
|
||||
</button>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Button
|
||||
type="submit"
|
||||
form={LOGIN_FORM_ID}
|
||||
size="large"
|
||||
buttonType="filled"
|
||||
palette="default"
|
||||
disabled={submitting}
|
||||
className="w-full !justify-center text-center px-[var(--space-400)] py-[var(--space-300)]"
|
||||
>
|
||||
{t("sendMagicLink")}
|
||||
{submitLabel}
|
||||
</Button>
|
||||
{onDismiss ? (
|
||||
<Button
|
||||
@@ -321,9 +371,9 @@ export default function LoginForm({
|
||||
</Link>
|
||||
{t("legalSuffix")}
|
||||
</p>
|
||||
</div>
|
||||
</form>
|
||||
) : null}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user