Skip to content

Commit

Permalink
Merge branch 'master' into feature/createTeamPath
Browse files Browse the repository at this point in the history
  • Loading branch information
dinxsh authored Aug 6, 2024
2 parents 5d0ac61 + d40b3a7 commit cfce1a6
Show file tree
Hide file tree
Showing 11 changed files with 718 additions and 142 deletions.
20 changes: 20 additions & 0 deletions app/api/brackets/[id]/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import dbConnect from '../../../../lib/dbConnect';
import Bracket from '../../../../model/Bracket';
import { NextResponse } from 'next/server';

export async function GET(request, { params }) {
const { id } = params;
console.log('Fetching bracket with ID:', id);
try {
await dbConnect();
const bracket = await Bracket.findById(id);
if (!bracket) {
console.log('Bracket not found');
return NextResponse.json({ error: 'Bracket not found' }, { status: 404 });
}
return NextResponse.json(bracket);
} catch (error) {
console.error('Error fetching bracket:', error);
return NextResponse.json({ error: 'Internal Server Error', details: error.message }, { status: 500 });
}
}
48 changes: 48 additions & 0 deletions app/api/brackets/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import dbConnect from '../../../lib/dbConnect';
import Bracket from '../../../model/Bracket';
import { NextResponse } from 'next/server';
import Tournament from '../../../model/Tournament';

export async function POST(request) {
try {
await dbConnect();

const { tournamentId, bracketName, bracketImage, bracketData } = await request.json();

const newBracket = new Bracket({
tournamentId,
bracketName,
bracketImage,
bracketData
});

await newBracket.save();

// Update the tournament with the new bracket
await Tournament.findByIdAndUpdate(tournamentId,
{ $push: { brackets: newBracket._id } }
);

return NextResponse.json({
message: 'Bracket created and associated with tournament successfully',
id: newBracket._id
}, { status: 201 });
} catch (error) {
console.error('Error creating bracket:', error);
return NextResponse.json({
error: 'Internal Server Error',
details: error.message
}, { status: 500 });
}
}

export async function GET() {
try {
await dbConnect();
const brackets = await Bracket.find({}).sort({ createdAt: -1 });
return NextResponse.json(brackets);
} catch (error) {
console.error('Error fetching brackets:', error);
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
}
}
15 changes: 15 additions & 0 deletions app/api/tournaments/[id]/brackets/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import dbConnect from '../../../../lib/dbConnect';
import Bracket from '../../../../model/Bracket';
import { NextResponse } from 'next/server';

export async function GET(request, { params }) {
const { id } = params;
try {
await dbConnect();
const brackets = await Bracket.find({ tournamentId: id });
return NextResponse.json(brackets);
} catch (error) {
console.error('Error fetching brackets for tournament:', error);
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
}
}
54 changes: 54 additions & 0 deletions app/bracket/[id]/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use client';

import { useEffect, useState } from 'react';
import { useParams } from 'next/navigation';

const BracketDisplay = () => {
const [bracket, setBracket] = useState(null);
const [error, setError] = useState(null);
const params = useParams();
const { id } = params;

useEffect(() => {
const fetchBracket = async () => {
try {
console.log('Fetching bracket with id:', id);
const response = await fetch(`/api/brackets/${id}`);
if (!response.ok) {
throw new Error(`Failed to fetch bracket: ${response.status} ${response.statusText}`);
}
const data = await response.json();
console.log('Received bracket data:', data);
setBracket(data);
} catch (error) {
console.error('Error fetching bracket:', error);
setError(error.message);
}
};

if (id) {
fetchBracket();
}
}, [id]);

if (error) {
return <div>Error: {error}</div>;
}

if (!bracket) {
return <div>Loading...</div>;
}

return (
<div>
<h1 className="text-3xl font-bold mb-4">{bracket.bracketName}</h1>
{bracket.bracketImage ? (
<img src={bracket.bracketImage} alt="Tournament Bracket" className="max-w-full h-auto" />
) : (
<p>No bracket image available</p>
)}
</div>
);
};

export default BracketDisplay;
6 changes: 3 additions & 3 deletions app/bracket/page.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import TournamentBracket from "../../components/TournamentBracket";

import BracketList from "../../components/BracketList";

const BracketPage = () => {

return (
<div>
<h1 className="text-3xl font-bold text-center my-8">Tournament Manager</h1>
<TournamentBracket />
<BracketList />
</div>
);
};

export default BracketPage;
export default BracketPage;
75 changes: 75 additions & 0 deletions components/BracketList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"use client";

import React, { useState, useEffect } from "react";
import Link from "next/link";
import Image from "next/image";

const BracketList = () => {
const [brackets, setBrackets] = useState([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState(null);

useEffect(() => {
const fetchBrackets = async () => {
setIsLoading(true);
try {
const response = await fetch("/api/brackets");
if (!response.ok) {
throw new Error("Failed to fetch brackets");
}
const data = await response.json();
setBrackets(data);
} catch (error) {
console.error("Error fetching brackets:", error);
setError("Failed to load brackets. Please try again later.");
} finally {
setIsLoading(false);
}
};

fetchBrackets();
}, []);

if (isLoading) return <div>Loading brackets...</div>;
if (error) return <div>Error: {error}</div>;

return (
<div className="mt-8">
<h2 className="text-2xl font-bold mb-4">Existing Brackets</h2>
{brackets.length === 0 ? (
<p>No brackets found...</p>
) : (
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
{brackets.map((bracket) => (
<Link href={`/bracket/${bracket._id}`} key={bracket._id}>
<div className="border p-4 rounded-lg shadow hover:shadow-md transition-shadow cursor-pointer">
{bracket.bracketImage && (
<div className="relative w-full h-40 mb-2">
<Image
src={bracket.bracketImage}
alt={`Thumbnail for ${bracket.bracketName}`}
layout="fill"
objectFit="cover"
className="rounded"
/>
</div>
)}
<h3 className="text-xl font-semibold mb-2">
{bracket.bracketName}
</h3>
<p className="text-gray-600 mb-1">
Tournament: {bracket.tournamentName || "N/A"}
</p>
<p className="text-gray-600">
Created: {new Date(bracket.createdAt).toLocaleDateString()}
</p>
</div>
</Link>
))}
</div>
)}
</div>
);
};

export default BracketList;
Loading

0 comments on commit cfce1a6

Please sign in to comment.