Skip to content

Commit

Permalink
feat(saved-search): Create saved search store (#12729)
Browse files Browse the repository at this point in the history
Update saved search store whenever a saved search is fetched. Data from
this store will be used in various saved search components in future.
  • Loading branch information
lynnagara authored Apr 11, 2019
1 parent dce7710 commit c78d79a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {MAX_RECENT_SEARCHES, SEARCH_TYPES} from 'app/constants';
import handleXhrErrorResponse from 'app/utils/handleXhrErrorResponse';
import SavedSearchActions from 'app/actions/savedSearchActions';

export function fetchSavedSearches(api, orgId, useOrgSavedSearches = false) {
const url = `/organizations/${orgId}/searches/`;
Expand All @@ -9,10 +10,16 @@ export function fetchSavedSearches(api, orgId, useOrgSavedSearches = false) {
data.use_org_level = '1';
}

return api.requestPromise(url, {
const promise = api.requestPromise(url, {
method: 'GET',
data,
});

promise.then(res => {
SavedSearchActions.updateSavedSearches(res);
});

return promise;
}

export function fetchProjectSavedSearches(api, orgId, projectId) {
Expand Down
3 changes: 3 additions & 0 deletions src/sentry/static/sentry/app/actions/savedSearchActions.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Reflux from 'reflux';

export default Reflux.createActions(['loadSavedSearches', 'updateSavedSearches']);
37 changes: 37 additions & 0 deletions src/sentry/static/sentry/app/stores/savedSearchStore.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Reflux from 'reflux';

import SavedSearchActions from 'app/actions/savedSearchActions';

const SavedSearchesStore = Reflux.createStore({
init() {
this.reset();
this.listenTo(SavedSearchActions.loadSavedSearches, this.onLoadSavedSearches);
this.listenTo(SavedSearchActions.updateSavedSearches, this.onUpdateSavedSearches);
},

reset() {
this.state = {
isLoading: true,
savedSearches: [],
};
},

get() {
return this.state;
},

onLoadSavedSearches() {
this.reset();
this.trigger(this.state);
},

onUpdateSavedSearches(savedSearches) {
this.state = {
isLoading: false,
savedSearches,
};
this.trigger(this.state);
},
});

export default SavedSearchesStore;
28 changes: 28 additions & 0 deletions tests/js/spec/stores/savedSearchStore.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import SavedSearchStore from 'app/stores/savedSearchStore';
import {fetchSavedSearches} from 'app/actionCreators/savedSearches';
import {Client} from 'app/api';

describe('SavedSearchStore', function() {
beforeEach(function() {
Client.addMockResponse({
url: '/organizations/org-1/searches/',
body: TestStubs.Searches(),
});
});

afterEach(function() {
Client.clearMockResponses();
});

it('get', function() {
expect(SavedSearchStore.get()).toEqual({isLoading: true, savedSearches: []});
});

it('fetching saved searches updates store', async function() {
const api = new Client();
fetchSavedSearches(api, 'org-1');
await tick();
expect(SavedSearchStore.get().savedSearches).toHaveLength(2);
expect(SavedSearchStore.get().isLoading).toBe(false);
});
});

0 comments on commit c78d79a

Please sign in to comment.