From 12f928aa93b436ec990cabb36b54a06fdeb1648f Mon Sep 17 00:00:00 2001 From: Atridad Lahiji Date: Wed, 27 Sep 2023 12:46:15 -0600 Subject: [PATCH] Fixed room 404 issues --- app/(client)/room/[id]/VoteUI.tsx | 138 ++++++++++++------------ app/api/internal/room/[roomId]/route.ts | 27 +++-- 2 files changed, 87 insertions(+), 78 deletions(-) diff --git a/app/(client)/room/[id]/VoteUI.tsx b/app/(client)/room/[id]/VoteUI.tsx index ade2464..479e147 100644 --- a/app/(client)/room/[id]/VoteUI.tsx +++ b/app/(client)/room/[id]/VoteUI.tsx @@ -1,10 +1,18 @@ "use client"; +import { EventTypes } from "@/_utils/types"; import Image from "next/image"; import { useEffect, useState } from "react"; -import { EventTypes } from "@/_utils/types"; +import LoadingIndicator from "@/_components/LoadingIndicator"; +import type { PresenceItem, RoomResponse, VoteResponse } from "@/_utils/types"; +import { useUser } from "@clerk/nextjs"; +import { useChannel, usePresence } from "ably/react"; +import { isAdmin, isVIP, jsonToCsv } from "app/_utils/helpers"; +import { env } from "env.mjs"; import { useParams } from "next/navigation"; +import { FaShieldAlt } from "react-icons/fa"; +import { GiStarFormation } from "react-icons/gi"; import { IoCheckmarkCircleOutline, IoCopyOutline, @@ -15,15 +23,7 @@ import { IoReloadOutline, IoSaveOutline, } from "react-icons/io5"; -import { GiStarFormation } from "react-icons/gi"; -import { FaShieldAlt } from "react-icons/fa"; import { RiVipCrownFill } from "react-icons/ri"; -import { env } from "env.mjs"; -import { isAdmin, isVIP, jsonToCsv } from "app/_utils/helpers"; -import type { PresenceItem, RoomResponse, VoteResponse } from "@/_utils/types"; -import LoadingIndicator from "@/_components/LoadingIndicator"; -import { useUser } from "@clerk/nextjs"; -import { useChannel, usePresence } from "ably/react"; import NoRoomUI from "./NoRoomUI"; const VoteUI = () => { @@ -40,12 +40,15 @@ const VoteUI = () => { const [votesFromDb, setVotesFromDb] = useState(undefined); const getRoomHandler = async () => { - const dbRoomResponse = await fetch(`/api/internal/room/${roomId}`, { + fetch(`/api/internal/room/${roomId}`, { cache: "no-cache", method: "GET", + }).then(async (response) => { + const dbRoom = (await response.json()) as RoomResponse; + setRoomFromDb(dbRoom); + }).catch(() => { + setRoomFromDb(null); }); - const dbRoom = (await dbRoomResponse.json()) as RoomResponse; - setRoomFromDb(dbRoom); }; const getVotesHandler = async () => { @@ -196,7 +199,7 @@ const VoteUI = () => { if (visible) { if (!!matchedVote) { - return
{matchedVote.value}
; + return
{ matchedVote.value }
; } else { return ; } @@ -216,30 +219,30 @@ const VoteUI = () => { } else if (roomFromDb) { return (
-
{roomFromDb.roomName}
+
{ roomFromDb.roomName }
ID:
-
{roomFromDb.id}
+
{ roomFromDb.id }
- {roomFromDb && ( + { roomFromDb && (
-

Story: {roomFromDb.storyName}

+

Story: { roomFromDb.storyName }

    - {presenceData && + { presenceData && presenceData .filter( (value, index, self) => @@ -252,81 +255,80 @@ const VoteUI = () => { .map((presenceItem) => { return (
  • {`${presenceItem.data.name}'s

    - {presenceItem.data.name}{" "} - {presenceItem.data.isAdmin && ( + { presenceItem.data.name }{ " " } + { presenceItem.data.isAdmin && ( - )}{" "} - {presenceItem.data.isVIP && ( + ) }{ " " } + { presenceItem.data.isVIP && ( - )}{" "} - {presenceItem.clientId === roomFromDb.userId && ( + ) }{ " " } + { presenceItem.clientId === roomFromDb.userId && ( - )} - {" : "} + ) } + { " : " }

    - {roomFromDb && + { roomFromDb && votesFromDb && voteString( roomFromDb.visible, votesFromDb, presenceItem.data - )} + ) }
  • ); - })} + }) }
- {roomFromDb.scale?.split(",").map((scaleItem, index) => { + { roomFromDb.scale?.split(",").map((scaleItem, index) => { return ( ); - })} + }) }
- )} + ) } - {!!roomFromDb && + { !!roomFromDb && (roomFromDb.userId === user?.id || isAdmin(user?.publicMetadata)) && ( <>
@@ -334,40 +336,40 @@ const VoteUI = () => {

Room Settings

{ + value={ roomScale } + onChange={ (event) => { setRoomScale(event.target.value); - }} + } } /> - + { + value={ storyNameText } + onChange={ (event) => { setStoryNameText(event.target.value); - }} + } } />
- {votesFromDb && + { votesFromDb && (roomFromDb.logs.length > 0 || votesFromDb.length > 0) && (
- )} + ) }
- )} + ) } ); // Room does not exist diff --git a/app/api/internal/room/[roomId]/route.ts b/app/api/internal/room/[roomId]/route.ts index 2bb360a..510a42d 100644 --- a/app/api/internal/room/[roomId]/route.ts +++ b/app/api/internal/room/[roomId]/route.ts @@ -1,13 +1,13 @@ -import { type NextRequest, NextResponse } from "next/server"; +import { NextResponse, type NextRequest } from "next/server"; -import { db } from "@/_lib/db"; -import { logs, rooms, votes } from "@/_lib/schema"; -import { eq } from "drizzle-orm"; import { publishToChannel } from "@/_lib/ably"; -import { EventTypes } from "@/_utils/types"; +import { db } from "@/_lib/db"; import { invalidateCache } from "@/_lib/redis"; -import { createId } from "@paralleldrive/cuid2"; +import { logs, rooms, votes } from "@/_lib/schema"; +import { EventTypes } from "@/_utils/types"; import { getAuth } from "@clerk/nextjs/server"; +import { createId } from "@paralleldrive/cuid2"; +import { eq } from "drizzle-orm"; export const runtime = "edge"; export const preferredRegion = ["pdx1"]; @@ -30,10 +30,17 @@ export async function GET( }, }); - return NextResponse.json(roomFromDb, { - status: 200, - statusText: "SUCCESS", - }); + if (roomFromDb) { + return NextResponse.json(roomFromDb, { + status: 200, + statusText: "SUCCESS", + }); + } else { + return new NextResponse("ROOM NOT FOUND", { + status: 404, + statusText: "ROOM NOT FOUND", + }); + } } export async function DELETE(