Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
useLeaveAndJoin
Browse files Browse the repository at this point in the history
useLeaveAndJoin
  • Loading branch information
serranoio committed May 2, 2023
1 parent 34652b9 commit 28cff76
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
28 changes: 28 additions & 0 deletions client/src/api/hooks/team/useLeaveAndJoin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react'
import { useMutation, useQueryClient } from 'react-query'
import { useNavigate } from 'react-router-dom'

import http from '../../../http'

const { api } = http

export const useLeaveAndJoin = () => {
const queryClient = useQueryClient()
const navigate = useNavigate()

const leaveAndJoin = async (details) => {
const leave = await api.put(`/teams/leave`, details)

const join = await api.put('/teams/join', details)

return { leave: leave.data, join: join.data }
}

return useMutation(leaveAndJoin, {
mutationKey: 'leaveAndJoin',
onSuccess: async () => {
await queryClient.invalidateQueries('checkAuth', { refetchInactive: true })
navigate('/my-team')
},
})
}
4 changes: 3 additions & 1 deletion client/src/components/Forms/TeamForm/TeamForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ function TeamForm() {
return <Loader />
}

if (team === undefined) {
console.log(user)

if (user.team === undefined) {
return <NoTeam />
}

Expand Down
32 changes: 27 additions & 5 deletions client/src/components/Forms/TeamsList/TeamsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import teamsAPI from '../../../api/endpoints/team'
import { useCheckAuth } from '../../../api/hooks/auth/useCheckAuth'
// import { useAddUserToTeam } from '../../../api/hooks/team/useAddUserToTeam'
import { useJoinTeam } from '../../../api/hooks/team/useJoinTeam'
import { useLeaveAndJoin } from '../../../api/hooks/team/useLeaveAndJoin'
import { useLeave } from '../../../api/hooks/team/useLeaveTeam'
import { B2fs, B2fw, B2lh, B3fs, B3fw, B3lh } from '../../../assets/fonts'
import { LOCAL_PATH } from '../../../http'
Expand Down Expand Up @@ -54,12 +55,13 @@ function TeamsList() {

const { mutate: leaveTeam, isLoading: isLeaving } = useLeave()

const { mutate: leaveAndJoin, isLoading: isLeavingAndJoining } = useLeaveAndJoin()

useEffect(() => {
const makeRequest = async () => {
const teams = await teamsAPI.getAllTeams()

console.log(teams)
setTeams(teams.data)
setTeams(teams.data.filter((team) => team.type === 'open'))
setIsTeamsLoading(false)
}

Expand All @@ -77,7 +79,6 @@ function TeamsList() {
user_id: user?._id,
teamid: user?.team._id,
})
handleClose()
}

const handleClose = () => {
Expand All @@ -102,21 +103,42 @@ function TeamsList() {
// }
}

const handleLeaveAndJoin = () => {
leaveAndJoin({ user_id: user?._id, teamid: user?.team._id })
}

if (isUserTeamLoading || isTeamsLoading || isLeaving) {
return <Loader />
}

console.log(user)
// lol we could move entire teams list to display her implementation
// of checking out a team

const getModalState = () => {
if (changeModal === 'alreadyOnTeam') {
if (user.team.leader === user._id) {
const isOnlyMember =
user.team.members.length === 1 ? 'You must delete team' : 'You must transfer leadership'

return (
<Box sx={style}>
<TeamActionModal
firstText="You cannot join team."
secondText={`${isOnlyMember} to join new team.`}
firstButton="Okay"
firstButtonHandler={handleClose}
/>
</Box>
)
}

return (
<Box sx={style}>
<TeamActionModal
firstText="You're already on a team."
secondText="Do you want to leave current team?"
firstButton="Leave Current Team"
secondText="Do you want to leave current team and join new?"
firstButton="Leave & Join"
firstButtonHandler={handleLeave}
secondButton="Cancel"
secondButtonHandler={handleClose}
Expand Down

0 comments on commit 28cff76

Please sign in to comment.