Skip to content

Commit

Permalink
fix(AAiT.web.1): Fixed multiple toast in auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Son-OfAnton committed Aug 31, 2023
1 parent 1f5a839 commit 1819f5d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
24 changes: 14 additions & 10 deletions aait/web/group-1/components/signin/SigninForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { ChangeEvent, useState } from "react";
import { ChangeEvent, useEffect, useState } from "react";
import { useLoginMutation } from "@/store/auth/authApi";

import TextField from "../signup/TextField";
Expand All @@ -26,15 +26,10 @@ const SignInForm = () => {
const { name, value } = e.target;
setCredentials((prevData) => ({ ...prevData, [name]: value }));
};

const handleSignin = async (e) => {
e.preventDefault();
if (!credentials.email || !credentials.password) {
toast.error("Please fill all fields");
}
const response = await login(credentials);

useEffect(() => {
if (isSuccess) {
localStorage.setItem("user", JSON.stringify(response));
localStorage.setItem("user", JSON.stringify(data));
toast.success("Signed in successfully");
router.push("/");
}
Expand All @@ -46,6 +41,15 @@ const SignInForm = () => {
toast.error(errMsg || 'Unable to signin');
}
}
}, [isError, isSuccess, data, error])

const handleSignin = async (e) => {
e.preventDefault();
if (!credentials.email || !credentials.password) {
toast.error("Please fill all fields");
return
}
login(credentials);
};

return (
Expand All @@ -62,7 +66,7 @@ const SignInForm = () => {
id={field[1]}
placeholder={field[2]}
value={credentials[field[1]]}
onChange={handleInputChange}
onChange={(e) => handleInputChange(e)}
/>
))}
<button
Expand Down
22 changes: 17 additions & 5 deletions aait/web/group-1/components/signup/SignupForm.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"use client"

import { ChangeEvent, useState } from "react"
import { ChangeEvent, useState, useEffect } from "react"
import { useRegisterMutation } from "@/store/auth/authApi"

import TextField from "./TextField"
import { toast } from 'react-toastify'
import { useRouter } from "next/navigation"

const fieldInfo: Array<Array<string>> = [
["text", "name", "Full Name"],
Expand All @@ -14,25 +15,38 @@ const fieldInfo: Array<Array<string>> = [


const SignupForm = () => {
const router = useRouter()
const [register, { isLoading, isError, isSuccess, error }] = useRegisterMutation()
const [credentials, setCredentials] = useState({
name: "",
email: "",
password: "",
})

useEffect(() => {
if (isError) {
toast.error('Unable to signup')
}
if(isSuccess) {
toast.success('Signed up successfully')
router.push('/signin')
}
}, [isError, isSuccess])


const handleInputChange = (e: ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target
setCredentials((prevData) => ({...prevData, [name]: value}))
}

const handleSignup = () => {
if(!credentials.name || !credentials.email || !credentials.password) {
toast.error("Please fill all fields")
return
}
register(credentials)
}

return (
<div className="flex flex-col text-left gap-3.5 bg-white text-white border rounded-lg p-10 w-96">
<h2 className="text-form-gray-primary font-semibold text-3xl">Sign up</h2>
Expand All @@ -56,8 +70,6 @@ const SignupForm = () => {
>
{isLoading ? 'Signing up...' : 'Sign up'}
</button>
{isError && toast.error('Unable to signin')}
{isSuccess && toast.success('Signed up successfully')}
</div>
)
}
Expand Down

0 comments on commit 1819f5d

Please sign in to comment.