From ab3b6ea8e7cc3209fae822eb27756eefd489674f Mon Sep 17 00:00:00 2001 From: Anup Cowkur Date: Mon, 4 Dec 2023 17:41:21 +0530 Subject: [PATCH] chore(webapp): fetch team from API in Team page --- measure-web-app/app/[teamId]/team/page.tsx | 113 +++++++++++++++------ 1 file changed, 83 insertions(+), 30 deletions(-) diff --git a/measure-web-app/app/[teamId]/team/page.tsx b/measure-web-app/app/[teamId]/team/page.tsx index a4848274b..cc5edf629 100644 --- a/measure-web-app/app/[teamId]/team/page.tsx +++ b/measure-web-app/app/[teamId]/team/page.tsx @@ -1,7 +1,10 @@ "use client" import Dropdown from "@/app/components/dropdown"; -import { useState } from "react"; +import { getAccessTokenOrRedirectToAuth, logoutIfAuthError } from "@/app/utils/auth_utils"; +import { useEffect, useState } from "react"; +import { useRouter } from 'next/navigation'; + const getSelectedTeam = () => { const { pathname } = new URL(document.location.href) @@ -67,11 +70,51 @@ const teamMembers = [ ]; export default function Team({ params }: { params: { teamId: string } }) { - const defaultTeamName = "Anup's team" + + enum TeamsApiStatus { + Loading, + Success, + Error + } + const emptyTeam = { 'id': '', 'name': '' } + const [teamsApiStatus, setTeamsApiStatus] = useState(TeamsApiStatus.Loading); + const [team, setTeam] = useState(emptyTeam) + const [saveTeamNameButtonDisabled, setSaveTeamNameButtonDisabled] = useState(true); const [role, setRole] = useState("Owner") const [email, setEmail] = useState() + const router = useRouter(); + + const getTeam = async () => { + setTeamsApiStatus(TeamsApiStatus.Loading) + + const authToken = await getAccessTokenOrRedirectToAuth(router) + const origin = process.env.NEXT_PUBLIC_API_BASE_URL + const opts = { + headers: { + "Authorization": `Bearer ${authToken}` + } + }; + + const res = await fetch(`${origin}/teams`, opts); + if (!res.ok) { + setTeamsApiStatus(TeamsApiStatus.Error) + logoutIfAuthError(router, res) + return + } + + setTeamsApiStatus(TeamsApiStatus.Success) + const data: [{ id: string, name: string }] = await res.json() + + + setTeam(data.filter((i) => i.id === params.teamId)[0]) + } + + useEffect(() => { + getTeam() + }, []); + const handleInvite = (e: React.FormEvent) => { e.preventDefault() const teamId = getSelectedTeam() @@ -84,35 +127,45 @@ export default function Team({ params }: { params: { teamId: string } }) {

Team

-

Team name

-
-
- event.target.value === defaultTeamName ? setSaveTeamNameButtonDisabled(true) : setSaveTeamNameButtonDisabled(false)} className="w-96 border border-black rounded-md outline-none focus-visible:outline-yellow-300 text-black py-2 px-4 font-sans placeholder:text-neutral-400" /> - -
-
-

Invite team members

-
-
-
- ) => setEmail(e.target.value)} defaultValue={email} /> -
- setRole(item)} initialItemIndex={0} /> - -
- -
-

Members

-
-
- {teamMembers.map(({ id, email }) => ( -
-
{email}
-
-
+ + {/* Loading message for team */} + {teamsApiStatus === TeamsApiStatus.Loading &&

Loading team...

} + + {/* Error message for tean fetch error */} + {teamsApiStatus === TeamsApiStatus.Error &&

Error fetching team, please refresh page to try again

} + + {teamsApiStatus === TeamsApiStatus.Success && +
+

Team name

+
+
+ event.target.value === team.name ? setSaveTeamNameButtonDisabled(true) : setSaveTeamNameButtonDisabled(false)} className="w-96 border border-black rounded-md outline-none focus-visible:outline-yellow-300 text-black py-2 px-4 font-sans placeholder:text-neutral-400" /> + +
+
+

Invite team members

+
+
+
+ ) => setEmail(e.target.value)} defaultValue={email} /> +
+ setRole(item)} initialItemIndex={0} /> + +
+ +
+

Members

+
+
+ {teamMembers.map(({ id, email }) => ( +
+
{email}
+
+
+
+ ))}
- ))} -
+
}
) }