Made a central error UI

This commit is contained in:
Meliora Ho 2024-03-18 22:23:59 +00:00
parent f47bc76514
commit 1bc2f2e5c7
6 changed files with 74 additions and 72 deletions

View File

@ -1 +0,0 @@
{}

View File

@ -1,71 +1,65 @@
"use client" // pages/forgot-password.tsx
"use client";
import React, { useState, useEffect } from 'react';
import Input from '@/components/Input';
import Button from '@/components/Button'; import Button from '@/components/Button';
import Input from '@/components/Input'
import InlineLink from '@/components/InlineLink'; import InlineLink from '@/components/InlineLink';
import Paper from '@/components/auth/Paper'; import Paper from '@/components/auth/Paper';
import { useState, useEffect } from 'react'; import ErrorBanner from '@/components/auth/ErrorBanner';
function isValidEmail(email: string) { function isValidEmail(email: string): string | null {
//check email validation
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (email.trim() === '') { if (email.trim() === '') {
//check if input is empty
return 'Email cannot be empty'; return 'Email cannot be empty';
} else if (!emailRegex.test(email)) { } else if (!emailRegex.test(email)) {
//check for incorrect email format
return 'Invalid email format'; return 'Invalid email format';
} }
else{ return null; // No error
//return message for no error in input
return 'If your email exists in the database, you should receive a password reset in your inbox.'
}
} }
export default function Page() { export default function ForgotPasswordPage() {
const [confirmEmail, setConfirmEmail] = useState("");
const [emailError, setEmailError] = useState<string | null>(null);
const [isButtonDisabled, setIsButtonDisabled] = useState(true); const [isButtonDisabled, setIsButtonDisabled] = useState(true);
const [confirmEmail, setconfirmEmail] = useState('');
const [emailError, setEmailError] = useState('');
useEffect(() => { useEffect(() => {
//when email string inputted it is checked in isValidEmail
const error = isValidEmail(confirmEmail); const error = isValidEmail(confirmEmail);
setEmailError(error); setEmailError(error); // set the error message based on validation
//button is disabled if any error is detected setIsButtonDisabled(!!error); // disable button if there is an error
setIsButtonDisabled(error !== null && !error.includes('exists in the database'));
}, [confirmEmail]); }, [confirmEmail]);
return ( return (
<> <>
<Paper> <Paper>
<form <form
className="mb-0 m-auto mt-6 space-y-4 rounded-lg p-4 shadow-lg sm:p-6 lg:p-8 bg-white max-w-xl"> onSubmit={(e) => {
<h1 className="font-bold text-xl text-purple-800">Forgot password</h1> e.preventDefault();
<div className="mb-4"> // submit form logic here. since it's a "forgot password" form,
<Input type='email' // typically you would send a reset password link to the email provided.
title="Enter your email address" console.log('Form submitted with email:', confirmEmail);
placeholder="janedoe@gmail.com" }}
//setting a placeholder in the email input box className="mb-0 m-auto mt-6 space-y-4 rounded-lg p-4 shadow-lg sm:p-6 lg:p-8 bg-white max-w-xl"
value={confirmEmail} >
onChange={(e) => { <h1 className="font-bold text-xl text-purple-800">Forgot Password</h1>
setconfirmEmail(e.target.value); <div className="mb-6">
}}/> <Input
{emailError && ( type='email'
<p className={`mt-2 ${emailError.includes('exists in the database') ? 'text-green-500' : 'text-red-500'}`}> valid={emailError !== null}
{emailError} title="Enter your email address"
</p> placeholder="janedoe@gmail.com"
//if email does not meet certain criteria, then issue an error value={confirmEmail}
)} onChange={(e) => setConfirmEmail(e.target.value)}
/>
</div> </div>
{emailError && <ErrorBanner heading={emailError} />}
<div className="flex flex-col items-left space-y-4"> <div className="flex flex-col items-left space-y-4">
<InlineLink href= "/auth/login"> {/* link back to login page */} <InlineLink href="/auth/login">
Back to Sign In Back to Sign In
</InlineLink> </InlineLink>
<Button type="submit" disabled={isButtonDisabled}> <Button type="submit" disabled={isButtonDisabled}>
Send Send
</Button> </Button>
</div> </div>
</form> </form>
<p className="text-center mt-6 text-gray-500 text-xs"> <p className="text-center mt-6 text-gray-500 text-xs">
@ -74,5 +68,4 @@ export default function Page() {
</Paper> </Paper>
</> </>
); );
}; }

View File

@ -6,13 +6,15 @@ import Input from '@/components/Input'
import InlineLink from '@/components/InlineLink'; import InlineLink from '@/components/InlineLink';
import Paper from '@/components/auth/Paper'; import Paper from '@/components/auth/Paper';
import Image from 'next/image'; import Image from 'next/image';
import { ChangeEvent, useState } from "react"; import { useState } from "react";
import PasswordInput from '@/components/auth/PasswordInput'; import PasswordInput from '@/components/auth/PasswordInput';
import ErrorBanner from '@/components/auth/ErrorBanner';
export default function Page() { export default function Page() {
const [email, setEmail] = useState(""); const [email, setEmail] = useState("");
const [password, setPassword] = useState(""); const [password, setPassword] = useState("");
const [error, setError] = useState(""); const [emailError, setEmailError] = useState("");
const [passwordError, setPasswordError] = useState("");
const handleEmailChange = (event: React.ChangeEvent<HTMLInputElement>) => { const handleEmailChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setEmail(event.currentTarget.value); setEmail(event.currentTarget.value);
@ -26,17 +28,17 @@ export default function Page() {
// Priority: Incorrect combo > Missing email > Missing password // Priority: Incorrect combo > Missing email > Missing password
if (password.trim().length === 0) { if (password.trim().length === 0) {
setError("Please enter your password.") setEmailError("Please enter your password.")
event.preventDefault(); event.preventDefault();
} }
// This shouldn't happen, <input type="email"> already provides validation, but just in case. // This shouldn't happen, <input type="email"> already provides validation, but just in case.
if (email.trim().length === 0) { if (email.trim().length === 0) {
setError("Please enter your email.") setPasswordError("Please enter your email.")
event.preventDefault(); event.preventDefault();
} }
// Placeholder for incorrect email + password combo. // Placeholder for incorrect email + password combo.
if (email === "incorrect@gmail.com" && password) { if (email === "incorrect@gmail.com" && password) {
setError("Incorrect password.") setPasswordError("Incorrect password.")
event.preventDefault(); event.preventDefault();
} }
} }
@ -44,7 +46,9 @@ export default function Page() {
return ( return (
<> <>
<Paper> <Paper>
<form className="mb-0 m-auto mt-6 space-y-4 rounded-lg p-4 shadow-lg sm:p-6 lg:p-8 bg-white max-w-xl"> <form onSubmit={(e) => {
e.preventDefault();
}} className="mb-0 m-auto mt-6 space-y-4 rounded-lg p-4 shadow-lg sm:p-6 lg:p-8 bg-white max-w-xl">
<Image <Image
src="/logo.png" src="/logo.png"
alt='Compass Center logo.' alt='Compass Center logo.'
@ -54,13 +58,17 @@ export default function Page() {
<h1 className='font-bold text-xl text-purple-800'>Login</h1> <h1 className='font-bold text-xl text-purple-800'>Login</h1>
<div className="mb-4"> <div className="mb-6">
<Input type='email' title="Email" placeholder="janedoe@gmail.com" onChange={handleEmailChange} required /> <Input type='email' valid={emailError !== null} title="Email" placeholder="janedoe@gmail.com" onChange={handleEmailChange} required />
</div> </div>
{emailError && <ErrorBanner heading={emailError} />}
<div className="mb-6"> <div className="mb-6">
<PasswordInput title="Password" onChange={handlePasswordChange} required /> <PasswordInput title="Password" valid={passwordError !== null} onChange={handlePasswordChange} required />
</div> </div>
{passwordError && <ErrorBanner heading={passwordError} />}
<div className="flex flex-col items-left space-y-4"> <div className="flex flex-col items-left space-y-4">
<InlineLink href="/auth/forgot_password"> <InlineLink href="/auth/forgot_password">
@ -69,9 +77,6 @@ export default function Page() {
<Button onClick={handleClick}> <Button onClick={handleClick}>
Login Login
</Button> </Button>
<div className="text-center text-red-600" hidden={!error}>
<p>{error}</p>
</div>
</div> </div>
</form> </form>

View File

@ -1,13 +1,11 @@
// pages/index.tsx // pages/index.tsx
"use client"; "use client";
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import Input from '@/components/Input';
import Button from '@/components/Button'; import Button from '@/components/Button';
import Paper from '@/components/auth/Paper'; import Paper from '@/components/auth/Paper';
import PasswordInput from '@/components/auth/PasswordInput'; import PasswordInput from '@/components/auth/PasswordInput';
import ErrorBanner from '@/components/auth/ErrorBanner';
function isStrongPassword(password: string): boolean { function isStrongPassword(password: string): boolean {
@ -45,7 +43,6 @@ export default function Page() {
</div> </div>
<div className="mb-4"> <div className="mb-4">
<PasswordInput <PasswordInput
type="password"
title="Enter New Password" title="Enter New Password"
value={newPassword} value={newPassword}
valid={!isButtonDisabled || isStrongPassword(newPassword)} valid={!isButtonDisabled || isStrongPassword(newPassword)}
@ -54,15 +51,9 @@ export default function Page() {
}} }}
/> />
</div> </div>
{isStrongPassword(newPassword) || newPassword === '' ? null : <div role="alert" className="rounded border-s-4 border-red-500 bg-red-50 p-4"> {isStrongPassword(newPassword) || newPassword === '' ? null : <ErrorBanner heading="Password is not strong enough." description="Tip: Use a mix of letters, numbers, and symbols for a strong password. Aim for at least 8 characters!" />}
<strong className="block text-sm font-semibold text-red-800"> Password is not strong enough. </strong>
<p className="mt-2 text-xs font-thin text-red-700">
Tip: Use a mix of letters, numbers, and symbols for a strong password. Aim for at least 8 characters!
</p>
</div>}
<div className="mb-6"> <div className="mb-6">
<PasswordInput <PasswordInput
type="password"
title="Confirm Password" title="Confirm Password"
value={confirmPassword} value={confirmPassword}
valid={!isButtonDisabled || (newPassword === confirmPassword && confirmPassword !== '')} valid={!isButtonDisabled || (newPassword === confirmPassword && confirmPassword !== '')}
@ -71,12 +62,7 @@ export default function Page() {
}} }}
/> />
</div> </div>
{newPassword === confirmPassword || confirmPassword === '' ? null : <div role="alert" className="rounded border-s-4 border-red-500 bg-red-50 p-4"> {newPassword === confirmPassword || confirmPassword === '' ? null : <ErrorBanner heading="Passwords do not match." description="Please make sure both passwords are the exact same!"/>}
<strong className="block text-sm font-semibold text-red-800"> Passwords do not match. </strong>
<p className="mt-2 text-xs font-thin text-red-700">
Please make sure both passwords are the exact same!
</p>
</div>}
<div className="flex flex-col items-left space-y-4"> <div className="flex flex-col items-left space-y-4">
<Button type="submit" disabled={isButtonDisabled} > <Button type="submit" disabled={isButtonDisabled} >
Send Send

View File

@ -59,7 +59,7 @@ export default function Page() {
/> />
<h1 className='font-bold text-xl text-purple-800'>Login</h1> <h1 className='font-bold text-xl text-purple-800'>Login</h1>
<div className="mb-4"> <div className="mb-4">
<Input type='email' title="Email" placeholder="janedoe@gmail.com" icon={'EmailInputIcon'} onChange={handleEmailChange} /> <Input type='email' title="Email" placeholder="janedoe@gmail.com" onChange={handleEmailChange} />
</div> </div>
<div className="mb-6"> <div className="mb-6">
<Input type='password' title="Password" onChange={handlePasswordChange} /> <Input type='password' title="Password" onChange={handlePasswordChange} />

View File

@ -0,0 +1,19 @@
import React from 'react';
interface ErrorBannerProps {
heading: string;
description?: string | null;
}
const ErrorBanner: React.FC<ErrorBannerProps> = ({ heading, description = null }) => {
return (
<div role="alert" className="rounded border-s-4 border-red-500 bg-red-50 p-4">
<strong className="block text-sm font-semibold text-red-800">{heading}</strong>
{description && <p className="mt-2 text-xs font-thin text-red-700">
{description}
</p>}
</div>
);
};
export default ErrorBanner;