Browse Source

ref(js): Remove SavedSearchActions (#38110)

Evan Purkhiser 2 years ago
parent
commit
22e37128a5

+ 12 - 12
static/app/actionCreators/savedSearches.tsx

@@ -1,18 +1,18 @@
 import {addErrorMessage} from 'sentry/actionCreators/indicator';
-import SavedSearchesActions from 'sentry/actions/savedSearchesActions';
 import {Client} from 'sentry/api';
 import {MAX_AUTOCOMPLETE_RECENT_SEARCHES} from 'sentry/constants';
 import {t} from 'sentry/locale';
+import SavedSearchesStore from 'sentry/stores/savedSearchesStore';
 import {RecentSearch, SavedSearch, SavedSearchType} from 'sentry/types';
 import handleXhrErrorResponse from 'sentry/utils/handleXhrErrorResponse';
 
 export function resetSavedSearches() {
-  SavedSearchesActions.resetSavedSearches();
+  SavedSearchesStore.onReset();
 }
 
 export function fetchSavedSearches(api: Client, orgSlug: string): Promise<SavedSearch[]> {
   const url = `/organizations/${orgSlug}/searches/`;
-  SavedSearchesActions.startFetchSavedSearches();
+  SavedSearchesStore.onStartFetchSavedSearches();
 
   const promise = api.requestPromise(url, {
     method: 'GET',
@@ -20,10 +20,10 @@ export function fetchSavedSearches(api: Client, orgSlug: string): Promise<SavedS
 
   promise
     .then(resp => {
-      SavedSearchesActions.fetchSavedSearchesSuccess(resp);
+      SavedSearchesStore.onFetchSavedSearchesSuccess(resp);
     })
     .catch(err => {
-      SavedSearchesActions.fetchSavedSearchesError(err);
+      SavedSearchesStore.onFetchSavedSearchesError(err);
       addErrorMessage(t('Unable to load saved searches'));
     });
 
@@ -100,7 +100,7 @@ export function createSavedSearch(
   // Need to wait for saved search to save unfortunately because we need to redirect
   // to saved search URL
   promise.then(resp => {
-    SavedSearchesActions.createSavedSearchSuccess(resp);
+    SavedSearchesStore.onCreateSavedSearchSuccess(resp);
   });
 
   return promise;
@@ -153,7 +153,7 @@ export function pinSearch(
   const url = getPinSearchUrl(orgSlug);
 
   // Optimistically update store
-  SavedSearchesActions.pinSearch(type, query, sort);
+  SavedSearchesStore.onPinSearch(type, query, sort);
 
   const promise = api.requestPromise(url, {
     method: 'PUT',
@@ -164,12 +164,12 @@ export function pinSearch(
     },
   });
 
-  promise.then(SavedSearchesActions.pinSearchSuccess);
+  promise.then(SavedSearchesStore.onPinSearchSuccess);
 
   promise.catch(handleXhrErrorResponse('Unable to pin search'));
 
   promise.catch(() => {
-    SavedSearchesActions.unpinSearch(type);
+    SavedSearchesStore.onUnpinSearch(type);
   });
 
   return promise;
@@ -184,7 +184,7 @@ export function unpinSearch(
   const url = getPinSearchUrl(orgSlug);
 
   // Optimistically update store
-  SavedSearchesActions.unpinSearch(type);
+  SavedSearchesStore.onUnpinSearch(type);
 
   const promise = api.requestPromise(url, {
     method: 'DELETE',
@@ -197,7 +197,7 @@ export function unpinSearch(
 
   promise.catch(() => {
     const {type: pinnedType, query} = pinnedSearch;
-    SavedSearchesActions.pinSearch(pinnedType, query);
+    SavedSearchesStore.onPinSearch(pinnedType, query);
   });
 
   return promise;
@@ -221,7 +221,7 @@ export function deleteSavedSearch(
     .requestPromise(url, {
       method: 'DELETE',
     })
-    .then(() => SavedSearchesActions.deleteSavedSearchSuccess(search))
+    .then(() => SavedSearchesStore.onDeleteSavedSearchSuccess(search))
     .catch(handleXhrErrorResponse('Unable to delete a saved search'));
 
   return promise;

+ 0 - 15
static/app/actions/savedSearchesActions.tsx

@@ -1,15 +0,0 @@
-import {createActions} from 'reflux';
-
-const SavedSearchActions = createActions([
-  'resetSavedSearches',
-  'startFetchSavedSearches',
-  'fetchSavedSearchesSuccess',
-  'fetchSavedSearchesError',
-  'createSavedSearchSuccess',
-  'deleteSavedSearchSuccess',
-  'pinSearch',
-  'pinSearchSuccess',
-  'unpinSearch',
-]);
-
-export default SavedSearchActions;

+ 18 - 42
static/app/stores/savedSearchesStore.tsx

@@ -1,7 +1,6 @@
 import findIndex from 'lodash/findIndex';
 import {createStore, StoreDefinition} from 'reflux';
 
-import SavedSearchesActions from 'sentry/actions/savedSearchesActions';
 import {SavedSearch, SavedSearchType} from 'sentry/types';
 import {makeSafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
 
@@ -12,10 +11,21 @@ type State = {
 };
 
 interface SavedSearchesStoreDefinition extends StoreDefinition {
-  findByQuery(query: string, sort: string): SavedSearch | undefined;
+  findByQuery(query: string, sort?: string): SavedSearch | undefined;
   get(): State;
   getFilteredSearches(type: SavedSearchType, id?: string): SavedSearch[];
-  onPinSearch(type: SavedSearchType, query: string, sort: string): void;
+
+  onCreateSavedSearchSuccess: (data) => void;
+  onDeleteSavedSearchSuccess: (search: SavedSearch) => void;
+  onFetchSavedSearchesError: (data) => void;
+
+  onFetchSavedSearchesSuccess: (data) => void;
+  onPinSearch(type: SavedSearchType, query: string, sort?: string): void;
+  onPinSearchSuccess: (data) => void;
+  onReset: () => void;
+  onStartFetchSavedSearches: () => void;
+  onUnpinSearch: (type: SavedSearchType) => void;
+
   reset(): void;
   updateExistingSearch(id: string, changes: Partial<SavedSearch>): SavedSearch;
 }
@@ -30,40 +40,6 @@ const storeConfig: SavedSearchesStoreDefinition = {
   },
 
   init() {
-    const {
-      startFetchSavedSearches,
-      fetchSavedSearchesSuccess,
-      fetchSavedSearchesError,
-      createSavedSearchSuccess,
-      deleteSavedSearchSuccess,
-      pinSearch,
-      pinSearchSuccess,
-      resetSavedSearches,
-      unpinSearch,
-    } = SavedSearchesActions;
-
-    this.unsubscribeListeners.push(
-      this.listenTo(startFetchSavedSearches, this.onStartFetchSavedSearches)
-    );
-    this.unsubscribeListeners.push(
-      this.listenTo(fetchSavedSearchesSuccess, this.onFetchSavedSearchesSuccess)
-    );
-    this.unsubscribeListeners.push(
-      this.listenTo(fetchSavedSearchesError, this.onFetchSavedSearchesError)
-    );
-    this.unsubscribeListeners.push(this.listenTo(resetSavedSearches, this.onReset));
-    this.unsubscribeListeners.push(
-      this.listenTo(createSavedSearchSuccess, this.onCreateSavedSearchSuccess)
-    );
-    this.unsubscribeListeners.push(
-      this.listenTo(deleteSavedSearchSuccess, this.onDeleteSavedSearchSuccess)
-    );
-    this.unsubscribeListeners.push(this.listenTo(pinSearch, this.onPinSearch));
-    this.unsubscribeListeners.push(
-      this.listenTo(pinSearchSuccess, this.onPinSearchSuccess)
-    );
-    this.unsubscribeListeners.push(this.listenTo(unpinSearch, this.onUnpinSearch));
-
     this.reset();
   },
 
@@ -174,10 +150,10 @@ const storeConfig: SavedSearchesStoreDefinition = {
     this.trigger(this.state);
   },
 
-  onCreateSavedSearchSuccess(resp) {
+  onCreateSavedSearchSuccess(data) {
     this.state = {
       ...this.state,
-      savedSearches: [...this.state.savedSearches, resp],
+      savedSearches: [...this.state.savedSearches, data],
     };
 
     this.trigger(this.state);
@@ -225,11 +201,11 @@ const storeConfig: SavedSearchesStoreDefinition = {
     this.trigger(this.state);
   },
 
-  onPinSearchSuccess(resp) {
-    const existingSearch = this.findByQuery(resp.query, resp.sort);
+  onPinSearchSuccess(data) {
+    const existingSearch = this.findByQuery(data.query, data.sort);
 
     if (existingSearch) {
-      this.updateExistingSearch(existingSearch.id, resp);
+      this.updateExistingSearch(existingSearch.id, data);
     }
 
     this.trigger(this.state);