5
5
* This source code is licensed under the license found in the LICENSE file in
6
6
* the root directory of this source tree.
7
7
*/
8
- import { abortableGet , post , del } from 'lib/AJAX' ;
9
8
import keyMirror from 'lib/keyMirror' ;
10
9
import Parse from 'parse' ;
11
10
import { List , Map } from 'immutable' ;
@@ -20,53 +19,46 @@ const LASTFETCHTIMEOUT = 60000;
20
19
// - showMore: Flag to show/hide button to fetch all audiences
21
20
22
21
// xhr map, key value pair of xhrKey, xhr reference
23
- let xhrMap = { } ;
24
22
25
23
function PushAudiencesStore ( state , action ) {
26
24
action . app . setParseKeys ( ) ;
27
- let urlPrefix = `/apps/${ action . app . slug } /dashboard_ajax/push_audiences` ;
28
- let legacyUrlPrefix = `/apps/${ action . app . slug } /push_audiences` ;
29
25
switch ( action . type ) {
30
26
case ActionTypes . FETCH :
31
27
if ( state && new Date ( ) - state . get ( 'lastFetch' ) < LASTFETCHTIMEOUT ) { //check for stale store
32
28
if ( state . get ( 'audiences' ) && state . get ( 'audiences' ) . size >= ( action . min || 0 ) ) { //check for valid audience size
33
29
return Parse . Promise . as ( state ) ;
34
30
}
35
31
}
36
- let { xhr, promise} = abortableGet ( action . limit ? urlPrefix + `?audience_limit=${ action . limit } ` : urlPrefix , action . xhrKey !== null ) ;
37
- xhrMap [ action . xhrKey ] = xhr ;
38
- return promise . then ( ( { audiences, showMore } ) => {
39
- return Map ( { lastFetch : new Date ( ) , audiences : List ( audiences ) , showMore : showMore } ) ;
32
+ const path = action . limit ? `push_audiences?audience_limit=${ action . limit } ` : 'push_audiences' ;
33
+ const promise = Parse . _request ( 'GET' , path , { } , { useMasterKey : true } ) ;
34
+
35
+ return promise . then ( ( { results, showMore } ) => {
36
+ return Map ( { lastFetch : new Date ( ) , audiences : List ( results ) , showMore : showMore } ) ;
40
37
} ) ;
41
38
case ActionTypes . CREATE :
42
- return post ( legacyUrlPrefix , {
43
- query : action . query ,
44
- name : action . name ,
45
- } ) . then ( ( { new_audience } ) => {
46
- return state . update ( 'audiences' , ( audiences ) => {
47
- return audiences . unshift ( {
48
- createdAt : new Date ( ) ,
49
- name : action . name ,
50
- objectId : new_audience ? new_audience . objectId || - 1 : - 1 ,
51
- count : 0 ,
52
- query : JSON . parse ( action . query ) ,
39
+ return Parse . _request ( 'POST' , 'push_audiences' , { query : action . query , name : action . name , } , { useMasterKey : true } )
40
+ . then ( ( { new_audience } ) => {
41
+ return state . update ( 'audiences' , ( audiences ) => {
42
+ return audiences . unshift ( {
43
+ createdAt : new Date ( ) ,
44
+ name : action . name ,
45
+ objectId : new_audience ? new_audience . objectId || - 1 : - 1 ,
46
+ count : 0 ,
47
+ query : JSON . parse ( action . query ) ,
48
+ } ) ;
49
+ } ) ;
53
50
} ) ;
54
- } ) ;
55
- } ) ;
56
51
case ActionTypes . DESTROY :
57
- return del ( legacyUrlPrefix + `/${ action . objectId } ` ) . then ( ( ) => {
58
- return state . update ( 'audiences' , ( audiences ) => {
59
- let index = audiences . findIndex ( function ( audience ) {
60
- return audience . objectId === action . objectId ;
52
+ return Parse . _request ( 'DELETE' , `push_audiences/${ action . objectId } ` , { } , { useMasterKey : true } )
53
+ . then ( ( ) => {
54
+ return state . update ( 'audiences' , ( audiences ) => {
55
+ let index = audiences . findIndex ( function ( audience ) {
56
+ return audience . objectId === action . objectId ;
57
+ } ) ;
58
+ return audiences . delete ( index ) ;
59
+ } ) ;
61
60
} ) ;
62
- return audiences . delete ( index ) ;
63
- } ) ;
64
- } ) ;
65
61
case ActionTypes . ABORT_FETCH :
66
- let xhrKey = action . xhrKey ;
67
- if ( xhrMap [ xhrKey ] ) {
68
- xhrMap [ xhrKey ] . abort ( ) ;
69
- }
70
62
return Parse . Promise . as ( state ) ;
71
63
}
72
64
}
0 commit comments