From 97b92fa60ca8d0a6cc21fc85df282614a64192e1 Mon Sep 17 00:00:00 2001 From: Arnaud Fauconnet Date: Sun, 23 Oct 2022 15:09:28 +0200 Subject: [PATCH] Made Login.tsx use the useForm hook and expanded the error page so that it can take another error if other shit happens. AuthComponent now doesn't have a hardcoded isLogged var, but looks at the query. --- package.json | 6 ++- src/components/ErrorPage.tsx | 8 ++- src/components/Login.tsx | 73 ++++++++++++++++++++-------- src/components/lib/AuthComponent.tsx | 11 +++-- 4 files changed, 72 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index b89f5ae..a94237a 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "@babel/preset-react": "^7.18.6", "@emotion/react": "^11.10.4", "@emotion/styled": "^11.10.4", + "@hookform/resolvers": "^2.9.10", "@mui/material": "^5.10.9", "@types/jest": "^29.1.1", "@types/node": "^18.8.2", @@ -21,12 +22,14 @@ "@types/react": "^18.0.21", "@types/react-dom": "^18.0.6", "@types/react-router-dom": "^5.3.3", + "@types/yup": "^0.32.0", "babel-loader": "^8.2.5", "css-loader": "^6.7.1", "html-webpack-plugin": "^5.5.0", "mini-css-extract-plugin": "^2.6.1", "react": "^18.2.0", "react-dom": "^18.2.0", + "react-hook-form": "^7.38.0", "react-router-dom": "^6.4.2", "sass": "^1.55.0", "sass-loader": "^13.1.0", @@ -36,6 +39,7 @@ "typescript": "^4.8.4", "webpack": "^5.74.0", "webpack-cli": "^4.10.0", - "webpack-dev-server": "^4.11.1" + "webpack-dev-server": "^4.11.1", + "yup": "^0.32.11" } } diff --git a/src/components/ErrorPage.tsx b/src/components/ErrorPage.tsx index 8527716..e1796ed 100644 --- a/src/components/ErrorPage.tsx +++ b/src/components/ErrorPage.tsx @@ -2,8 +2,12 @@ import React from "react"; import { useRouteError } from "react-router-dom"; import "@scss/error.scss" -export default function ErrorPage() { - const error: any = useRouteError(); +type ErrorProps = { + err?: Error +} + +export default function ErrorPage({err} : ErrorProps) { + const error: any = err || useRouteError(); console.error(error); return ( diff --git a/src/components/Login.tsx b/src/components/Login.tsx index 11ea462..733f6fb 100644 --- a/src/components/Login.tsx +++ b/src/components/Login.tsx @@ -2,41 +2,76 @@ import React, { useState, ChangeEvent, MouseEvent, useEffect } from "react"; import { Card, Button, TextField } from "@mui/material" import "@scss/login.scss" import { Link, useNavigate } from "react-router-dom"; +import { yupResolver } from '@hookform/resolvers/yup'; +import * as yup from "yup"; +import { useForm } from "react-hook-form" + +type Inputs = { + email: string; + password: string; +}; + +const schema = yup.object().shape({ + email: yup.string().email("Please provide a valid email").required("No email was provided"), + password: yup.string().required("No password was provided"), +}).required(); export default function Login() { - const [email, setEmail] = useState(""); - const [password, setPassword] = useState(""); const navigate = useNavigate(); + const { register, formState: { errors }, handleSubmit } = useForm({ + resolver: yupResolver(schema), // yup, joi and even your own. + }); function hash(s: string) { return s; } - function handleEmail(e: ChangeEvent) { - setEmail(e.target.value); - console.log(email); + const {ref: emailRef, ...emailRegisterProps} = register("email", { + required: "error txt lol" + }); + + const emailProps = { + ...emailRegisterProps, + inputRef: emailRef, + className: "textField", + name: "email", + id: "email", + label: "Email", + error: !!errors.email, + helperText: errors?.email?.message, } - function handlePassword(e: ChangeEvent) { - setPassword(e.target.value); - console.log(hash(password)); + const {ref: passRef, ...passRegisterProps} = register("password"); + + const passProps = { + ...passRegisterProps, + inputRef: passRef, + className: "textField", + name: "password", + id: "password", + label: "Password", + error: !!errors.password, + helperText: errors?.password?.message, } - function buttonSubmit(e: MouseEvent) { - e.preventDefault(); - console.log(`Sumbitting username "${email}" and password "${hash(password)}"`) - navigate("/") + function onSubmit(data: Inputs) { + console.log("There will be an API call now with the following data"); + console.table(data); + setTimeout(() => navigate("/?logged"), 3000) } + return (
- -

Login

- - - -

Forgot your password?

-
+
+ +

Login

+ + + +

Forgot your password?

+
+
) } diff --git a/src/components/lib/AuthComponent.tsx b/src/components/lib/AuthComponent.tsx index 63b9508..378cafa 100644 --- a/src/components/lib/AuthComponent.tsx +++ b/src/components/lib/AuthComponent.tsx @@ -1,13 +1,14 @@ +import ErrorPage from "@components/ErrorPage"; import React, { useEffect } from "react"; -import { useNavigate } from "react-router-dom"; +import { useNavigate, useSearchParams } from "react-router-dom"; type AuthProps = { children: JSX.Element | JSX.Element[], } -export default function AuthComponent({children} : AuthProps){ +export default function AuthComponent({ children }: AuthProps) { const navigate = useNavigate(); - - const isLogged = true; + const searchParams = useSearchParams()[0]; + const isLogged = searchParams.has("logged"); useEffect(() => { // navigate needs to be wrapped in a useEffect so that it gets executed after the component is mounted. Otherwise it doesn't redirect @@ -17,4 +18,6 @@ export default function AuthComponent({children} : AuthProps){ if (isLogged) return <>{children}; + // else + // return ; } \ No newline at end of file