Skip to content

Commit

Permalink
added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
aj-bhatia committed Jan 9, 2024
1 parent 15629f9 commit 5334d7c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 40 deletions.
66 changes: 42 additions & 24 deletions src/utils/apis/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
} from "../types"
// eslint-disable-next-line import/named
import { User } from "firebase/auth"
import { Dispatch } from "react"

/**
* Function using Firebase sdk for checking if an application is
Expand Down Expand Up @@ -229,7 +228,7 @@ export const updateApplicationRsvp = async (email: string, rsvp: boolean) => {
/**
* Function using Firebase sdk for returning the team information of a user
* @param user Firebase User
* @returns teamProfile if email found, otherwise an error is thrown
* @returns teamProfile if user found, otherwise an error is thrown
*
*/
export const getTeamProfile = async (user: User) => {
Expand All @@ -242,6 +241,7 @@ export const getTeamProfile = async (user: User) => {
const userInvites = userDocSnap.data()?.invites
const teamName = userDocSnap.data()?.teamName

// if user is not apart of a team, return their team invites
if (teamName == "") return {invites: userInvites}

const teamDocRef = doc(db, `teams/${teamName}`)
Expand All @@ -262,7 +262,7 @@ export const getTeamProfile = async (user: User) => {
* Function using Firebase sdk to remove a member of the team
* @param user Firebase User who is the team leader
* @param email Firebase User's Email to be removed
* @returns true if success, error thrown otherwise
* @returns updated team profile if success, error thrown otherwise
*
*/
export const removeTeamMember = async (user: User, email: string) => {
Expand All @@ -286,10 +286,12 @@ export const removeTeamMember = async (user: User, email: string) => {

const teamMembers = teamDocSnap.data()?.teamMembers

// get team members except for the one removed
const newTeamMembers = teamMembers.filter((teamMember: TeamMemberProps) => teamMember.memberEmail !== email)

const invitedTeamMembers = teamDocSnap.data()?.invitedTeamMembers

// get invited team members except for the one removed
const newInvitedTeamMembers = invitedTeamMembers.filter((teamMember: TeamMemberProps) => teamMember.memberEmail !== email)

await updateDoc(teamDocRef, {
Expand All @@ -300,6 +302,9 @@ export const removeTeamMember = async (user: User, email: string) => {
const oldMemberDocRef = doc(db, `users/${email}/user_items/team`)
const oldMemberDocSnap = await getDoc(oldMemberDocRef)
const oldMemberInvites = oldMemberDocSnap.data()?.invites

// get invites except for the team removed
// NOTE: I dont think this is needed, invites could be set to [] as the use shouldn't have any invites if they have been on a team
const newOldMemberInvites = oldMemberInvites.filter((invites: InvitationProps) => invites.teamName !== userTeamName)


Expand All @@ -320,10 +325,10 @@ export const removeTeamMember = async (user: User, email: string) => {
}

/**
* Function using Firebase sdk to remove a member of the team
* Function using Firebase sdk to delete a team
* @param user Firebase User who is the team leader
* @param email Firebase User's Email to be removed
* @returns true if team deletion successful, error thrown otherwise
* @param teamName Team to be removed
* @returns Empty team profile if team deletion successful, error thrown otherwise
*
*/
export const deleteTeam = async (user: User, teamName: string) => {
Expand Down Expand Up @@ -365,9 +370,10 @@ export const deleteTeam = async (user: User, teamName: string) => {
}

/**
* Function using Firebase sdk for returning the team information of a user
* Function using Firebase sdk for setting lockedIn value to true
* @param user Firebase User
* @returns teamProfile if email found, otherwise an error is thrown
* @param teamName Team to be locked
* @returns updated team profile if success, otherwise an error is thrown
*
*/
export const lockTeam = async (user: User, teamName: string) => {
Expand Down Expand Up @@ -410,9 +416,10 @@ export const lockTeam = async (user: User, teamName: string) => {
}

/**
* Function using Firebase sdk for returning the team information of a user
* Function using Firebase sdk for creating a team in Firestore
* @param user Firebase User
* @returns teamProfile if email found, otherwise an error is thrown
* @param teamName Team name to be created
* @returns new teamProfile created, otherwise an error is thrown
*
*/
export const createTeam = async (user: User, teamName: string) => {
Expand Down Expand Up @@ -459,9 +466,10 @@ export const createTeam = async (user: User, teamName: string) => {
}

/**
* Function using Firebase sdk for returning the team information of a user
* Function using Firebase sdk for inviting a team member to a team
* @param user Firebase User
* @returns teamProfile if email found, otherwise an error is thrown
* @param email Firebase User's Email to be invited
* @returns updated teamProfile if email found, otherwise an error is thrown
*
*/
export const inviteTeamMember = async (user: User, email: string) => {
Expand All @@ -477,7 +485,6 @@ export const inviteTeamMember = async (user: User, email: string) => {
if (userTeamLeader !== user.email) throw new Error("User is not the team leader")
if (email == user.email) throw new Error("Cannot invite yourself to the team")


const otherUserDocRef = doc(db, `users/${email}/user_items/team`)
const otherUserDocSnap = await getDoc(otherUserDocRef)

Expand All @@ -501,6 +508,8 @@ export const inviteTeamMember = async (user: User, email: string) => {

if (lockedIn) throw new Error("Team is locked in, cannot invite new members")
if (teamMembers.length + invitedTeamMembers.length >= 4) throw new Error("Team is full, remove a team member or univnite a member to invite a new member")

// check that the user has not been already invited to the team
if (otherUserDocSnap.data()?.invites !== undefined && otherUserDocSnap.data()?.invites.some((team: TeamProps) => team.teamName == teamName)) throw new Error("User has already been invited to the team")
if (invitedTeamMembers !== undefined && invitedTeamMembers.some((member: TeamMemberProps) => member.memberEmail == email)) throw new Error("User has already been invited to the team")
const invitedTeams = otherUserDocSnap.data()?.invites
Expand Down Expand Up @@ -528,9 +537,11 @@ export const inviteTeamMember = async (user: User, email: string) => {
}

/**
* Function using Firebase sdk for returning the team information of a user
* Function using Firebase sdk for accepting or denying a team's invite
* @param user Firebase User
* @returns teamProfile if email found, otherwise an error is thrown
* @param teamName Team name that is inviting the user
* @param status ACCEPTED or DENIED response to invite
* @returns updated team profile depending on status, otherwise an error is thrown
*
*/
export const rsvpInvite = async (user: User, teamName: string, status: string) => {
Expand All @@ -545,6 +556,8 @@ export const rsvpInvite = async (user: User, teamName: string, status: string) =
let teamDocSnap = await getDoc(teamDocRef)
if (!teamDocSnap.exists()) throw new Error("Team does not exist")
const invitedTeamMembers = teamDocSnap.data()?.invitedTeamMembers

// remove team member from the list of invitees of the team (doesn't matter if they accept or deny, they will need to be removed)
const newInvitedTeamMembers = invitedTeamMembers.filter((teamMember: TeamMemberProps) => teamMember.memberEmail !== user.email)

if (status === "ACCEPTED") {
Expand All @@ -558,14 +571,14 @@ export const rsvpInvite = async (user: User, teamName: string, status: string) =
const userNameDocSnap = await getDoc(userNameDocRef)
const fullName = userNameDocSnap.data()?.fullname


await updateDoc(teamDocRef, {
teamMembers: [...teamMembers, { memberName: fullName, memberEmail: user.email }],
invitedTeamMembers: newInvitedTeamMembers
})

const invitedTeams = userDocSnap.data()?.invites

// remove user from all other team's invite lists
for (let i = 0; i < invitedTeams.length; i++) {
if (invitedTeams[i].teamName != teamName) {
const tempTeamDocRef = doc(db, `teams/${invitedTeams[i].teamName}`)
Expand Down Expand Up @@ -614,6 +627,12 @@ export const rsvpInvite = async (user: User, teamName: string, status: string) =
}
}

/**
* Function using Firebase sdk for removing a team member from a team using admin access
* @param email email address to be removed from their team
* @returns updated team profile if successful remove, otherwise an error is thrown
*
*/
export const removeTeamMemberAdmin = async (email: string) => {
try {
if (!email) throw new Error("No email provided")
Expand Down Expand Up @@ -659,9 +678,9 @@ export const removeTeamMemberAdmin = async (email: string) => {
}

/**
* Function using Firebase sdk for returning the team information of a user
* @param user Firebase User
* @returns teamProfile if email found, otherwise an error is thrown
* Function using Firebase sdk for removing or adding a lock in to a team
* @param teamName team to be locked or unlocked
* @returns updated team profile if successful, otherwise an error is thrown
*
*/
export const flipLockAdmin = async (teamName: string) => {
Expand Down Expand Up @@ -691,11 +710,10 @@ export const flipLockAdmin = async (teamName: string) => {
}

/**
* Function using Firebase sdk to remove a member of the team
* @param user Firebase User who is the team leader
* @param email Firebase User's Email to be removed
* @returns true if team deletion successful, error thrown otherwise
*
* Function using Firebase sdk for deleting a team using admin access
* @param teamName team to be deleted from the database
* @returns empty team profile if successful, otherwise an error is thrown
*
*/
export const deleteTeamAdmin = async (teamName: string) => {
try {
Expand Down
22 changes: 9 additions & 13 deletions src/views/portal/admin/teams/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Dispatch, useEffect, useReducer, useState } from "react"
import React, { useEffect, useState } from "react"
import { TeamFormationProps } from "../../../../utils/types";
import { getTeams } from "../../../../utils/apis/firebase";
import useAuth from "../../../../hooks/useAuth";
Expand All @@ -18,31 +18,27 @@ const TeamsAdmin = () => {
console.log(teams)
setTeams(teams)
setRender(true)

})
}, [])

if (render) {
return (
<div>
<h1 className='font-title text-xl'>Team Formation</h1>
<div className='flex flex-col flex-wrap gap-4'>
<div className="flex flex-row flex-wrap place-content-center justify-around rounded-xl border-2 border-[#DFDFF6] bg-[#FFF] p-5 drop-shadow-lg">
{teams.map((team) => {
return (
<div key={team.teamName} className="w-full rounded-lg border-2 border-[#DFDFDF] bg-[#DFDFDF] drop-shadow-sm sm:mb-4 md:mb-4 lg:mb-0 lg:w-1/2">
<TeamAdminDisplay teamPage={team} teamPages={teams} setTeamPages={setTeams}></TeamAdminDisplay>
</div>
)
})}
</div>
</div>
{teams.map((team) => {
return (
<div key={team.teamName} className="w-full gap-4 rounded-lg border-2 border-[#DFDFDF] bg-[#DFDFDF] drop-shadow-sm sm:mb-4 md:mb-4 lg:mb-0 lg:w-1/2">
<TeamAdminDisplay teamPage={team} teamPages={teams} setTeamPages={setTeams}></TeamAdminDisplay>
</div>
)
})}
</div>
</div>
)
} else {
return <div>loading</div>
}
}


export default TeamsAdmin
1 change: 1 addition & 0 deletions src/views/portal/hacker/teams/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const TeamHacker = () => {
invites: []
}

// reducer for teamPage to set all non-set fields in teamPage to initial fields
const teamPageReducer = (
state: TeamFormationProps,
action: Partial<TeamFormationProps>
Expand Down
6 changes: 3 additions & 3 deletions src/views/team/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Binary from "../../assets/Binary.jpg"
import Lake from "../../assets/illustrations/Lake.svg"
import LinkedIn from "../../assets/icons/LinkedIn.svg"
import CruzHacksLogo from "../../assets/logos/CruzHacks.svg"
// import { AsyncImage } from "loadable-image"
import { AsyncImage } from "loadable-image"
import Star from "../../components/Star"

const TeamMember = ({ name, role, image, linkedIn }: Organizer) => {
Expand All @@ -18,7 +18,7 @@ const TeamMember = ({ name, role, image, linkedIn }: Organizer) => {

return (
<div className='flex flex-col items-center gap-3'>
{/* <AsyncImage
<AsyncImage
src={image}
alt={name + " profile picture"}
style={{
Expand All @@ -29,7 +29,7 @@ const TeamMember = ({ name, role, image, linkedIn }: Organizer) => {
border: "4px solid rgb(211 218 244 / 0.2)",
}}
loader={<div className='h-8 w-8 bg-blue-imperial/50' />}
/> */}
/>
<h3 className='text-center font-title text-lg uppercase md:text-lg'>
{name}
</h3>
Expand Down

0 comments on commit 5334d7c

Please sign in to comment.