mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
178 lines
4.9 KiB
JavaScript
178 lines
4.9 KiB
JavaScript
"use client";
|
|
import { useState, useEffect } from "react";
|
|
import { useSession } from "next-auth/react";
|
|
import { useRouter } from "next/navigation";
|
|
import Link from "next/link";
|
|
|
|
export default function TeamSelection() {
|
|
const { data: session, status } = useSession();
|
|
const [teamCode, setTeamCode] = useState("");
|
|
const [error, setError] = useState("");
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
const router = useRouter();
|
|
|
|
useEffect(() => {
|
|
// Clear any leftover team data
|
|
localStorage.removeItem("teamData");
|
|
sessionStorage.removeItem("teamData");
|
|
localStorage.removeItem("userLeftTeam");
|
|
|
|
if (status === "authenticated") {
|
|
fetch(`/api/user?_t=${Date.now()}`, {
|
|
headers: {
|
|
"Cache-Control": "no-cache, no-store, must-revalidate",
|
|
Pragma: "no-cache",
|
|
},
|
|
credentials: "include",
|
|
})
|
|
.then((res) => res.json())
|
|
.then((userData) => {
|
|
console.log("User data:", userData);
|
|
if (userData.role === "admin") {
|
|
router.push("/admin");
|
|
} else if (userData.teamId) {
|
|
// Use router.push for client-side navigation
|
|
router.push(`/${userData.teamId}`);
|
|
} else {
|
|
setIsLoading(false);
|
|
}
|
|
})
|
|
.catch((error) => {
|
|
console.error("Failed to fetch user data:", error);
|
|
setIsLoading(false);
|
|
});
|
|
} else if (status === "unauthenticated") {
|
|
router.push("/login");
|
|
} else {
|
|
setIsLoading(true);
|
|
}
|
|
}, [status, router]);
|
|
|
|
const handleJoinTeam = async (e) => {
|
|
e.preventDefault();
|
|
setIsLoading(true);
|
|
setError("");
|
|
|
|
try {
|
|
const response = await fetch("/api/join-team", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ teamCode }),
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (!response.ok) {
|
|
throw new Error(data.error || "Failed to join team");
|
|
}
|
|
|
|
// Add validation before constructing the URL
|
|
if (!data.teamId || isNaN(parseInt(data.teamId))) {
|
|
throw new Error("Invalid team ID received");
|
|
}
|
|
|
|
// Navigate to the team page using the correct URL format
|
|
window.location.href = `/${data.teamId}`;
|
|
} catch (err) {
|
|
setError(err.message);
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
if (isLoading) {
|
|
return <div className="loading">Loading...</div>;
|
|
}
|
|
|
|
return (
|
|
<div className="auth-page">
|
|
<div className="page-header">
|
|
<div className="back-button"></div>
|
|
<div className="page-title">Team Selection</div>
|
|
<div className="placeholder"></div>
|
|
</div>
|
|
|
|
<div className="auth-container">
|
|
<div className="auth-card">
|
|
<h1>Join a Team</h1>
|
|
<form onSubmit={handleJoinTeam}>
|
|
<div className="form-group">
|
|
<input
|
|
type="text"
|
|
placeholder="Team Code"
|
|
value={teamCode}
|
|
onChange={(e) => setTeamCode(e.target.value)}
|
|
required
|
|
className="auth-input"
|
|
/>
|
|
</div>
|
|
<button type="submit" className="auth-button" disabled={isLoading}>
|
|
{isLoading ? "Joining..." : "Join Team"}
|
|
</button>
|
|
</form>
|
|
{error && <p className="error-message">{error}</p>}
|
|
<div className="team-options">
|
|
<Link href="/create-team">
|
|
<button className="auth-button">Create a new team</button>
|
|
</Link>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Navbar removed from here */}
|
|
|
|
<style jsx>{`
|
|
.auth-page {
|
|
display: flex;
|
|
flex-direction: column;
|
|
justify-content: center;
|
|
align-items: center;
|
|
height: 100vh;
|
|
overflow: hidden;
|
|
}
|
|
.auth-container {
|
|
display: flex;
|
|
flex-direction: column;
|
|
justify-content: center;
|
|
align-items: center;
|
|
width: 100%;
|
|
max-width: 400px;
|
|
padding: 20px;
|
|
box-sizing: border-box;
|
|
}
|
|
.auth-card {
|
|
width: 100%;
|
|
padding: 20px;
|
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
|
border-radius: 8px;
|
|
background-color: #fff;
|
|
}
|
|
.form-group {
|
|
margin-bottom: 10px;
|
|
}
|
|
.auth-input {
|
|
width: 100%;
|
|
padding: 10px;
|
|
border: 1px solid #ccc;
|
|
border-radius: 4px;
|
|
}
|
|
.auth-button {
|
|
width: 100%;
|
|
padding: 10px;
|
|
margin-top: 10px;
|
|
background-color: #68543c; /* Updated brown color */
|
|
color: white;
|
|
border: none;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
text-align: center;
|
|
}
|
|
.error-message {
|
|
color: red;
|
|
margin-top: 10px;
|
|
}
|
|
/* Navbar styles removed from here */
|
|
`}</style>
|
|
</div>
|
|
);
|
|
}
|