Made each form use useForm
This commit is contained in:
parent
e1e1d58335
commit
9728321bf8
@ -1,15 +1,37 @@
|
|||||||
import React, { useState, ChangeEvent, MouseEvent } from "react";
|
import React, { MouseEvent } from "react";
|
||||||
import { Card, Button, TextField } from "@mui/material";
|
import { Card, Button, TextField } from "@mui/material";
|
||||||
import { useNavigate } from "react-router-dom";
|
|
||||||
import "@scss/forgot_password.scss";
|
import "@scss/forgot_password.scss";
|
||||||
|
import { 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")
|
||||||
|
}).required();
|
||||||
|
|
||||||
|
|
||||||
export default function ForgotPassword() {
|
export default function ForgotPassword() {
|
||||||
const [email, setEmail] = useState("");
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
const { register, formState: { errors }, handleSubmit } = useForm<Inputs>({
|
||||||
|
resolver: yupResolver(schema), // yup, joi and even your own.
|
||||||
|
});
|
||||||
|
|
||||||
function handleEmail(e: ChangeEvent<HTMLInputElement>) {
|
|
||||||
setEmail(e.target.value);
|
const {ref: emailRef, ...emailRegisterProps} = register("email");
|
||||||
console.log(email);
|
const emailProps = {
|
||||||
|
...emailRegisterProps,
|
||||||
|
inputRef: emailRef,
|
||||||
|
className: "textfield",
|
||||||
|
id: "email",
|
||||||
|
label: "Email",
|
||||||
|
error: !!errors.email,
|
||||||
|
helperText: errors?.email?.message,
|
||||||
}
|
}
|
||||||
|
|
||||||
function buttonCancel(e: MouseEvent<HTMLButtonElement>) {
|
function buttonCancel(e: MouseEvent<HTMLButtonElement>) {
|
||||||
@ -17,27 +39,22 @@ export default function ForgotPassword() {
|
|||||||
navigate("/login")
|
navigate("/login")
|
||||||
}
|
}
|
||||||
|
|
||||||
function buttonSubmit(e: MouseEvent<HTMLButtonElement>) {
|
function onSubmit(data: Inputs) {
|
||||||
e.preventDefault();
|
console.log("There will be an API call now with the following data");
|
||||||
console.log(`Sumbitting email "${email}" to which send a link.`);
|
console.table(data);
|
||||||
navigate("/reset")
|
console.log("You can now go to http://localhost:8080/reset to acces the reset form")
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className="cardContainer">
|
<main className="cardContainer">
|
||||||
|
<form onSubmit={handleSubmit(onSubmit)}>
|
||||||
<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 email
|
||||||
with a link to reset it.
|
with a link to reset it.
|
||||||
</p>
|
</p>
|
||||||
<TextField
|
<TextField {...emailProps} />
|
||||||
className="textfield"
|
|
||||||
id="email"
|
|
||||||
label="Email"
|
|
||||||
value={email}
|
|
||||||
onChange={handleEmail}
|
|
||||||
/>
|
|
||||||
<div className="buttons">
|
<div className="buttons">
|
||||||
<Button
|
<Button
|
||||||
id="cancel"
|
id="cancel"
|
||||||
@ -50,12 +67,12 @@ export default function ForgotPassword() {
|
|||||||
type="submit"
|
type="submit"
|
||||||
id="submit"
|
id="submit"
|
||||||
variant="contained"
|
variant="contained"
|
||||||
onClick={buttonSubmit}
|
|
||||||
>
|
>
|
||||||
Submit
|
Submit
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</Card>
|
</Card>
|
||||||
|
</form>
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -26,9 +26,7 @@ export default function Login() {
|
|||||||
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");
|
||||||
required: "error txt lol"
|
|
||||||
});
|
|
||||||
|
|
||||||
const emailProps = {
|
const emailProps = {
|
||||||
...emailRegisterProps,
|
...emailRegisterProps,
|
||||||
|
@ -1,46 +1,76 @@
|
|||||||
import React, { useState, 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 { useNavigate } from "react-router-dom";
|
|
||||||
import "@scss/reset.scss"
|
import "@scss/reset.scss"
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import { yupResolver } from '@hookform/resolvers/yup';
|
||||||
|
import * as yup from "yup";
|
||||||
|
import { useForm } from "react-hook-form"
|
||||||
|
|
||||||
|
type Inputs = {
|
||||||
|
password: string;
|
||||||
|
confirm_password: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const schema = yup.object().shape({
|
||||||
|
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 [password, setPassword] = useState("");
|
|
||||||
const [confirmPassword, setConfirmPassword] = useState("");
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
const { register, formState: { errors }, handleSubmit } = useForm<Inputs>({
|
||||||
|
resolver: yupResolver(schema), // yup, joi and even your own.
|
||||||
|
});
|
||||||
|
|
||||||
function hash(s: string) {
|
function hash(s: string) {
|
||||||
return s;
|
return `hash of '${s}' to be definied`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function handlePassword(e: ChangeEvent<HTMLInputElement>) {
|
const {ref: passRef, ...passRegisterProps} = register("password");
|
||||||
setPassword(e.target.value);
|
|
||||||
console.log(hash(password));
|
const passProps = {
|
||||||
|
...passRegisterProps,
|
||||||
|
inputRef: passRef,
|
||||||
|
className: "textField",
|
||||||
|
name: "password",
|
||||||
|
id: "password",
|
||||||
|
label: "Password",
|
||||||
|
error: !!errors.password,
|
||||||
|
helperText: errors?.password?.message,
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleConfirmPassword(e: ChangeEvent<HTMLInputElement>) {
|
const {ref: confirmPassRef, ...confirmPassRegisterProps} = register("confirm_password");
|
||||||
setConfirmPassword(e.target.value);
|
|
||||||
console.log(confirmPassword);
|
const confirmPassProps = {
|
||||||
|
...confirmPassRegisterProps,
|
||||||
|
inputRef: confirmPassRef,
|
||||||
|
className: "textField",
|
||||||
|
name: "confirm_password",
|
||||||
|
id: "confirm_password",
|
||||||
|
label: "Confirm Password",
|
||||||
|
error: !!errors.confirm_password,
|
||||||
|
helperText: errors?.confirm_password?.message,
|
||||||
}
|
}
|
||||||
|
|
||||||
function buttonSubmit(e: MouseEvent<HTMLButtonElement>) {
|
function onSubmit(data: Inputs) {
|
||||||
e.preventDefault();
|
console.log("There will be an API call now with the following data");
|
||||||
if (password == confirmPassword && password.length != 0){
|
data = {password: hash(data.password), confirm_password: hash(data.confirm_password)}
|
||||||
console.log(`Sumbitting password "${hash(password)}"`)
|
console.table(data);
|
||||||
navigate("/login")
|
// setTimeout(() => navigate("/?logged"), 3000)
|
||||||
}
|
|
||||||
else{
|
|
||||||
console.warn("Passwords are not matching or are empty");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className="cardContainer">
|
<main className="cardContainer">
|
||||||
|
<form onSubmit={handleSubmit(onSubmit)}>
|
||||||
<Card className="card">
|
<Card className="card">
|
||||||
<h3>Reset Password</h3>
|
<h3>Reset Password</h3>
|
||||||
<TextField className="textfield" id="password" label="Password" type="password" value={password} onChange={handlePassword} />
|
<TextField {...passProps}/>
|
||||||
<TextField className="textfield" id="configrmPassword" label="Confirm Password" type="password" value={confirmPassword} onChange={handleConfirmPassword} />
|
<TextField {...confirmPassProps}/>
|
||||||
<Button id="submit" variant="contained" onClick={buttonSubmit}>Reset Password</Button>
|
<Button type="submit" id="submit" variant="contained">Reset Password</Button>
|
||||||
</Card>
|
</Card>
|
||||||
|
</form>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user