Skip to content

Commit

Permalink
update ui and add error alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
minhd-vu committed Feb 25, 2024
1 parent 329e98b commit e26e493
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 37 deletions.
4 changes: 2 additions & 2 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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() {
const session = await getServerSession();

return (
<div className="bg-base-200 flex flex-grow justify-center pt-20">
<div>{session ? <Party /> : <Hero />}</div>
<div>{session ? <App /> : <Hero />}</div>
</div>
);
}
23 changes: 23 additions & 0 deletions components/App.tsx
Original file line number Diff line number Diff line change
@@ -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<SetStateAction<string | undefined>>;
};

export const ErrorContext = createContext<ErrorContextProps>(undefined!);

export default function App() {
const [error, setError] = useState<string>();

return (
<ErrorContext.Provider value={{ error, setError }}>
{error && <Alert>{error}</Alert>}
<Party />
</ErrorContext.Provider>
);
}
11 changes: 11 additions & 0 deletions components/LeaveParty.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
"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",
});

if (!res.ok) {
throw new Error(await res.json());
}

mutate("/api/user");
}

return (
<button className="btn btn-error" onClick={() => leaveParty()}>
Leave Party
{isLoading && <span className="loading loading-dots loading-sm"></span>}
</button>
);
}
57 changes: 24 additions & 33 deletions components/Party.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<User, string> = (url) =>
Expand All @@ -34,16 +35,16 @@ export default function Party() {

const user = data;
if (!user) {
return;
return <Alert>User not found. Try logging in.</Alert>;
}

const party = user.party;
if (!party) {
return (
<div className="">
<>
<JoinParty />
<CreateParty />
</div>
</>
);
}

Expand All @@ -53,19 +54,14 @@ export default function Party() {
}

return (
<div className="card w-96 bg-base-100 shadow-xl">
<div className="card-body">
<h1 className="card-title text-3xl">
Code: {party.code.toUpperCase()}
</h1>
<h2>Target: {user.target.name}</h2>
<div className="card-actions justify-center">
<KillTarget />
{party.adminId === user.id && <StopGame />}
<LeaveParty />
</div>
<PartyCard code={party.code}>
<h2>Target: {user.target.name}</h2>
<div className="card-actions justify-center">
<KillTarget />
{party.adminId === user.id && <StopGame />}
<LeaveParty />
</div>
</div>
</PartyCard>
);
}

Expand Down Expand Up @@ -95,24 +91,19 @@ export default function Party() {
});

return (
<div className="card w-96 bg-base-100 shadow-xl">
<div className="card-body">
<h1 className="card-title text-3xl">
Code: {party.code.toUpperCase()}
</h1>
<h2>Mode: {_.startCase(_.toLower(party.mode))}</h2>
{!isAdmin && (
<p className="text-sm">
Waiting for party leader to start the party...
</p>
)}
<h2>Players:</h2>
<ul className="space-y-2">{players}</ul>
<div className="card-actions justify-center">
{isAdmin && <StartGame />}
<LeaveParty />
</div>
<PartyCard code={party.code}>
<h2>Mode: {_.startCase(_.toLower(party.mode))}</h2>
{!isAdmin && (
<p className="text-sm">
Waiting for party leader to start the party...
</p>
)}
<h2>Players:</h2>
<ul className="space-y-2">{players}</ul>
<div className="card-actions justify-center">
{isAdmin && <StartGame />}
<LeaveParty />
</div>
</div>
</PartyCard>
);
}
16 changes: 16 additions & 0 deletions components/PartyCard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default function PartyCard({
children,
code,
}: {
children: React.ReactNode;
code: string;
}) {
return (
<div className="card w-96 bg-base-100 shadow-xl">
<div className="card-body">
<h1 className="card-title text-3xl">Code: {code.toUpperCase()}</h1>
{children}
</div>
</div>
);
}
12 changes: 10 additions & 2 deletions components/StartGame.tsx
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down

0 comments on commit e26e493

Please sign in to comment.