Skip to content

Commit

Permalink
Merge branch 'master' into auth-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzzshawn authored Dec 17, 2024
2 parents 1e54418 + 46440e0 commit 5d9f5ed
Show file tree
Hide file tree
Showing 14 changed files with 7,814 additions and 10,855 deletions.
46 changes: 31 additions & 15 deletions app/api/teams/create-team/route.jsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,49 @@
import dbConnect from "../../../../lib/dbConnect";
import { teamSchema } from "../../../../model/Schema/teamSchema";
import { TeamModel } from "../../../../model/Team";

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

try {
const { teamname, game, role, rank, server, language, players, requests } = await request.json();
// Parse JSON body from the request
const { teamname, game, role, rank, server, language, players, participantCount} = await request.json();

await TeamModel.create({
teamname,
game,
role,
rank,
server,
language,
players,
requests
});
// Zod validation
const parsedData = teamSchema.parse({
teamname,
game,
role,
rank,
server,
language,
players,
participantCount,
// requests,
});

// Ensure 'requests' is always an array
const formattedData = {
...parsedData,
requests: Array.isArray(parsedData.requests) ? parsedData.requests : [parsedData.requests],
};

// Create a new team document in the database
const team = await TeamModel.create(formattedData);

console.log("Team created successfully:", team);

return Response.json({
success: true,
message: 'Team created successfully.'
message: "Team created successfully.",
team,
}, { status: 201 });

} catch (error) {
console.error('Error creating team:', error);
console.error("Error creating team:", error);
return Response.json({
success: false,
message: 'Error creating team'
message: "Error creating team",
error: error.message,
}, { status: 500 });
}
}
18 changes: 8 additions & 10 deletions app/create/team/page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function CreateTeamForm() {
const [server, setServer] = useState("");
const [language, setLanguage] = useState("");
const [players, setPlayers] = useState("");
const [requests, setRequests] = useState("");
// const [requests, setRequests] = useState("");
const [participantCount, setParticipantCount] = useState("");

const { toast } = useToast();
Expand All @@ -40,7 +40,7 @@ export default function CreateTeamForm() {
server: "",
language: "",
players: "",
requests: "",
// requests: "",
participantCount: "",
},
shouldFocusError: false,
Expand All @@ -58,14 +58,12 @@ export default function CreateTeamForm() {
server !== "" &&
language !== "" &&
players !== "" &&
requests !== "" &&
// requests !== "" &&
participantCount !== ""
) {
const playersArray = players.split(",").map((player) => player.trim());
// const playersArray = players.split(",").map((player) => player.trim());
const dataWithPlayersArray = {
...data,
players: playersArray,
participantCount: parseInt(participantCount, 10),
...data
};

const response = await axios.post(
Expand All @@ -86,7 +84,7 @@ export default function CreateTeamForm() {
setServer("");
setLanguage("");
setPlayers("");
setRequests("");
// setRequests("");
setParticipantCount("");
}
} catch (error) {
Expand Down Expand Up @@ -274,7 +272,7 @@ export default function CreateTeamForm() {
</FormItem>
)}
/>
<FormField
{/* <FormField
control={form.control}
name="requests"
render={({ field }) => (
Expand All @@ -294,7 +292,7 @@ export default function CreateTeamForm() {
<FormMessage className="text-red-500" />
</FormItem>
)}
/>
/> */}
<FormField
control={form.control}
name="participantCount"
Expand Down
4 changes: 2 additions & 2 deletions app/news/page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import News from "../../components/News";

const page = async () => {
return (
<div className="flex flex-col">
{/* <div className="bg-cover bg-center bg-[url(https://im.indiatimes.in/facebook/2019/Oct/mobile_gaming_1570096408.jpg)] w-full flex flex-col justify-center items-center h-96 py-8 px-4"></div> */}
<div className="flex flex-col justify-center items-center ">
<div className="bg-cover mb-5 bg-center bg-[url(https://im.indiatimes.in/facebook/2019/Oct/mobile_gaming_1570096408.jpg)] w-5/6 rounded-sm flex flex-col justify-center items-center h-96 py-8 px-4 "></div>
<News />
</div>
);
Expand Down
100 changes: 65 additions & 35 deletions app/teams/page.jsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,82 @@
"use client";

import React from "react";
import FiltersSidebar from "../../components/FiltersSidebar";
import TeamCard from "../../components/TeamCard";
import React, { useState, useEffect } from "react";
import { useRouter } from "next/navigation";
import { useState, useEffect } from "react";
import axios from "axios";
import {
Card,
CardContent,
CardHeader,
CardTitle,
} from "../../@/components/ui/card";
import FiltersSidebar from "../../components/FiltersSidebar";
import TeamCard from "../../components/TeamCard";
import { Card, CardContent, CardHeader, CardTitle } from "../../@/components/ui/card";
import { Button } from "../../@/components/ui/button";

const TeamFinder = () => {
const router = useRouter();
const [teams, setTeams] = useState([]);
const [filteredTeams, setFilteredTeams] = useState([]); // For filtered display
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [filters, setFilters] = useState({ game: "", role: "", language: "" }); // Filters state

// Fetch teams from the backend
useEffect(() => {
const fetchTeams = async () => {
try {
const response = await axios.get("/api/teams/get-teams");
if (response.data.success) {
setTeams(response.data.teams);
setFilteredTeams(response.data.teams); // Initialize filtered teams
} else if (response.status === 401) {
router.push("/auth/login");
} else {
console.error("Failed to fetch teams:", response.data.message);
throw new Error(response.data.message || "Failed to fetch teams.");
}
} catch (error) {
console.error("Error fetching teams:", error);
setError("Unable to fetch teams. Please try again later.");
} finally {
setLoading(false);
}
};

fetchTeams();
}, []);
}, [router]);

// Apply filters to the team list
useEffect(() => {
const applyFilters = () => {
const filtered = teams.filter((team) => {
return (
(!filters.game || team.game === filters.game) &&
(!filters.role || team.role === filters.role) &&
(!filters.language || team.language === filters.language)
);
});
setFilteredTeams(filtered);
};

applyFilters();
}, [filters, teams]);

const NavigateToCreateTeam = () => {
router.push("/create/team");
// Reset filters
const handleResetFilters = () => {
setFilters({ game: "", role: "", language: "" });
};

if (loading) {
return (
<div className="flex items-center justify-center min-h-screen">
<p>Loading teams...</p>
</div>
);
}

if (error) {
return (
<div className="flex items-center justify-center min-h-screen">
<p className="text-red-500">{error}</p>
</div>
);
}

return (
<div className="mt-20 px-5 md:px-10 xl:px-[12%] min-h-[70vh] transition-all">
<h1 className="text-3xl ml-4 mb-14 font-semibold tracking-tight">
Expand All @@ -50,36 +88,28 @@ const TeamFinder = () => {
<CardHeader className="flex flex-col gap-5">
<CardTitle className="flex items-center justify-between">
<h2 className="text-2xl">Find Team</h2>
<Button
// variant="outline"
className=""
onClick={NavigateToCreateTeam}
>
<Button onClick={() => router.push("/create/team")}>
Create Team
</Button>
</CardTitle>

<div className="mb-4 flex flex-wrap gap-2 justify-between">
<Button variant="outline" className="px-4 py-2 rounded text-sm">
Request Raised
</Button>
<Button variant="outline" className="px-4 py-2 rounded text-sm">
Request Sent
</Button>
<Button variant="outline" className="px-4 py-2 rounded text-sm">
+ New Request
</Button>
</div>
</CardHeader>

<CardContent className="min-h-80 lg:h-full border-t pt-5 transition-all">
{teams.map((team, index) => (
<TeamCard key={index} team={team} />
))}
{filteredTeams.length > 0 ? (
filteredTeams.map((team, index) => (
<TeamCard key={index} team={team} />
))
) : (
<p>No teams found. Try creating one!</p>
)}
</CardContent>
</Card>

<FiltersSidebar className="" />
<FiltersSidebar
filters={filters}
setFilters={setFilters}
onReset={handleResetFilters}
/>
</div>
</div>
);
Expand Down
Loading

0 comments on commit 5d9f5ed

Please sign in to comment.