|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState } from "react"; |
| 4 | +import { createClient } from "../../../lib/supabase/client"; |
| 5 | +import { useRouter } from "next/navigation"; |
| 6 | +import { toast } from "react-toastify"; |
| 7 | +import Link from "next/link"; |
| 8 | + |
| 9 | +export default function JoinButton({ |
| 10 | + code, |
| 11 | + leaderboardSlug, |
| 12 | + isLoggedIn, |
| 13 | + alreadyMember, |
| 14 | +}: { |
| 15 | + code: string; |
| 16 | + leaderboardSlug: string; |
| 17 | + isLoggedIn: boolean; |
| 18 | + alreadyMember: boolean; |
| 19 | +}) { |
| 20 | + const router = useRouter(); |
| 21 | + const [joining, setJoining] = useState(false); |
| 22 | + |
| 23 | + if (alreadyMember) { |
| 24 | + return ( |
| 25 | + <Link |
| 26 | + href={`/leaderboard/${leaderboardSlug}`} |
| 27 | + className="btn-primary inline-flex items-center justify-center gap-2 w-full py-4 text-sm font-bold rounded-xl shadow-lg shadow-indigo-500/20" |
| 28 | + > |
| 29 | + <svg className="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor"> |
| 30 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" /> |
| 31 | + </svg> |
| 32 | + Already a Member — View Leaderboard |
| 33 | + </Link> |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + if (!isLoggedIn) { |
| 38 | + return ( |
| 39 | + <div className="space-y-3"> |
| 40 | + <Link |
| 41 | + href={`/login?redirect=${encodeURIComponent(`/join?id=${code}`)}`} |
| 42 | + className="btn-primary inline-flex items-center justify-center gap-2 w-full py-4 text-sm font-bold rounded-xl shadow-lg shadow-indigo-500/20" |
| 43 | + > |
| 44 | + <svg className="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor"> |
| 45 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M11 16l-4-4m0 0l4-4m-4 4h14m-5 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h7a3 3 0 013 3v1" /> |
| 46 | + </svg> |
| 47 | + Log In to Join |
| 48 | + </Link> |
| 49 | + <p className="text-xs text-gray-500"> |
| 50 | + Don't have an account?{" "} |
| 51 | + <Link href={`/signup?redirect=${encodeURIComponent(`/join?id=${code}`)}`} className="text-indigo-400 hover:text-indigo-300 transition-colors"> |
| 52 | + Sign up free |
| 53 | + </Link> |
| 54 | + </p> |
| 55 | + </div> |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + const handleJoin = async () => { |
| 60 | + setJoining(true); |
| 61 | + const supabase = createClient(); |
| 62 | + |
| 63 | + const joinPromise = (async () => { |
| 64 | + const { data: userData } = await supabase.auth.getUser(); |
| 65 | + const user = userData.user; |
| 66 | + if (!user) throw new Error("Not authenticated"); |
| 67 | + |
| 68 | + const { data: board } = await supabase |
| 69 | + .from("leaderboards") |
| 70 | + .select("id") |
| 71 | + .eq("join_code", code) |
| 72 | + .single(); |
| 73 | + |
| 74 | + if (!board) throw new Error("Invalid invite code"); |
| 75 | + |
| 76 | + const { error } = await supabase.from("leaderboard_members").insert({ |
| 77 | + leaderboard_id: board.id, |
| 78 | + user_id: user.id, |
| 79 | + }); |
| 80 | + |
| 81 | + if (error) throw error; |
| 82 | + return board; |
| 83 | + })(); |
| 84 | + |
| 85 | + try { |
| 86 | + await toast.promise(joinPromise, { |
| 87 | + pending: "Joining leaderboard...", |
| 88 | + success: "You're in! Welcome to the leaderboard.", |
| 89 | + error: { |
| 90 | + render({ data }) { |
| 91 | + const err = data as Error; |
| 92 | + if (err?.code === "23505") { |
| 93 | + return "You are already a member of this leaderboard."; |
| 94 | + } |
| 95 | + return err?.message || "Failed to join. Please try again."; |
| 96 | + }, |
| 97 | + }, |
| 98 | + }); |
| 99 | + |
| 100 | + router.push(`/leaderboard/${leaderboardSlug}`); |
| 101 | + } finally { |
| 102 | + setJoining(false); |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + return ( |
| 107 | + <button |
| 108 | + onClick={handleJoin} |
| 109 | + disabled={joining} |
| 110 | + className="btn-primary inline-flex items-center justify-center gap-2 w-full py-4 text-sm font-bold rounded-xl shadow-lg shadow-indigo-500/20 hover:shadow-indigo-500/30 transition-all disabled:opacity-50 disabled:cursor-not-allowed" |
| 111 | + > |
| 112 | + {joining ? ( |
| 113 | + <> |
| 114 | + <svg className="w-5 h-5 animate-spin" fill="none" viewBox="0 0 24 24"> |
| 115 | + <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" /> |
| 116 | + <path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z" /> |
| 117 | + </svg> |
| 118 | + Joining... |
| 119 | + </> |
| 120 | + ) : ( |
| 121 | + <> |
| 122 | + <svg className="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor"> |
| 123 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M18 9v3m0 0v3m0-3h3m-3 0h-3m-2-5a4 4 0 11-8 0 4 4 0 018 0zM3 20a6 6 0 0112 0v1H3v-1z" /> |
| 124 | + </svg> |
| 125 | + Accept Invite & Join |
| 126 | + </> |
| 127 | + )} |
| 128 | + </button> |
| 129 | + ); |
| 130 | +} |
0 commit comments