Skip to content

Enabling audiences UI #712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* _Contributing to this repo? Add info about your change here to be included in next release_
* Feature: When editing Object or Array fields the data is displayed in a prettier format and the textarea is resizable
* Fix: Display bug on safari when table has empty cells ('')
* Feature: UI for managing push audiences, thanks to [Davi Macedo](https://github.com/davimacedo)

### 1.0.28
* Feature: Add ability to search Object columns (#727), thanks to [Samuli Siivinen](https://github.com/ssamuli)
Expand Down
56 changes: 24 additions & 32 deletions src/lib/stores/PushAudiencesStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import { abortableGet, post, del } from 'lib/AJAX';
import keyMirror from 'lib/keyMirror';
import Parse from 'parse';
import { List, Map } from 'immutable';
Expand All @@ -20,53 +19,46 @@ const LASTFETCHTIMEOUT = 60000;
// - showMore: Flag to show/hide button to fetch all audiences

// xhr map, key value pair of xhrKey, xhr reference
let xhrMap = {};

function PushAudiencesStore(state, action) {
action.app.setParseKeys();
let urlPrefix = `/apps/${action.app.slug}/dashboard_ajax/push_audiences`;
let legacyUrlPrefix = `/apps/${action.app.slug}/push_audiences`;
switch (action.type) {
case ActionTypes.FETCH:
if (state && new Date() - state.get('lastFetch') < LASTFETCHTIMEOUT) { //check for stale store
if (state.get('audiences') && state.get('audiences').size >= (action.min || 0)) { //check for valid audience size
return Parse.Promise.as(state);
}
}
let {xhr, promise} = abortableGet(action.limit ? urlPrefix + `?audience_limit=${action.limit}` : urlPrefix, action.xhrKey !== null);
xhrMap[action.xhrKey] = xhr;
return promise.then(({ audiences, showMore }) => {
return Map({ lastFetch: new Date(), audiences: List(audiences) , showMore: showMore});
const path = action.limit ? `push_audiences?audience_limit=${action.limit}` : 'push_audiences';
const promise = Parse._request('GET', path, {}, { useMasterKey: true });

return promise.then(({ results, showMore }) => {
return Map({ lastFetch: new Date(), audiences: List(results), showMore: showMore});
});
case ActionTypes.CREATE:
return post(legacyUrlPrefix, {
query: action.query,
name: action.name,
}).then(({ new_audience }) => {
return state.update('audiences',(audiences) => {
return audiences.unshift({
createdAt: new Date(),
name: action.name,
objectId: new_audience ? new_audience.objectId || -1 : -1,
count: 0,
query: JSON.parse(action.query),
return Parse._request('POST', 'push_audiences', { query: action.query, name: action.name, }, { useMasterKey: true })
.then(({ new_audience }) => {
return state.update('audiences',(audiences) => {
return audiences.unshift({
createdAt: new Date(),
name: action.name,
objectId: new_audience ? new_audience.objectId || -1 : -1,
count: 0,
query: JSON.parse(action.query),
});
});
});
});
});
case ActionTypes.DESTROY:
return del(legacyUrlPrefix + `/${action.objectId}`).then(() => {
return state.update('audiences',(audiences) => {
let index = audiences.findIndex(function(audience) {
return audience.objectId === action.objectId;
return Parse._request('DELETE', `push_audiences/${action.objectId}`, {}, { useMasterKey: true })
.then(() => {
return state.update('audiences',(audiences) => {
let index = audiences.findIndex(function(audience) {
return audience.objectId === action.objectId;
});
return audiences.delete(index);
});
});
return audiences.delete(index);
});
});
case ActionTypes.ABORT_FETCH:
let xhrKey = action.xhrKey;
if (xhrMap[xhrKey]) {
xhrMap[xhrKey].abort();
}
return Parse.Promise.as(state);
}
}
Expand Down