Skip to content

Commit

Permalink
feat: added remove team flow
Browse files Browse the repository at this point in the history
  • Loading branch information
akshatmittal61 committed Aug 27, 2023
1 parent b0053cd commit 6fec7df
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 4 deletions.
1 change: 1 addition & 0 deletions constants/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const defaultAvatar = "/vectors/user.svg";

export const admin_emails = [
"akshatmittal2506@gmail.com",
"meraki@iiitu.ac.in",
"clubforce@iiitu.ac.in",
"aavesh@iiitu.ac.in",
];
18 changes: 18 additions & 0 deletions controllers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,24 @@ export const updateEvent = async (req: ApiRequest, res: ApiResponse) => {
updateDetails[key] = req.body[key];
}
});
const foundEvent = await Event.findById(eventId);
if (
+foundEvent.teamSize === 1 &&
updateDetails.teamSize &&
+updateDetails.teamSize > 1
) {
return res.status(400).json({
message: "Cannot set team size greater than 1 for solo events",
});
} else if (
+foundEvent.teamSize > 1 &&
updateDetails.teamSize &&
+updateDetails.teamSize === 1
) {
return res.status(400).json({
message: "Cannot set team size to 1 for team events",
});
}
const updatedEvent = await Event.findByIdAndUpdate(
eventId,
{ $set: updateDetails },
Expand Down
8 changes: 8 additions & 0 deletions controllers/participation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,14 @@ export const participateInEvent = async (req: ApiRequest, res: ApiResponse) => {
const foundTeam = await Team.findById(teamId);
if (!foundTeam)
return res.status(404).json({ message: "Team not found" });
const noOfParticipants = await Participant.countDocuments({
team: teamId,
});
if (noOfParticipants >= foundEvent.teamSize) {
return res.status(409).json({
message: "Team is already full",
});
}
const newParticipant = await Participant.create({
event: eventId,
team: teamId,
Expand Down
49 changes: 46 additions & 3 deletions pages/admin/events/[...slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import {
removeParticipantFromEvent,
approveParticipant as approveParticipantApi,
} from "@/utils/api/participation";
import { getTeamsForEvent } from "@/utils/api/teams";
import {
getTeamsForEvent,
removeTeam as removeTeamApi,
} from "@/utils/api/teams";
import Member from "@/components/Member";
import Responsive from "@/layouts/Responsive";
import { stylesConfig } from "@/utils/functions";
Expand Down Expand Up @@ -171,12 +174,34 @@ const AdminEventPage: React.FC = () => {
}
};

const removeTeam = async (teamId: string) => {
try {
const res = await removeTeamApi(teamId);
if (!eventDetails.teamSize) {
toast.error("Something went wrong");
return;
} else if (eventDetails.teamSize === 1) {
toast.error("Something went wrong");
return;
} else if (eventDetails.teamSize > 1) {
const newRegistrations = registrations.filter(
(registration: any) => registration._id !== teamId
);
setRegistrations(newRegistrations);
}
return Promise.resolve(res.message ?? "Removed team");
} catch (error: any) {
console.error(error);
return Promise.reject(error.message ?? "Something went wrong");
}
};

useEffect(() => {
if (isLoggedIn) getEventDetails();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isCheckingLoggedIn]);
useEffect(() => {
getAllRegistrations();
if (eventDetails.teamSize) getAllRegistrations();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [eventDetails]);

Expand Down Expand Up @@ -373,7 +398,25 @@ const AdminEventPage: React.FC = () => {
)}
>
{team.name}
<AiOutlineDelete />
<AiOutlineDelete
onClick={() => {
toast.promise(
removeTeam(
team._id
),
{
loading:
"Removing team...",
success: (
message
) => message,
error: (
message
) => message,
}
);
}}
/>
</Typography>
<Responsive.Row>
{team.participants.map(
Expand Down
56 changes: 56 additions & 0 deletions pages/profile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import {
approveParticipant as approveParticipantApi,
} from "@/utils/api/participation";
import { getMyRegistrations } from "@/utils/api/users";
import { removeTeam as removeTeamApi } from "@/utils/api/teams";
import { stylesConfig } from "@/utils/functions";
import { useRouter } from "next/router";
import React, { useEffect, useState } from "react";
import { toast } from "react-hot-toast";
import { FiLogOut } from "react-icons/fi";
import { PiCaretLeftBold } from "react-icons/pi";
import { AiOutlineDelete } from "react-icons/ai";

interface IRegistration {
event: {
Expand Down Expand Up @@ -186,6 +188,28 @@ const ProfilePage: React.FC = () => {
}
};

const removeTeam = async (teamSize: number, teamId: string) => {
try {
const res = await removeTeamApi(teamId);
if (!teamSize) {
toast.error("Something went wrong");
return;
} else if (teamSize === 1) {
toast.error("Something went wrong");
return;
} else if (teamSize > 1) {
const newRegistrations: IRegistration[] = registrations.filter(
(registration) => registration.team?.id !== teamId
);
setRegistrations(newRegistrations);
}
return Promise.resolve(res.message ?? "Removed team");
} catch (error: any) {
console.error(error);
return Promise.reject(error.message ?? "Something went wrong");
}
};

useEffect(() => {
if (!isCheckingLoggedIn) {
if (!isLoggedIn) {
Expand Down Expand Up @@ -369,6 +393,38 @@ const ProfilePage: React.FC = () => {
registration.team
?.name
}
{registration.team
?.createdBy ===
user?._id ? (
<AiOutlineDelete
onClick={() => {
toast.promise(
removeTeam(
+registration
.event
.teamSize,
registration
.team
?.id ??
""
),
{
loading:
"Removing team...",
success:
(
message
) =>
message,
error: (
message
) =>
message,
}
);
}}
/>
) : null}
</Typography>
<Typography
type="heading"
Expand Down
11 changes: 11 additions & 0 deletions styles/pages/Profile.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@
align-items: flex-start;
flex-direction: column;
gap: 12px;

&-subheading {
display: flex;
justify-content: flex-start;
align-items: center;
gap: 8px;
svg {
@include init-button();
cursor: pointer;
}
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion styles/pages/admin/Event.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,14 @@
&-name {
width: 100%;
display: flex;
justify-content: space-between;
justify-content: flex-start;
align-items: center;
gap: 24px;

svg {
@include init-button();
cursor: pointer;
}
}
}

Expand Down

0 comments on commit 6fec7df

Please sign in to comment.