diff --git a/client/src/components/pages/Login.tsx b/client/src/components/pages/Login.tsx index 1a4418e..16969d3 100644 --- a/client/src/components/pages/Login.tsx +++ b/client/src/components/pages/Login.tsx @@ -1,20 +1,16 @@ import { useCallback, useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; import { useAuthContext } from "../../context/AuthContext"; -import { IUserAuth } from "../../schemas"; import { attemptLogin } from "../../util/apiUtils"; -import { Button, Page, Panel } from "../ui"; -import Form, { FormConfig } from "../ui/Form"; +import { IUserAuth } from "../../schemas"; +import { Button, Form, Page, Panel } from "../ui"; export default function Login() { // setup and local state const authContext = useAuthContext(); const navigate = useNavigate(); const [form, setForm] = useState(); - const [input, setInput] = useState({ - email: '', - password: '' - }) + const [input, setInput] = useState({ email: '', password: '' }); // retrieve and store state from form const getFormState = useCallback((received: IUserAuth) => { @@ -29,18 +25,19 @@ export default function Login() { navigate('/'); } - const formConfig: FormConfig = { - parent: 'login', - keys: Object.keys(input), - labels: ["Email", "Password"], - dataTypes: Object.keys(input), - initialState: input, - getState: getFormState - } - + // check for logged in user and mount form useEffect(() => { if (authContext.user) navigate('/'); - setForm(new Form(formConfig).mount()) + setForm( + new Form({ + parent: 'login', + keys: Object.keys(input), + labels: ["Email", "Password"], + dataTypes: Object.keys(input), + initialState: input, + getState: getFormState + }).mount() + ); }, []) return ( diff --git a/client/src/components/ui/Form.tsx b/client/src/components/ui/Form.tsx index f88b3e4..5593047 100644 --- a/client/src/components/ui/Form.tsx +++ b/client/src/components/ui/Form.tsx @@ -22,7 +22,6 @@ export default class Form{ public labels: string[]; public keys: string[]; public dataTypes: any[] - public length: number; public state: T; public getState: (received: T) => void @@ -30,7 +29,6 @@ export default class Form{ this.parent = config.parent; this.keys = config.keys; this.labels = config.labels || this.keys; - this.length = config.keys.length; this.dataTypes = config.dataTypes || new Array(this.keys.length).fill('text'); this.state = config.initialState; this.getState = config.getState; @@ -49,7 +47,7 @@ export default class Form{ mount() { let output = new Array(); - for (let i = 0; i < this.length; i++) { + for (let i = 0; i < this.keys.length; i++) { output.push(
diff --git a/client/src/components/ui/Navbar/index.tsx b/client/src/components/ui/Navbar/index.tsx index 29c073e..17b5ab6 100644 --- a/client/src/components/ui/Navbar/index.tsx +++ b/client/src/components/ui/Navbar/index.tsx @@ -1,70 +1,36 @@ -import { useEffect, useState } from "react"; +import { useCallback, useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; +import { LoggedIn, NotLoggedIn, Registering } from "./variants"; import { useAuthContext } from "../../../context/AuthContext"; -import { attemptLogout } from "../../../util/apiUtils"; import { IUser } from "../../../schemas"; -import Button from "../Button"; import "/src/sass/components/Navbar.scss"; const Navbar = () => { // setup and local state const navigate = useNavigate(); const authContext = useAuthContext(); - const [received, setReceived] = useState(); + const [received, setReceived] = useState(); const [displayed, setDisplayed] = useState(); - // helper to unwrap async result - const handleLogout = async () => { - const success = await attemptLogout(); - if (success) setReceived(undefined); + // lift and store state from navbar variants + const liftChange = useCallback((newValue: IUser | undefined) => { + authContext.user = newValue; + setReceived(newValue); + }, []) + + const variants = { + loggedin: , + notloggedin: , + registering: } - // jsx variations - const navbarLoggedIn = ( - - ) - - const navbarNotLoggedIn = ( - - ) - - const navbarRegistering = ( - - ) - // side effects for live rendering useEffect(() => { - console.log(authContext); authContext && setReceived(authContext.user); }, [authContext]) useEffect(() => { - console.log(received); - setDisplayed(received ? navbarLoggedIn : navbarNotLoggedIn); + setDisplayed(received ? variants.loggedin : variants.notloggedin); }, [received, setReceived]); return displayed ||

Loading...

; diff --git a/client/src/components/ui/Navbar/variants.tsx b/client/src/components/ui/Navbar/variants.tsx new file mode 100644 index 0000000..8105222 --- /dev/null +++ b/client/src/components/ui/Navbar/variants.tsx @@ -0,0 +1,53 @@ +import { attemptLogout } from "../../../util/apiUtils"; +import { NavbarType } from "../../../util/types"; +import Button from "../Button"; + +const LoggedIn: NavbarType = ({ received, liftChange, navigate }) => { + const handleLogout = async () => { + const success = await attemptLogout(); + if (success) liftChange!(undefined); + navigate('/'); + } + + return ( + + ) +} + +const NotLoggedIn: NavbarType = ({ navigate }) => { + return ( + + ) +} + +const Registering: NavbarType = ({ received, navigate }) => { + return ( + + ) +} + +export { LoggedIn, NotLoggedIn, Registering } \ No newline at end of file diff --git a/client/src/components/ui/index.ts b/client/src/components/ui/index.ts index 4c5ca96..5ed473b 100644 --- a/client/src/components/ui/index.ts +++ b/client/src/components/ui/index.ts @@ -1,9 +1,19 @@ import Button from "./Button"; +import Card from "./Card"; +import Divider from "./Divider"; +import Form from "./Form"; +import Navbar from "./Navbar"; import Page from "./Page"; import Panel from "./Panel"; +import UserCard from "./UserCard"; export { Button, + Card, + Divider, + Form, + Navbar, Page, - Panel + Panel, + UserCard } \ No newline at end of file diff --git a/client/src/util/types.ts b/client/src/util/types.ts index c81347b..51b688a 100644 --- a/client/src/util/types.ts +++ b/client/src/util/types.ts @@ -1,4 +1,5 @@ -import { FC, ReactNode } from "react"; +import { Dispatch, FC, ReactNode, SetStateAction } from "react"; +import { useNavigate } from "react-router-dom"; import { IUser } from "../schemas"; interface PortalBase { @@ -18,8 +19,15 @@ interface UserCardProps extends PortalBase { user: IUser } +interface NavbarProps { + received: IUser | undefined + navigate: (path: string) => void + liftChange?: (newValue: IUser | undefined) => void +} + export type PageComponent = FC export type PanelComponent = FC export type ButtonComponent = FC export type ProtectPortal = FC -export type UserCardType = FC \ No newline at end of file +export type UserCardType = FC +export type NavbarType = FC \ No newline at end of file