Skip to content

Commit bde9bf7

Browse files
anees-asgharnalves599
authored andcommitted
Add validate spin wheel button; Refactor code
1 parent 1006f74 commit bde9bf7

File tree

14 files changed

+211
-70
lines changed

14 files changed

+211
-70
lines changed

src/app/(authenticated)/home/page.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import authOptions from "@/app/api/auth/[...nextauth]/authOptions";
1414
import { UserService } from "@/services/UserService";
1515
import { isCompany, isToday } from "@/utils/utils";
1616
import UserSignOut from "@/components/UserSignOut";
17+
import { SPIN_WHEEL_MAXIMUM } from "@/constants";
1718

1819
const N_SESSION_TILES = 3;
1920
const N_COMPANY_TILES = 6;
2021
const N_SPEAKER_TILES = 6;
21-
const SPIN_WHEEL_MAXIMUM = 10;
2222

2323
export default async function Home() {
2424
const session = (await getServerSession(authOptions))!;
@@ -58,7 +58,10 @@ export default async function Home() {
5858
{/* Spin the Wheel Section */}
5959
{showSpinWheelSection && (
6060
<ProgressBar
61-
current={spinWheelData?.signatures.length ?? 0}
61+
current={Math.min(
62+
spinWheelData?.signatures.length ?? 0,
63+
SPIN_WHEEL_MAXIMUM
64+
)}
6265
maximum={SPIN_WHEEL_MAXIMUM}
6366
title="Companies Visited Today"
6467
className="pb-2"

src/app/(authenticated)/spin/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import { CompanyTile } from "@/components/company";
44
import GridList from "@/components/GridList";
55
import ProgressBar from "@/components/ProgressBar";
66
import UserSignOut from "@/components/UserSignOut";
7+
import { SPIN_WHEEL_MAXIMUM } from "@/constants";
78
import { CompanyService } from "@/services/CompanyService";
89
import { UserService } from "@/services/UserService";
910
import { isCompany, isToday } from "@/utils/utils";
1011
import { getServerSession } from "next-auth";
1112

12-
const SPIN_WHEEL_MAXIMUM = 10;
13-
1413
export default async function Spin() {
1514
const session = (await getServerSession(authOptions))!;
1615
const user = await UserService.getMe(session.cannonToken);
@@ -39,7 +38,8 @@ export default async function Spin() {
3938
spinWheelData?.signatures
4039
.sort((a, b) => a.date.localeCompare(b.date))
4140
.map((s) => companyMap.get(s.companyId))
42-
.filter((c) => c !== undefined) ?? [];
41+
.filter((c) => c !== undefined)
42+
.slice(0, SPIN_WHEEL_MAXIMUM) ?? [];
4343

4444
return (
4545
<div className="container mx-auto">

src/app/(authenticated)/users/[id]/ConnectButton.tsx

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/app/(authenticated)/users/[id]/DemoteButton.tsx

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use client";
2+
3+
import { UserPlus } from "lucide-react";
4+
import { ProfileButtonProps } from ".";
5+
6+
export default function ConnectButton({
7+
cannonToken,
8+
user,
9+
otherUser,
10+
}: ProfileButtonProps) {
11+
if (user.id === otherUser.id) return <></>;
12+
13+
return (
14+
<button className="button-primary text-sm">
15+
<UserPlus size={16} />
16+
Connect
17+
</button>
18+
);
19+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use client";
2+
3+
import { UserService } from "@/services/UserService";
4+
import { isCompany, isMember } from "@/utils/utils";
5+
import { ArrowBigDown } from "lucide-react";
6+
import { useRouter } from "next/navigation";
7+
import { ProfileButtonProps } from ".";
8+
9+
export default function DemoteButton({
10+
cannonToken,
11+
user,
12+
otherUser,
13+
}: ProfileButtonProps) {
14+
const router = useRouter();
15+
16+
if (
17+
user.id === otherUser.id ||
18+
!isMember(user.role) ||
19+
(!isCompany(otherUser.role) && !isMember(otherUser.role))
20+
)
21+
return <></>;
22+
23+
async function handleDemote() {
24+
if (await UserService.demote(cannonToken, otherUser.id)) router.refresh();
25+
else alert("Failed to demote!");
26+
}
27+
28+
return (
29+
<button className="button-secondary text-sm" onClick={handleDemote}>
30+
<ArrowBigDown size={20} />
31+
Demote
32+
</button>
33+
);
34+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"use client";
2+
3+
import { isCompany, isMember, isToday } from "@/utils/utils";
4+
import { FerrisWheel } from "lucide-react";
5+
import { ProfileButtonProps } from ".";
6+
import { UserService } from "@/services/UserService";
7+
import { useRouter } from "next/navigation";
8+
import { SPIN_WHEEL_MAXIMUM } from "@/constants";
9+
10+
export default function ValidateSpinButton({
11+
cannonToken,
12+
user,
13+
otherUser,
14+
}: ProfileButtonProps) {
15+
const router = useRouter();
16+
17+
if (
18+
user.id === otherUser.id ||
19+
!isMember(user.role) ||
20+
isCompany(otherUser.role)
21+
) {
22+
return <></>;
23+
}
24+
25+
const spinWheelData = otherUser.signatures?.find(
26+
(s) => s.edition === process.env.EVENT_EDITION && isToday(s.day)
27+
);
28+
29+
const isEligible =
30+
(spinWheelData &&
31+
!spinWheelData.redeemed &&
32+
spinWheelData.signatures.length >= SPIN_WHEEL_MAXIMUM) ??
33+
false;
34+
35+
async function validateSpinWheel() {
36+
const success = await UserService.validateSpinWheel(
37+
cannonToken,
38+
otherUser.id
39+
);
40+
if (success) router.refresh();
41+
else alert("Could not validate!");
42+
}
43+
44+
return (
45+
<button
46+
className="button-tertiary text-sm"
47+
disabled={!isEligible}
48+
onClick={validateSpinWheel}
49+
>
50+
<FerrisWheel size={16} />
51+
Validate Spin the Wheel
52+
</button>
53+
);
54+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import ConnectButton from "./ConnectButton";
2+
import DemoteButton from "./DemoteButton";
3+
import ValidateSpinButton from "./ValidateSpinButton";
4+
5+
export interface ProfileButtonProps {
6+
cannonToken: string;
7+
user: User;
8+
otherUser: User;
9+
}
10+
11+
export default function ProfileButtons(buttonProps: ProfileButtonProps) {
12+
return (
13+
<div className="px-4 pb-2 flex flex-col lg:flex-row gap-2 [&>button]:w-full [&>button]:lg:w-60 text-sm">
14+
<ConnectButton {...buttonProps} />
15+
<ValidateSpinButton {...buttonProps} />
16+
<DemoteButton {...buttonProps} />
17+
</div>
18+
);
19+
}

src/app/(authenticated)/users/[id]/page.tsx

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import ProfileInformations from "@/components/user/ProfileInformations";
88
import { AchievementService } from "@/services/AchievementService";
99
import { UserService } from "@/services/UserService";
1010
import { isCompany, isMember } from "@/utils/utils";
11-
import { UserPlus } from "lucide-react";
1211
import { getServerSession } from "next-auth";
13-
import DemoteButton from "./DemoteButton";
1412
import UserSignOut from "@/components/UserSignOut";
1513
import BlankPageWithMessage from "@/components/BlankPageMessage";
14+
import ProfileButtons from "./buttons";
1615

1716
interface UserProfileParams {
1817
id: string;
@@ -34,8 +33,6 @@ export default async function UserProfile({
3433
return <BlankPageWithMessage message="User not found!" />;
3534
}
3635

37-
const isMyself = userProfile.id === user.id;
38-
3936
const achievements = await AchievementService.getAchievements();
4037
const userAchievements = achievements?.filter((a) =>
4138
a.users?.includes(userProfile.id)
@@ -44,21 +41,12 @@ export default async function UserProfile({
4441
return (
4542
<div className="container mx-auto">
4643
<ProfileHeader user={userProfile} />
47-
{!isMyself && (
48-
<div className="px-4 py-2">
49-
<button className="button-primary text-sm w-full mt-2">
50-
<UserPlus size={16} />
51-
Connect
52-
</button>
53-
{isMember(user.role) &&
54-
(isMember(userProfile.role) || isCompany(userProfile.role)) && (
55-
<DemoteButton
56-
cannonToken={session.cannonToken}
57-
userId={userProfile.id}
58-
/>
59-
)}
60-
</div>
61-
)}
44+
45+
<ProfileButtons
46+
cannonToken={session.cannonToken}
47+
user={user}
48+
otherUser={userProfile}
49+
/>
6250

6351
{/* Notes */}
6452
{/* <List title="Notes">

src/constants/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const SPIN_WHEEL_MAXIMUM = 10;

0 commit comments

Comments
 (0)