Skip to content

Commit 407b704

Browse files
committed
move badge client code to separate file
1 parent 9944c05 commit 407b704

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed

src/lib/badges-api.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const { default: ApiClient } = require('./api-client')
2+
const logger = require('./logger')
3+
4+
const apiClient = new ApiClient()
5+
6+
/**
7+
* Get badges from an organization
8+
*
9+
* @param {int} orgId - id of the organization
10+
* @returns {array[badges]}
11+
*/
12+
export async function getOrgBadges(orgId) {
13+
try {
14+
const badges = await apiClient.get(`/organizations/${orgId}/badges`)
15+
return badges
16+
} catch (e) {
17+
if (e.statusCode === 401) {
18+
logger.error("User doesn't have access to organization badges.")
19+
} else {
20+
logger.error(e)
21+
}
22+
}
23+
}
24+
25+
export async function getUserBadges(userId) {
26+
try {
27+
const badges = await apiClient.get(`/user/${userId}/badges`)
28+
return badges
29+
} catch (e) {
30+
if (e.statusCode === 401) {
31+
logger.error("User doesn't have access to organization badges.")
32+
} else {
33+
logger.error(e)
34+
}
35+
}
36+
}

src/pages/organizations/[id]/index.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,15 @@ import Button from '../../../components/button'
1919
import Modal from 'react-modal'
2020
import ProfileModal from '../../../components/profile-modal'
2121
import { contains, prop, map } from 'ramda'
22-
import APIClient from '../../../lib/api-client'
2322
import join from 'url-join'
2423
import { getSession } from 'next-auth/react'
24+
import { getOrgBadges, getUserBadges } from '../../../lib/badges-api'
2525
import TeamsTable from '../../../components/tables/teams'
2626
import UsersTable from '../../../components/tables/users'
2727
import logger from '../../../lib/logger'
2828

2929
const URL = process.env.APP_URL
3030

31-
const apiClient = new APIClient()
32-
3331
export function SectionWrapper(props) {
3432
return (
3533
<div>
@@ -72,7 +70,6 @@ class Organization extends Component {
7270

7371
this.closeProfileModal = this.closeProfileModal.bind(this)
7472
this.renderBadges = this.renderBadges.bind(this)
75-
this.getBadges = this.getBadges.bind(this)
7673
}
7774

7875
async componentDidMount() {
@@ -90,9 +87,9 @@ class Organization extends Component {
9087
const profileInfo = await getUserOrgProfile(id, user.id)
9188

9289
// Fetch badges for this organization
93-
const profileBadges = (
94-
await apiClient.get(`/user/${user.id}/badges`)
95-
).badges.filter((b) => b.organization_id === parseInt(id))
90+
const profileBadges = (await getUserBadges(user.id)).badges.filter(
91+
(b) => b.organization_id === parseInt(id)
92+
)
9693

9794
this.setState({
9895
profileInfo,
@@ -164,16 +161,17 @@ class Organization extends Component {
164161
async getBadges() {
165162
try {
166163
const { id: orgId } = this.props
167-
const badges = await apiClient.get(`/organizations/${orgId}/badges`)
164+
const badges = await getOrgBadges(orgId)
168165
this.setState({
169166
badges,
170167
})
171168
} catch (e) {
172-
if (e.statusCode === 401) {
173-
logger.error("User doesn't have access to organization badges.")
174-
} else {
175-
logger.error(e)
176-
}
169+
this.setState({
170+
org: {
171+
error: e,
172+
status: 'error',
173+
},
174+
})
177175
}
178176
}
179177

src/pages/teams/[id]/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { getOrgStaff } from '../../../lib/org-api'
3333
import { toast } from 'react-toastify'
3434
import logger from '../../../lib/logger'
3535
import MembersTable from './members-table'
36+
import { getOrgBadges } from '../../../lib/badges-api'
3637

3738
const APP_URL = process.env.APP_URL
3839
const Map = dynamic(() => import('../../../components/team-map'), {
@@ -101,18 +102,21 @@ class Team extends Component {
101102
let team = await getTeam(id)
102103
let teamMembers = { moderators: [], members: [] }
103104
let teamProfile = []
105+
let orgBadges = []
104106
teamMembers = await getTeamMembers(id)
105107
teamProfile = await getTeamProfile(id)
106108

107109
let orgOwners = []
108110
if (team.org) {
109111
orgOwners = (await getOrgStaff(team.org.organization_id)).owners
112+
orgBadges = await getOrgBadges(team.org.organization_id)
110113
}
111114
this.setState({
112115
team,
113116
teamProfile,
114117
teamMembers,
115118
orgOwners,
119+
orgBadges,
116120
loading: false,
117121
})
118122
} catch (e) {
@@ -262,6 +266,8 @@ class Team extends Component {
262266
const members = map(prop('id'), teamMembers.members)
263267
const moderators = map(prop('osm_id'), teamMembers.moderators)
264268

269+
logger.info(this.state)
270+
265271
// TODO: moderators is an array of ints while members are an array of strings. fix this.
266272
const isUserModerator =
267273
contains(parseInt(userId), moderators) ||

0 commit comments

Comments
 (0)