Formatted the code of components

This commit is contained in:
Karma Riuk 2022-10-26 22:40:09 +02:00
parent de07c92579
commit 61d31fa3c4
3 changed files with 94 additions and 62 deletions

View File

@ -2,28 +2,36 @@ import React, { MouseEvent } from "react";
import { Card, Button, TextField } from "@mui/material"; import { Card, Button, TextField } from "@mui/material";
import "@scss/forgot_password.scss"; import "@scss/forgot_password.scss";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import { yupResolver } from '@hookform/resolvers/yup'; import { yupResolver } from "@hookform/resolvers/yup";
import * as yup from "yup"; import * as yup from "yup";
import { useForm } from "react-hook-form" import { useForm } from "react-hook-form";
type Inputs = { type Inputs = {
email: string; email: string;
password: string; password: string;
}; };
const schema = yup.object().shape({ const schema = yup
email: yup.string().email("Please provide a valid email").required("No email was provided") .object()
}).required(); .shape({
email: yup
.string()
.email("Please provide a valid email")
.required("No email was provided"),
})
.required();
export default function ForgotPassword() { export default function ForgotPassword() {
const navigate = useNavigate(); const navigate = useNavigate();
const { register, formState: { errors }, handleSubmit } = useForm<Inputs>({ const {
resolver: yupResolver(schema), // yup, joi and even your own. register,
formState: { errors },
handleSubmit,
} = useForm<Inputs>({
resolver: yupResolver(schema),
}); });
const { ref: emailRef, ...emailRegisterProps } = register("email");
const {ref: emailRef, ...emailRegisterProps} = register("email");
const emailProps = { const emailProps = {
...emailRegisterProps, ...emailRegisterProps,
inputRef: emailRef, inputRef: emailRef,
@ -32,17 +40,19 @@ export default function ForgotPassword() {
label: "Email", label: "Email",
error: !!errors.email, error: !!errors.email,
helperText: errors?.email?.message, helperText: errors?.email?.message,
} };
function buttonCancel(e: MouseEvent<HTMLButtonElement>) { function buttonCancel(e: MouseEvent<HTMLButtonElement>) {
e.preventDefault(); e.preventDefault();
navigate("/login") navigate("/login");
} }
function onSubmit(data: Inputs) { function onSubmit(data: Inputs) {
console.log("There will be an API call now with the following data"); console.log("There will be an API call now with the following data");
console.table(data); console.table(data);
console.log("You can now go to http://localhost:8080/reset to acces the reset form") console.log(
"You can now go to http://localhost:8080/reset to acces the reset form"
);
} }
return ( return (
@ -51,8 +61,8 @@ export default function ForgotPassword() {
<Card className="card"> <Card className="card">
<h3>Forgot Passowrd</h3> <h3>Forgot Passowrd</h3>
<p> <p>
If you have forgot your password, we can send you an email If you have forgot your password, we can send you an
with a link to reset it. email with a link to reset it.
</p> </p>
<TextField {...emailProps} /> <TextField {...emailProps} />
<div className="buttons"> <div className="buttons">
@ -63,11 +73,7 @@ export default function ForgotPassword() {
> >
Cancel Cancel
</Button> </Button>
<Button <Button type="submit" id="submit" variant="contained">
type="submit"
id="submit"
variant="contained"
>
Submit Submit
</Button> </Button>
</div> </div>

View File

@ -1,32 +1,42 @@
import React from "react"; import React from "react";
import { Card, Button, TextField } from "@mui/material" import { Card, Button, TextField } from "@mui/material";
import "@scss/login.scss" import "@scss/login.scss";
import { Link, useNavigate } from "react-router-dom"; import { Link, useNavigate } from "react-router-dom";
import { yupResolver } from '@hookform/resolvers/yup'; import { yupResolver } from "@hookform/resolvers/yup";
import * as yup from "yup"; import * as yup from "yup";
import { useForm } from "react-hook-form" import { useForm } from "react-hook-form";
type Inputs = { type Inputs = {
email: string; email: string;
password: string; password: string;
}; };
const schema = yup.object().shape({ const schema = yup
email: yup.string().email("Please provide a valid email").required("No email was provided"), .object()
password: yup.string().required("No password was provided"), .shape({
}).required(); 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() { export default function Login() {
const navigate = useNavigate(); const navigate = useNavigate();
const { register, formState: { errors }, handleSubmit } = useForm<Inputs>({ const {
resolver: yupResolver(schema), // yup, joi and even your own. register,
formState: { errors },
handleSubmit,
} = useForm<Inputs>({
resolver: yupResolver(schema),
}); });
function hash(s: string) { function hash(s: string) {
return `hash of '${s}' to be definied`; return `hash of '${s}' to be definied`;
} }
const {ref: emailRef, ...emailRegisterProps} = register("email"); const { ref: emailRef, ...emailRegisterProps } = register("email");
const emailProps = { const emailProps = {
...emailRegisterProps, ...emailRegisterProps,
@ -37,9 +47,9 @@ export default function Login() {
label: "Email", label: "Email",
error: !!errors.email, error: !!errors.email,
helperText: errors?.email?.message, helperText: errors?.email?.message,
} };
const {ref: passRef, ...passRegisterProps} = register("password"); const { ref: passRef, ...passRegisterProps } = register("password");
const passProps = { const passProps = {
...passRegisterProps, ...passRegisterProps,
@ -50,27 +60,30 @@ export default function Login() {
label: "Password", label: "Password",
error: !!errors.password, error: !!errors.password,
helperText: errors?.password?.message, helperText: errors?.password?.message,
} };
function onSubmit(data: Inputs) { function onSubmit(data: Inputs) {
console.log("There will be an API call now with the following data"); console.log("There will be an API call now with the following data");
data = { ...data, password: hash(data.password) } data = { ...data, password: hash(data.password) };
console.table(data); console.table(data);
setTimeout(() => navigate("/?logged"), 3000) setTimeout(() => navigate("/?logged"), 3000);
} }
return ( return (
<main className="cardContainer"> <main className="cardContainer">
<form onSubmit={handleSubmit(onSubmit)}> <form onSubmit={handleSubmit(onSubmit)}>
<Card className="card"> <Card className="card">
<h3>Login</h3> <h3>Login</h3>
<TextField {...emailProps} /> <TextField {...emailProps} />
<TextField {...passProps}/> <TextField {...passProps} />
<Button type="submit" id="submit" variant="contained">Login</Button> <Button type="submit" id="submit" variant="contained">
<p>Forgot your <Link to="/forgot">password</Link>?</p> Login
</Button>
<p>
Forgot your <Link to="/forgot">password</Link>?
</p>
</Card> </Card>
</form> </form>
</main> </main>
) );
} }

View File

@ -1,33 +1,42 @@
import React, { ChangeEvent, MouseEvent } from "react"; import React, { ChangeEvent, MouseEvent } from "react";
import { Card, Button, TextField } from "@mui/material" import { Card, Button, TextField } from "@mui/material";
import "@scss/reset.scss" import "@scss/reset.scss";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import { yupResolver } from '@hookform/resolvers/yup'; import { yupResolver } from "@hookform/resolvers/yup";
import * as yup from "yup"; import * as yup from "yup";
import { useForm } from "react-hook-form" import { useForm } from "react-hook-form";
type Inputs = { type Inputs = {
password: string; password: string;
confirm_password: string; confirm_password: string;
}; };
const schema = yup.object().shape({ const schema = yup
password: yup.string().required("No password was provided"), .object()
confirm_password: yup.string().oneOf([yup.ref('password'), null], 'Passwords must match') .shape({
}).required(); password: yup.string().required("No password was provided"),
confirm_password: yup
.string()
.oneOf([yup.ref("password"), null], "Passwords must match"),
})
.required();
export default function Reset() { export default function Reset() {
const navigate = useNavigate(); const navigate = useNavigate();
const { register, formState: { errors }, handleSubmit } = useForm<Inputs>({ const {
resolver: yupResolver(schema), // yup, joi and even your own. register,
formState: { errors },
handleSubmit,
} = useForm<Inputs>({
resolver: yupResolver(schema),
}); });
function hash(s: string) { function hash(s: string) {
return `hash of '${s}' to be definied`; return `hash of '${s}' to be definied`;
} }
const {ref: passRef, ...passRegisterProps} = register("password"); const { ref: passRef, ...passRegisterProps } = register("password");
const passProps = { const passProps = {
...passRegisterProps, ...passRegisterProps,
@ -38,9 +47,10 @@ export default function Reset() {
label: "Password", label: "Password",
error: !!errors.password, error: !!errors.password,
helperText: errors?.password?.message, helperText: errors?.password?.message,
} };
const {ref: confirmPassRef, ...confirmPassRegisterProps} = register("confirm_password"); const { ref: confirmPassRef, ...confirmPassRegisterProps } =
register("confirm_password");
const confirmPassProps = { const confirmPassProps = {
...confirmPassRegisterProps, ...confirmPassRegisterProps,
@ -51,13 +61,15 @@ export default function Reset() {
label: "Confirm Password", label: "Confirm Password",
error: !!errors.confirm_password, error: !!errors.confirm_password,
helperText: errors?.confirm_password?.message, helperText: errors?.confirm_password?.message,
} };
function onSubmit(data: Inputs) { function onSubmit(data: Inputs) {
console.log("There will be an API call now with the following data"); console.log("There will be an API call now with the following data");
data = {password: hash(data.password), confirm_password: hash(data.confirm_password)} data = {
password: hash(data.password),
confirm_password: hash(data.confirm_password),
};
console.table(data); console.table(data);
// setTimeout(() => navigate("/?logged"), 3000)
} }
return ( return (
@ -65,12 +77,13 @@ export default function Reset() {
<form onSubmit={handleSubmit(onSubmit)}> <form onSubmit={handleSubmit(onSubmit)}>
<Card className="card"> <Card className="card">
<h3>Reset Password</h3> <h3>Reset Password</h3>
<TextField {...passProps}/> <TextField {...passProps} />
<TextField {...confirmPassProps}/> <TextField {...confirmPassProps} />
<Button type="submit" id="submit" variant="contained">Reset Password</Button> <Button type="submit" id="submit" variant="contained">
Reset Password
</Button>
</Card> </Card>
</form> </form>
</main> </main>
);
)
} }