redirecting from the router to team dashboard if user is in a team
This commit is contained in:
+12
-1
@@ -13,14 +13,19 @@ import {
|
||||
FaBullhorn
|
||||
} from "react-icons/fa";
|
||||
import BackgroundLayout from "../../components/Layouts/BackgroundLayout"
|
||||
import TeamService from "../../services/teamService"
|
||||
|
||||
/**
|
||||
* figure out where to take the user based on everything
|
||||
*/
|
||||
|
||||
const userService: UserService = new UserService()
|
||||
const teamService: TeamService = new TeamService()
|
||||
|
||||
function RouteHandler() {
|
||||
const { currUser } = useAuth()
|
||||
const router = useRouter()
|
||||
const userService: UserService = new UserService()
|
||||
|
||||
const [loading, setLoading] = useState<Boolean>(true)
|
||||
const [user, setUser] = useState<User | null>(null)
|
||||
|
||||
@@ -44,6 +49,12 @@ function RouteHandler() {
|
||||
setUser(returnedUser)
|
||||
|
||||
// check if user is in a team go to the team dashboard
|
||||
const returnedTeamMembers = await teamService.getTeamMembersByUserId(currUser.uid)
|
||||
if (returnedTeamMembers.length > 0) {
|
||||
console.log('in a team! going to the team dashboard of the first one!')
|
||||
router.push('/teams/' + returnedTeamMembers[0].teamId)
|
||||
return
|
||||
}
|
||||
|
||||
} catch(error) {
|
||||
console.log(error)
|
||||
|
||||
@@ -104,6 +104,35 @@ export default class TeamService implements IService {
|
||||
return teamMember
|
||||
}
|
||||
|
||||
async getTeamMembersByUserId(userId: string): Promise<TeamMember[]> {
|
||||
const q = query(
|
||||
collection(this.db, Collections.teamMembers),
|
||||
where("userId", "==", userId),
|
||||
)
|
||||
|
||||
const querySnapshot = await getDocs(q);
|
||||
|
||||
if (querySnapshot.size > 1) {
|
||||
console.log('this user is part of multiple teams')
|
||||
}
|
||||
|
||||
console.log(querySnapshot)
|
||||
|
||||
var teamMembers: TeamMember[] = []
|
||||
|
||||
querySnapshot.forEach((doc) => {
|
||||
// doc.data() is never undefined for query doc snapshots
|
||||
// get the first one and just return...shouldn't be more
|
||||
console.log('got teammember data')
|
||||
let teamMember:TeamMember = doc.data() as TeamMember
|
||||
teamMember.id = doc.id
|
||||
|
||||
teamMembers.push(teamMember)
|
||||
});
|
||||
|
||||
return teamMembers
|
||||
}
|
||||
|
||||
async updateTeamMember(teamMember: TeamMember) {
|
||||
const docRef = doc(this.db, Collections.teamMembers, teamMember.id);
|
||||
await setDoc(docRef, { ...teamMember, lastUpdatedDate: serverTimestamp() }, { merge: true })
|
||||
|
||||
Reference in New Issue
Block a user