Skip to content

Commit

Permalink
Wrap login error in suspense
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-sherman committed Sep 11, 2024
1 parent 9add1fe commit 1516095
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions packages/frontpage/app/(auth)/login/_lib/form.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { startTransition, useActionState } from "react";
import { startTransition, Suspense, useActionState } from "react";
import { loginAction } from "./action";
import { Label } from "@/lib/components/ui/label";
import { Input } from "@/lib/components/ui/input";
Expand All @@ -11,8 +11,6 @@ import { CrossCircledIcon } from "@radix-ui/react-icons";

export function LoginForm() {
const [state, action, isPending] = useActionState(loginAction, null);
const searchParams = useSearchParams();
const error = state?.error ?? searchParams.get("error");

return (
<>
Expand All @@ -36,13 +34,22 @@ export function LoginForm() {
</Button>
</div>
</form>
{error ? (
<Alert variant="destructive">
<CrossCircledIcon className="h-4 w-4" />
<AlertTitle>Login error</AlertTitle>
<AlertDescription>{error}</AlertDescription>
</Alert>
) : null}
<Suspense>
<LoginError errorState={state?.error} />
</Suspense>
</>
);
}

function LoginError({ errorState }: { errorState?: string }) {
const searchParams = useSearchParams();
const error = errorState ?? searchParams.get("error");
if (!error) return null;
return (
<Alert variant="destructive">
<CrossCircledIcon className="h-4 w-4" />
<AlertTitle>Login error</AlertTitle>
<AlertDescription>{error}</AlertDescription>
</Alert>
);
}

0 comments on commit 1516095

Please sign in to comment.