diff --git a/app/(main)/team-selection/page.js b/app/(main)/team-selection/page.js index b9aff34..d0e177b 100644 --- a/app/(main)/team-selection/page.js +++ b/app/(main)/team-selection/page.js @@ -2,6 +2,7 @@ import { useState, useEffect } from 'react'; import { useSession } from 'next-auth/react'; import { useRouter } from 'next/navigation'; +import Link from 'next/link'; import { joinTeam } from '../../actions/auth'; export default function TeamSelection() { @@ -56,6 +57,12 @@ export default function TeamSelection() { return (
+
+
←
+
Team Selection
+
+
+

Team Selection

@@ -79,7 +86,22 @@ export default function TeamSelection() {
{error &&

{error}

}
- Create a new team + Create a new team +
+
+ +
+
+
🏠
+
+
+
📊
+
+
+
🏆
+
+
+
👤
diff --git a/app/(main)/team/[teamId]/page.js b/app/(main)/team/[teamId]/page.js new file mode 100644 index 0000000..47defdf --- /dev/null +++ b/app/(main)/team/[teamId]/page.js @@ -0,0 +1,122 @@ +'use client'; // Mark as a Client Component +import { useState, useEffect } from 'react'; +import { useSession } from 'next-auth/react'; +import { useRouter } from 'next/navigation'; +import Link from 'next/link'; + +export default function TeamPage({ params }) { + const { data: session, status } = useSession(); + const [team, setTeam] = useState(null); + const [teamMembers, setTeamMembers] = useState([]); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(''); + const router = useRouter(); + const { teamId } = params; + + // Fetch team data + useEffect(() => { + async function fetchTeamData() { + try { + // Fetch team details + const teamResponse = await fetch(`/api/teams/${teamId}`); + if (!teamResponse.ok) { + throw new Error('Failed to fetch team data'); + } + const teamData = await teamResponse.json(); + setTeam(teamData); + + // Fetch team members + const membersResponse = await fetch(`/api/teams/${teamId}/members`); + if (!membersResponse.ok) { + throw new Error('Failed to fetch team members'); + } + const membersData = await membersResponse.json(); + setTeamMembers(membersData); + } catch (err) { + console.error(err); + setError(err.message); + } finally { + setLoading(false); + } + } + + if (status === 'authenticated') { + fetchTeamData(); + } + }, [teamId, status]); + + // Handle authentication + useEffect(() => { + if (status === 'unauthenticated') { + router.push('/login'); + } + }, [status, router]); + + if (status === 'loading' || loading) { + return
Loading...
; + } + + if (error) { + return
Error: {error}
; + } + + if (!team) { + return
Team not found
; + } + + return ( +
+
+ ← +
Team Details
+
+
+ +
+

{team.name}

+ +
+

Team Code

+
{team.code}
+

Share this code with others to join your team

+
+ +
+

Team Members

+ {teamMembers.length === 0 ? ( +

No members have joined yet

+ ) : ( +
    + {teamMembers.map(member => ( +
  • + {member.username} +
  • + ))} +
+ )} +
+ +
+ + Start Hunt + +
+
+ +
+
+
🏠
+
+
+
📊
+
+
+
🏆
+
+
+
👤
+
+
+
+ ); +} \ No newline at end of file diff --git a/app/actions/auth.js b/app/actions/auth.js index 3f239ea..c9c88cf 100644 --- a/app/actions/auth.js +++ b/app/actions/auth.js @@ -73,4 +73,66 @@ export async function validateCredentials(username, password) { } } -// Other functions remain the same... \ No newline at end of file +// Join a team +export async function joinTeam(userId, teamCode) { + try { + // Find the team with the given code + const team = await queryOne( + 'SELECT * FROM teams WHERE code = ?', + [teamCode] + ); + + if (!team) { + return { success: false, error: 'Invalid team code' }; + } + + // Update the user's team_id + await run( + 'UPDATE users SET team_id = ? WHERE id = ?', + [team.id, userId] + ); + + return { + success: true, + teamId: team.id + }; + } catch (error) { + console.error('Join team error:', error); + return { + success: false, + error: 'Failed to join team: ' + error.message + }; + } +} + +// Start/Stop Hunt Timer +export async function startHuntTimer(active) { + try { + // Update the hunt status + await run( + 'UPDATE hunt_settings SET active = ?', + [active ? 1 : 0] + ); + + return { success: true }; + } catch (error) { + console.error('Hunt timer error:', error); + return { + success: false, + error: 'Failed to update hunt timer: ' + error.message + }; + } +} + +// Get Hunt Status +export async function getHuntStatus() { + try { + const settings = await queryOne('SELECT active FROM hunt_settings LIMIT 1'); + return { + active: settings ? Boolean(settings.active) : false + }; + } catch (error) { + console.error('Get hunt status error:', error); + throw error; + } +} \ No newline at end of file diff --git a/app/api/teams/[teamId]/route.js b/app/api/teams/[teamId]/route.js index f1aca38..eb7bbf4 100644 --- a/app/api/teams/[teamId]/route.js +++ b/app/api/teams/[teamId]/route.js @@ -6,7 +6,7 @@ export async function GET(request, { params }) { const session = await getServerSession(authOptions); if (!session) { - return Response.json({ error: 'Unauthorized' }, { status: 401 }); + return Response.json({ error: 'Unauthorised' }, { status: 401 }); } try {