2
2
* @module "services.communities"
3
3
* @desc Communities service.
4
4
*/
5
+ import _ from 'lodash' ;
5
6
import fetch from 'isomorphic-fetch' ;
6
7
import qs from 'qs' ;
7
8
import { config } from 'topcoder-react-utils' ;
8
9
10
+ /**
11
+ * Returns Community App URL, or throws an error if URL cannot be found in
12
+ * config.
13
+ */
14
+ function getCommunityAppUrl ( ) {
15
+ const url = _ . get ( config , 'URL.COMMUNITY_APP' ) ;
16
+ if ( ! url ) throw new Error ( 'No URL.COMMUNITY_APP param found in config' ) ;
17
+ return url ;
18
+ }
19
+
9
20
/**
10
21
* Client-side version of the service.
11
22
*/
@@ -24,33 +35,32 @@ class Communities {
24
35
* @return {Promise } Resolves to the array of community data objects. Each of
25
36
* the objects indludes only the most important data on the community.
26
37
*/
27
- getList ( userGroupIds ) {
28
- let url = `/community-app-assets/api/tc-communities?${ qs . stringify ( { groups : userGroupIds } ) } ` ;
29
- if ( config . URL && config . URL . COMMUNITY_APP ) {
30
- url = `${ config . URL . COMMUNITY_APP } url` ;
31
- }
32
- return fetch ( url , {
38
+ async getList ( userGroupIds ) {
39
+ let url = getCommunityAppUrl ( ) ;
40
+ url += '/community-app-assets/api/tc-communities?' ;
41
+ url += qs . stringify ( { groups : userGroupIds } ) ;
42
+ const res = await fetch ( url , {
33
43
headers : {
34
44
authorization : this . private . tokenV3 ,
35
45
} ,
36
- } ) . then ( res => res . json ( ) ) ;
46
+ } ) ;
47
+ return res . json ( ) ;
37
48
}
38
49
39
50
/**
40
51
* Gets metadata for the specified community.
41
52
* @param {String } communityId
42
53
* @return {Promise } Resolves to the community metadata.
43
54
*/
44
- getMetadata ( communityId ) {
45
- let url = `/community-app-assets/api/tc-communities/${ communityId } /meta` ;
46
- if ( config . URL && config . URL . COMMUNITY_APP ) {
47
- url = `${ config . URL . COMMUNITY_APP } url` ;
48
- }
49
- return fetch ( url , {
55
+ async getMetadata ( communityId ) {
56
+ let url = getCommunityAppUrl ( ) ;
57
+ url += `/community-app-assets/api/tc-communities/${ communityId } /meta` ;
58
+ const res = await fetch ( url , {
50
59
headers : {
51
60
authorization : this . private . tokenV3 ,
52
61
} ,
53
- } ) . then ( res => res . json ( ) ) ;
62
+ } ) ;
63
+ return res . json ( ) ;
54
64
}
55
65
}
56
66
0 commit comments