From c7f406840d6e275453f1798e18e7b5c736a50f11 Mon Sep 17 00:00:00 2001 From: Marc Farra Date: Fri, 26 Nov 2021 17:02:47 +0200 Subject: [PATCH] Separate team members call to API --- app/manage/index.js | 2 ++ app/manage/teams.js | 28 +++++++++++++++++++++++----- lib/teams-api.js | 18 ++++++++++++++++++ pages/team-edit.js | 4 ++-- pages/team.js | 14 ++++++++------ 5 files changed, 53 insertions(+), 13 deletions(-) diff --git a/app/manage/index.js b/app/manage/index.js index 1c4d21f7..b31b7a88 100644 --- a/app/manage/index.js +++ b/app/manage/index.js @@ -12,6 +12,7 @@ const { createTeam, destroyTeam, getTeam, + getTeamMembers, joinTeam, listTeams, listMyTeams, @@ -95,6 +96,7 @@ function manageRouter (nextApp) { router.get('/api/my/teams', can('public:authenticated'), listMyTeams) router.post('/api/teams', can('public:authenticated'), createTeam) router.get('/api/teams/:id', can('team:view'), getTeam) + router.get('/api/teams/:id/members', can('team:view'), getTeamMembers) router.put('/api/teams/:id', can('team:edit'), updateTeam) router.delete('/api/teams/:id', can('team:edit'), destroyTeam) router.put('/api/teams/add/:id/:osmId', can('team:edit'), addMember) diff --git a/app/manage/teams.js b/app/manage/teams.js index 85d359a1..14e2ffd0 100644 --- a/app/manage/teams.js +++ b/app/manage/teams.js @@ -53,21 +53,38 @@ async function getTeam (req, reply) { try { const teamData = await team.get(id) const associatedOrg = await team.associatedOrg(id) - const memberIds = map(getOsmId, (await team.getMembers(id))) - const members = await team.resolveMemberNames(memberIds) - const moderators = await team.getModerators(id) - if (!teamData && !members) { + if (!teamData) { return reply.boom.notFound() } - return reply.send(Object.assign({}, teamData, { members, moderators }, { org: associatedOrg })) + return reply.send(Object.assign({}, teamData, { org: associatedOrg })) } catch (err) { console.log(err) return reply.boom.badRequest(err.message) } } +async function getTeamMembers (req, reply) { + const { id } = req.params + + if (!id) { + return reply.boom.badRequest('team id is required') + } + + try { + const memberIds = map(getOsmId, (await team.getMembers(id))) + const members = await team.resolveMemberNames(memberIds) + const moderators = await team.getModerators(id) + + return reply.send(Object.assign({}, { teamId: id }, { members, moderators })) + } catch (err) { + console.log(err) + return reply.boom.badRequest(err.message) + } + +} + async function createTeam (req, reply) { const { body } = req const { user_id } = reply.locals @@ -264,6 +281,7 @@ module.exports = { createTeam, destroyTeam, getTeam, + getTeamMembers, joinTeam, listTeams, listMyTeams, diff --git a/lib/teams-api.js b/lib/teams-api.js index 705377f5..5cb7d72a 100644 --- a/lib/teams-api.js +++ b/lib/teams-api.js @@ -113,6 +113,24 @@ export async function destroyTeam (id) { }) } +/** + * getMembers + * Get a team's members + * + * @param id - Team id + * @returns {Response} + */ +export async function getTeamMembers (id) { + let res = await fetch(join(URL, `${id}`, 'members')) + if (res.status === 200) { + return res.json() + } else { + const err = new Error('could not retrieve team members') + err.status = res.status + throw err + } +} + /** * updateMembers * Update a team's members diff --git a/pages/team-edit.js b/pages/team-edit.js index 73905dda..d3c153c5 100644 --- a/pages/team-edit.js +++ b/pages/team-edit.js @@ -34,9 +34,9 @@ export default class TeamEdit extends Component { let team = await getTeam(id) let teamAttributes = [] let profileValues = [] + profileValues = await getTeamProfile(id) if (team.org) { teamAttributes = await getOrgTeamAttributes(team.org.organization_id) - profileValues = await getTeamProfile(id) } this.setState({ team, @@ -140,7 +140,7 @@ export default class TeamEdit extends Component { { diff --git a/pages/team.js b/pages/team.js index 79fbbf97..43a751a7 100644 --- a/pages/team.js +++ b/pages/team.js @@ -12,7 +12,7 @@ import AddMemberForm from '../components/add-member-form' import ProfileModal from '../components/profile-modal' import theme from '../styles/theme' -import { getTeam, addMember, removeMember, joinTeam, assignModerator, removeModerator } from '../lib/teams-api' +import { getTeam, getTeamMembers, addMember, removeMember, joinTeam, assignModerator, removeModerator } from '../lib/teams-api' import { getTeamProfile, getUserOrgProfile, getUserTeamProfile } from '../lib/profiles-api' const Map = dynamic(() => import('../components/team-map'), { ssr: false }) @@ -46,10 +46,12 @@ export default class Team extends Component { const { id } = this.props try { let team = await getTeam(id) + let teamMembers = await getTeamMembers(id) let teamProfile = await getTeamProfile(id) this.setState({ team, teamProfile, + teamMembers, loading: false }) } catch (e) { @@ -167,7 +169,7 @@ export default class Team extends Component { } render () { - const { team, error, teamProfile } = this.state + const { team, error, teamProfile, teamMembers } = this.state if (error) { if (error.status === 401 || error.status === 403) { @@ -191,11 +193,11 @@ export default class Team extends Component { } } - if (!team) return null + if (!team || !teamMembers) return null const userId = this.props.user.uid - const members = map(prop('id'), team.members) - const moderators = map(prop('osm_id'), team.moderators) + const members = map(prop('id'), teamMembers.members) + const moderators = map(prop('osm_id'), teamMembers.moderators) // TODO: moderators is an array of ints while members are an array of strings. fix this. const isUserModerator = contains(parseInt(userId), moderators) @@ -207,7 +209,7 @@ export default class Team extends Component { { key: 'role' } ] - let memberRows = team.members.map(member => { + let memberRows = teamMembers.members.map(member => { const role = contains(parseInt(member.id), moderators) ? 'moderator' : 'member' return assoc('role', role, member) })