From e26e49364a2df4c1f92906219bce248ca096994c Mon Sep 17 00:00:00 2001 From: minhd-vu Date: Sun, 25 Feb 2024 18:30:38 -0500 Subject: [PATCH] update ui and add error alerts --- app/page.tsx | 4 +-- components/App.tsx | 23 ++++++++++++++++ components/LeaveParty.tsx | 11 ++++++++ components/Party.tsx | 57 +++++++++++++++++---------------------- components/PartyCard.tsx | 16 +++++++++++ components/StartGame.tsx | 12 +++++++-- 6 files changed, 86 insertions(+), 37 deletions(-) create mode 100644 components/App.tsx create mode 100644 components/PartyCard.tsx diff --git a/app/page.tsx b/app/page.tsx index 8772041..2eea9c3 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,5 +1,5 @@ +import App from "@/components/App"; import Hero from "@/components/Hero"; -import Party from "@/components/Party"; import { getServerSession } from "next-auth"; export default async function Home() { @@ -7,7 +7,7 @@ export default async function Home() { return (
-
{session ? : }
+
{session ? : }
); } diff --git a/components/App.tsx b/components/App.tsx new file mode 100644 index 0000000..818b198 --- /dev/null +++ b/components/App.tsx @@ -0,0 +1,23 @@ +"use client"; + +import { Dispatch, SetStateAction, createContext, useState } from "react"; +import Party from "./Party"; +import Alert from "./Alert"; + +type ErrorContextProps = { + error?: string; + setError: Dispatch>; +}; + +export const ErrorContext = createContext(undefined!); + +export default function App() { + const [error, setError] = useState(); + + return ( + + {error && {error}} + + + ); +} diff --git a/components/LeaveParty.tsx b/components/LeaveParty.tsx index fe8a076..db88b22 100644 --- a/components/LeaveParty.tsx +++ b/components/LeaveParty.tsx @@ -1,7 +1,15 @@ "use client"; +import { useState } from "react"; +import { useSWRConfig } from "swr"; + export default function LeaveParty() { + const [isLoading, setLoading] = useState(false); + const { mutate } = useSWRConfig(); + async function leaveParty() { + setLoading(true); + const res = await fetch("/api/party/leave", { method: "POST", }); @@ -9,11 +17,14 @@ export default function LeaveParty() { if (!res.ok) { throw new Error(await res.json()); } + + mutate("/api/user"); } return ( ); } diff --git a/components/Party.tsx b/components/Party.tsx index d832457..d9fc25e 100644 --- a/components/Party.tsx +++ b/components/Party.tsx @@ -15,6 +15,7 @@ import Alert from "./Alert"; import PromotePlayer from "./PromotePlayer"; import RemovePlayer from "./RemovePlayer"; import KillTarget from "./KillTarget"; +import PartyCard from "./PartyCard"; export default function Party() { const fetcher: Fetcher = (url) => @@ -34,16 +35,16 @@ export default function Party() { const user = data; if (!user) { - return; + return User not found. Try logging in.; } const party = user.party; if (!party) { return ( -
+ <> -
+ ); } @@ -53,19 +54,14 @@ export default function Party() { } return ( -
-
-

- Code: {party.code.toUpperCase()} -

-

Target: {user.target.name}

-
- - {party.adminId === user.id && } - -
+ +

Target: {user.target.name}

+
+ + {party.adminId === user.id && } +
-
+ ); } @@ -95,24 +91,19 @@ export default function Party() { }); return ( -
-
-

- Code: {party.code.toUpperCase()} -

-

Mode: {_.startCase(_.toLower(party.mode))}

- {!isAdmin && ( -

- Waiting for party leader to start the party... -

- )} -

Players:

-
    {players}
-
- {isAdmin && } - -
+ +

Mode: {_.startCase(_.toLower(party.mode))}

+ {!isAdmin && ( +

+ Waiting for party leader to start the party... +

+ )} +

Players:

+
    {players}
+
+ {isAdmin && } +
-
+ ); } diff --git a/components/PartyCard.tsx b/components/PartyCard.tsx new file mode 100644 index 0000000..4b0e660 --- /dev/null +++ b/components/PartyCard.tsx @@ -0,0 +1,16 @@ +export default function PartyCard({ + children, + code, +}: { + children: React.ReactNode; + code: string; +}) { + return ( +
+
+

Code: {code.toUpperCase()}

+ {children} +
+
+ ); +} diff --git a/components/StartGame.tsx b/components/StartGame.tsx index 9aac903..9b5b2d6 100644 --- a/components/StartGame.tsx +++ b/components/StartGame.tsx @@ -1,16 +1,24 @@ "use client"; +import { useContext } from "react"; +import { ErrorContext } from "./App"; +import { useSWRConfig } from "swr"; + export default function StartGame() { + const { setError } = useContext(ErrorContext); + const { mutate } = useSWRConfig(); + async function startGame() { const res = await fetch("/api/party/start", { method: "POST", }); if (!res.ok) { - throw new Error(await res.json()); + setError(await res.json()); + return; } - console.log(await res.json()); + mutate("/api/user"); } return (