From c78d79ad979ff12b09926aaf489335b60b17cb5a Mon Sep 17 00:00:00 2001 From: Lyn Nagara Date: Wed, 10 Apr 2019 18:56:02 -0700 Subject: [PATCH] feat(saved-search): Create saved search store (#12729) Update saved search store whenever a saved search is fetched. Data from this store will be used in various saved search components in future. --- .../app/actionCreators/savedSearches.jsx | 9 ++++- .../sentry/app/actions/savedSearchActions.jsx | 3 ++ .../sentry/app/stores/savedSearchStore.jsx | 37 +++++++++++++++++++ .../js/spec/stores/savedSearchStore.spec.jsx | 28 ++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/sentry/static/sentry/app/actions/savedSearchActions.jsx create mode 100644 src/sentry/static/sentry/app/stores/savedSearchStore.jsx create mode 100644 tests/js/spec/stores/savedSearchStore.spec.jsx diff --git a/src/sentry/static/sentry/app/actionCreators/savedSearches.jsx b/src/sentry/static/sentry/app/actionCreators/savedSearches.jsx index 5790caac9b8c3d..6a412811057b54 100644 --- a/src/sentry/static/sentry/app/actionCreators/savedSearches.jsx +++ b/src/sentry/static/sentry/app/actionCreators/savedSearches.jsx @@ -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/`; @@ -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) { diff --git a/src/sentry/static/sentry/app/actions/savedSearchActions.jsx b/src/sentry/static/sentry/app/actions/savedSearchActions.jsx new file mode 100644 index 00000000000000..25f374d98d8d57 --- /dev/null +++ b/src/sentry/static/sentry/app/actions/savedSearchActions.jsx @@ -0,0 +1,3 @@ +import Reflux from 'reflux'; + +export default Reflux.createActions(['loadSavedSearches', 'updateSavedSearches']); diff --git a/src/sentry/static/sentry/app/stores/savedSearchStore.jsx b/src/sentry/static/sentry/app/stores/savedSearchStore.jsx new file mode 100644 index 00000000000000..d9b59d17ff3f7a --- /dev/null +++ b/src/sentry/static/sentry/app/stores/savedSearchStore.jsx @@ -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; diff --git a/tests/js/spec/stores/savedSearchStore.spec.jsx b/tests/js/spec/stores/savedSearchStore.spec.jsx new file mode 100644 index 00000000000000..7553c841ade9ab --- /dev/null +++ b/tests/js/spec/stores/savedSearchStore.spec.jsx @@ -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); + }); +});