From 9f2d68e1ec324c6eee2f4c27a9bddb1f774bcc2c Mon Sep 17 00:00:00 2001 From: maksimowiczm Date: Wed, 17 Jan 2024 15:06:34 +0100 Subject: [PATCH] Login using Remix fetcher form --- app/routes/user.login.tsx | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/app/routes/user.login.tsx b/app/routes/user.login.tsx index 6c75c1a..6b7b080 100644 --- a/app/routes/user.login.tsx +++ b/app/routes/user.login.tsx @@ -1,6 +1,6 @@ -import React, { useState } from "react"; -import { Button, Checkbox, Input, Link, Spacer } from "@nextui-org/react"; -import { useActionData, useNavigate } from "@remix-run/react"; +import React, { useEffect, useState } from "react"; +import { Button, Checkbox, Input, Spacer } from "@nextui-org/react"; +import { useFetcher } from "@remix-run/react"; import type { ActionFunctionArgs } from "@remix-run/node"; import type { handleLoginRequest } from "~/actions/handleLogin"; import handleLogin from "~/actions/handleLogin"; @@ -14,7 +14,7 @@ export const action = async ({ request }: ActionFunctionArgs) => { const formData = await request.formData(); const data = Object.fromEntries(formData); - if (data.email === undefined || data.password === undefined) { + if (!data.email || !data.password) { return errorHandler([]); } @@ -28,35 +28,38 @@ export const action = async ({ request }: ActionFunctionArgs) => { return handleLogin(loginRequest); }; +interface LoginErrors { + errors?: ApiErrorResponse[]; +} + export default function Login() { - const actionData = useActionData() as unknown as { - errors?: ApiErrorResponse[]; - }; - const error = actionData?.errors?.hasError( - ApiErrors.LoginErrors.InvalidEmailOrPassword - ); + const fetcher = useFetcher(); + const data = fetcher.data as LoginErrors | undefined; + const error = + data?.errors?.hasError(ApiErrors.LoginErrors.InvalidEmailOrPassword) ?? + false; const [checked, setChecked] = useState(false); return ( -
+
@@ -69,9 +72,14 @@ export default function Login() {
- - +
); }