55 * This source code is licensed under the license found in the LICENSE file in
66 * the root directory of this source tree.
77 */
8- import { abortableGet , post , del } from 'lib/AJAX' ;
98import keyMirror from 'lib/keyMirror' ;
109import Parse from 'parse' ;
1110import { List , Map } from 'immutable' ;
@@ -20,53 +19,46 @@ const LASTFETCHTIMEOUT = 60000;
2019// - showMore: Flag to show/hide button to fetch all audiences
2120
2221// xhr map, key value pair of xhrKey, xhr reference
23- let xhrMap = { } ;
2422
2523function PushAudiencesStore ( state , action ) {
2624 action . app . setParseKeys ( ) ;
27- let urlPrefix = `/apps/${ action . app . slug } /dashboard_ajax/push_audiences` ;
28- let legacyUrlPrefix = `/apps/${ action . app . slug } /push_audiences` ;
2925 switch ( action . type ) {
3026 case ActionTypes . FETCH :
3127 if ( state && new Date ( ) - state . get ( 'lastFetch' ) < LASTFETCHTIMEOUT ) { //check for stale store
3228 if ( state . get ( 'audiences' ) && state . get ( 'audiences' ) . size >= ( action . min || 0 ) ) { //check for valid audience size
3329 return Parse . Promise . as ( state ) ;
3430 }
3531 }
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 } ) ;
4037 } ) ;
4138 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+ } ) ;
5350 } ) ;
54- } ) ;
55- } ) ;
5651 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+ } ) ;
6160 } ) ;
62- return audiences . delete ( index ) ;
63- } ) ;
64- } ) ;
6561 case ActionTypes . ABORT_FETCH :
66- let xhrKey = action . xhrKey ;
67- if ( xhrMap [ xhrKey ] ) {
68- xhrMap [ xhrKey ] . abort ( ) ;
69- }
7062 return Parse . Promise . as ( state ) ;
7163 }
7264}
0 commit comments