From dfc99078eed0088d4a3a808f1e336f5c306f78ef Mon Sep 17 00:00:00 2001 From: aJesusCode Date: Thu, 12 Oct 2023 10:43:41 +0200 Subject: [PATCH] feat: modal for making user login --- src/app/(surfspots)/spots/[spot_id]/page.tsx | 13 ++++- src/app/api/favorite/route.ts | 5 -- src/app/components/AddToFavoriteBtn.tsx | 52 +++++++++++++++++--- 3 files changed, 57 insertions(+), 13 deletions(-) diff --git a/src/app/(surfspots)/spots/[spot_id]/page.tsx b/src/app/(surfspots)/spots/[spot_id]/page.tsx index 491b086..fa3723b 100644 --- a/src/app/(surfspots)/spots/[spot_id]/page.tsx +++ b/src/app/(surfspots)/spots/[spot_id]/page.tsx @@ -1,6 +1,9 @@ import { HourlySurfData } from "@/app/constants/types"; import NotFound from "@/app/not-found"; -import { createServerComponentClient } from "@supabase/auth-helpers-nextjs"; +import { + User, + createServerComponentClient, +} from "@supabase/auth-helpers-nextjs"; import { cookies } from "next/headers"; import type { Database } from "@/app/lib/database.types"; import Link from "next/link"; @@ -12,6 +15,12 @@ import { getCurrentPintxoConditions } from "@/app/utils/surfUtils"; import { DateTime } from "luxon"; import SpotConditionsWeek from "@/app/components/SpotConditionsWeek"; +async function getUser(): Promise { + const supabase = createServerComponentClient({ cookies }); + const { data: user } = await supabase.auth.getUser(); + return user ? user.user : null; +} + async function getSpotInfo(id: string) { const supabase = createServerComponentClient({ cookies }); @@ -42,6 +51,7 @@ export default async function SpotPage({ params: { spot_id: string }; }) { const id = params.spot_id; + const user = await getUser(); const spot = await getSpotInfo(id); const favorite = await isSpotFavorite(id); const isFavorite = !!favorite; @@ -73,6 +83,7 @@ export default async function SpotPage({
diff --git a/src/app/api/favorite/route.ts b/src/app/api/favorite/route.ts index dd013f1..3f17226 100644 --- a/src/app/api/favorite/route.ts +++ b/src/app/api/favorite/route.ts @@ -23,9 +23,6 @@ export async function POST(req: NextRequest, res: NextResponse) { .from("fav_spots") .delete() .eq("spot_id", favorite.spot_id); - console.log( - "DELETEDDDDDDDD //////////////////////////////////////////////////////////////" - ); return NextResponse.json({ error, status: "200" }); } else { const { data, error } = await supabase @@ -37,8 +34,6 @@ export async function POST(req: NextRequest, res: NextResponse) { }) .select() .single(); - console.log("Spot added", data); - console.log("EROOORRRR", error); return NextResponse.json({ data, error }); } } catch (error) { diff --git a/src/app/components/AddToFavoriteBtn.tsx b/src/app/components/AddToFavoriteBtn.tsx index 18093b8..841c231 100644 --- a/src/app/components/AddToFavoriteBtn.tsx +++ b/src/app/components/AddToFavoriteBtn.tsx @@ -1,19 +1,29 @@ "use client"; -import React from "react"; +import React, { useState } from "react"; import { useRouter } from "next/navigation"; import { AddFavoriteIcon, RemoveFavoriteIcon } from "./icons/icons"; +import { User } from "@supabase/supabase-js"; interface AddFavoriteBtnProps { spotId: string; isFavorite: boolean; + user: User | null; } export default function AddToFavoriteBtn({ spotId, isFavorite, + user, }: AddFavoriteBtnProps) { + const [showModal, setShowModal] = useState(false); const router = useRouter(); const handleAddFavoriteSpot = async () => { + console.log(user); + if (!user) { + console.log(user); + setShowModal(true); + return; + } const res = await fetch("/api/favorite", { method: "POST", headers: { @@ -22,7 +32,6 @@ export default function AddToFavoriteBtn({ body: JSON.stringify(spotId), }); const json = await res.json(); - console.log(res); if (json.error) { console.log(json.error); } @@ -31,12 +40,41 @@ export default function AddToFavoriteBtn({ } }; return ( -
- {!isFavorite ? ( - - ) : ( - +
+ {showModal && ( +
+
+ +

+ Login Required +

+

+ Dear friend! You need to be logged in to add + favorites. +

+
+ +
+
+
)} +
+ {!isFavorite ? ( + + ) : ( + + )} +
); }