Browse Source

ref(js): Remove PluginActions (#37961)

Evan Purkhiser 2 years ago
parent
commit
bd85f06b59

+ 9 - 7
static/app/actionCreators/plugins.tsx

@@ -3,9 +3,9 @@ import {
   addLoadingMessage,
   addSuccessMessage,
 } from 'sentry/actionCreators/indicator';
-import PluginActions from 'sentry/actions/pluginActions';
 import {Client, RequestOptions} from 'sentry/api';
 import {t} from 'sentry/locale';
+import PluginsStore from 'sentry/stores/pluginsStore';
 import {Plugin} from 'sentry/types';
 
 const activeFetch = {};
@@ -34,7 +34,7 @@ type DoUpdateParams = Slugs & {
 } & Partial<RequestOptions>;
 
 function doUpdate({orgId, projectId, pluginId, update, ...params}: DoUpdateParams) {
-  PluginActions.update(pluginId, update);
+  PluginsStore.onUpdate(pluginId, update);
   const request = api.requestPromise(
     `/projects/${orgId}/${projectId}/plugins/${pluginId}/`,
     {
@@ -45,14 +45,14 @@ function doUpdate({orgId, projectId, pluginId, update, ...params}: DoUpdateParam
   // This is intentionally not chained because we want the unhandled promise to be returned
   request
     .then(() => {
-      PluginActions.updateSuccess(pluginId, update);
+      PluginsStore.onUpdateSuccess(pluginId, update);
     })
     .catch(resp => {
       const err =
         resp && resp.responseJSON && typeof resp.responseJSON.detail === 'string'
           ? new Error(resp.responseJSON.detail)
           : new Error('Unable to update plugin');
-      PluginActions.updateError(pluginId, update, err);
+      PluginsStore.onUpdateError(pluginId, update, err);
     });
 
   return request;
@@ -79,7 +79,7 @@ export function fetchPlugins(
     return activeFetch[path];
   }
 
-  PluginActions.fetchAll(options);
+  PluginsStore.onFetchAll(options);
   const request = api.requestPromise(path, {
     method: 'GET',
     includeAllArgs: true,
@@ -90,12 +90,14 @@ export function fetchPlugins(
   // This is intentionally not chained because we want the unhandled promise to be returned
   request
     .then(([data, _, resp]) => {
-      PluginActions.fetchAllSuccess(data, {pageLinks: resp?.getResponseHeader('Link')});
+      PluginsStore.onFetchAllSuccess(data, {
+        pageLinks: resp?.getResponseHeader('Link') ?? undefined,
+      });
 
       return data;
     })
     .catch(err => {
-      PluginActions.fetchAllError(err);
+      PluginsStore.onFetchAllError(err);
       throw new Error('Unable to fetch plugins');
     })
     .then(() => (activeFetch[path] = null));

+ 0 - 12
static/app/actions/pluginActions.tsx

@@ -1,12 +0,0 @@
-import {createActions} from 'reflux';
-
-const PluginActions = createActions([
-  'update',
-  'updateError',
-  'updateSuccess',
-  'fetchAll',
-  'fetchAllSuccess',
-  'fetchAllError',
-]);
-
-export default PluginActions;

+ 14 - 23
static/app/stores/pluginsStore.tsx

@@ -1,10 +1,8 @@
 import {createStore, StoreDefinition} from 'reflux';
 
-import PluginActions from 'sentry/actions/pluginActions';
 import {Plugin} from 'sentry/types';
-import {makeSafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
 
-interface PluginStoreDefinition extends StoreDefinition {
+interface InternalDefinition {
   plugins: Map<string, Plugin> | null;
   state: {
     error: Error | null;
@@ -15,6 +13,16 @@ interface PluginStoreDefinition extends StoreDefinition {
   updating: Map<string, Plugin>;
 }
 
+interface PluginStoreDefinition extends StoreDefinition, InternalDefinition {
+  onFetchAll: (options?: {resetLoading?: boolean}) => void;
+  onFetchAllError: (err) => void;
+  onFetchAllSuccess: (data: Plugin[], links: {pageLinks?: string}) => void;
+
+  onUpdate: (id: string, updateObj: Partial<Plugin>) => void;
+  onUpdateError: (id: string, _updateObj: Partial<Plugin>, err) => void;
+  onUpdateSuccess: (id: string, _updateObj: Partial<Plugin>) => void;
+}
+
 const defaultState = {
   loading: true,
   plugins: [],
@@ -26,7 +34,6 @@ const storeConfig: PluginStoreDefinition = {
   plugins: null,
   state: {...defaultState},
   updating: new Map(),
-  unsubscribeListeners: [],
 
   reset() {
     // reset our state
@@ -51,29 +58,13 @@ const storeConfig: PluginStoreDefinition = {
 
   init() {
     this.reset();
-    this.unsubscribeListeners.push(
-      this.listenTo(PluginActions.fetchAll, this.onFetchAll)
-    );
-    this.unsubscribeListeners.push(
-      this.listenTo(PluginActions.fetchAllSuccess, this.onFetchAllSuccess)
-    );
-    this.unsubscribeListeners.push(
-      this.listenTo(PluginActions.fetchAllError, this.onFetchAllError)
-    );
-    this.unsubscribeListeners.push(this.listenTo(PluginActions.update, this.onUpdate));
-    this.unsubscribeListeners.push(
-      this.listenTo(PluginActions.updateSuccess, this.onUpdateSuccess)
-    );
-    this.unsubscribeListeners.push(
-      this.listenTo(PluginActions.updateError, this.onUpdateError)
-    );
   },
 
   triggerState() {
     this.trigger(this.getState());
   },
 
-  onFetchAll({resetLoading}: {resetLoading?: boolean} = {}) {
+  onFetchAll({resetLoading} = {}) {
     if (resetLoading) {
       this.state.loading = true;
       this.state.error = null;
@@ -83,7 +74,7 @@ const storeConfig: PluginStoreDefinition = {
     this.triggerState();
   },
 
-  onFetchAllSuccess(data: Plugin[], {pageLinks}: {pageLinks?: string}) {
+  onFetchAllSuccess(data, {pageLinks}) {
     this.plugins = new Map(data.map(plugin => [plugin.id, plugin]));
     this.state.pageLinks = pageLinks || null;
     this.state.loading = false;
@@ -133,5 +124,5 @@ const storeConfig: PluginStoreDefinition = {
   },
 };
 
-const PluginStore = createStore(makeSafeRefluxStore(storeConfig));
+const PluginStore = createStore(storeConfig);
 export default PluginStore;

+ 13 - 14
tests/js/spec/stores/pluginsStore.spec.jsx

@@ -1,4 +1,3 @@
-import PluginActions from 'sentry/actions/pluginActions';
 import PluginsStore from 'sentry/stores/pluginsStore';
 
 describe('PluginsStore', function () {
@@ -27,7 +26,7 @@ describe('PluginsStore', function () {
     });
 
     it('has correct state when all plugins fetched successfully', function () {
-      PluginActions.fetchAll.trigger();
+      PluginsStore.onFetchAll();
       expect(PluginsStore.trigger).toHaveBeenCalledWith({
         loading: true,
         error: null,
@@ -35,7 +34,7 @@ describe('PluginsStore', function () {
         plugins: [],
       });
 
-      PluginActions.fetchAllSuccess.trigger(TestStubs.Plugins(), {pageLinks: null});
+      PluginsStore.onFetchAllSuccess(TestStubs.Plugins(), {pageLinks: null});
 
       expect(PluginsStore.trigger).toHaveBeenCalledWith({
         loading: false,
@@ -46,7 +45,7 @@ describe('PluginsStore', function () {
     });
 
     it('has correct state when error in fetching all plugins', function () {
-      PluginActions.fetchAll.trigger();
+      PluginsStore.onFetchAll();
 
       expect(PluginsStore.trigger).toHaveBeenCalledWith({
         loading: true,
@@ -55,7 +54,7 @@ describe('PluginsStore', function () {
         plugins: [],
       });
 
-      PluginActions.fetchAllError.trigger({responseJSON: {message: 'Error'}});
+      PluginsStore.onFetchAllError({responseJSON: {message: 'Error'}});
 
       expect(PluginsStore.trigger).toHaveBeenCalledWith({
         loading: false,
@@ -66,7 +65,7 @@ describe('PluginsStore', function () {
     });
 
     it('does not reset loading state on consecutive fetches', function () {
-      PluginActions.fetchAll.trigger();
+      PluginsStore.onFetchAll();
       expect(PluginsStore.trigger).toHaveBeenCalledWith({
         loading: true,
         error: null,
@@ -74,7 +73,7 @@ describe('PluginsStore', function () {
         plugins: [],
       });
 
-      PluginActions.fetchAllSuccess.trigger(TestStubs.Plugins(), {pageLinks: null});
+      PluginsStore.onFetchAllSuccess(TestStubs.Plugins(), {pageLinks: null});
 
       expect(PluginsStore.trigger).toHaveBeenCalledWith({
         loading: false,
@@ -83,7 +82,7 @@ describe('PluginsStore', function () {
         plugins: TestStubs.Plugins(),
       });
 
-      PluginActions.fetchAll.trigger();
+      PluginsStore.onFetchAll();
       expect(PluginsStore.trigger).toHaveBeenCalledWith({
         loading: false,
         error: null,
@@ -101,7 +100,7 @@ describe('PluginsStore', function () {
     });
 
     it('has optimistic state when updating', function () {
-      PluginActions.update.trigger('amazon-sqs', {name: 'Amazon Sqs'});
+      PluginsStore.onUpdate('amazon-sqs', {name: 'Amazon Sqs'});
 
       const state = PluginsStore.getState();
       expect(state).toMatchObject({
@@ -123,7 +122,7 @@ describe('PluginsStore', function () {
     });
 
     it('saves old plugin state', function () {
-      PluginActions.update.trigger('amazon-sqs', {name: 'Amazon Sqs'});
+      PluginsStore.onUpdate('amazon-sqs', {name: 'Amazon Sqs'});
 
       const state = PluginsStore.getState();
       expect(state).toMatchObject({
@@ -139,7 +138,7 @@ describe('PluginsStore', function () {
     });
 
     it('removes old plugin state on successful update', function () {
-      PluginActions.update.trigger('amazon-sqs', {name: 'Amazon Sqs'});
+      PluginsStore.onUpdate('amazon-sqs', {name: 'Amazon Sqs'});
 
       expect(PluginsStore.updating.get('amazon-sqs')).toMatchObject({
         ...plugin,
@@ -147,7 +146,7 @@ describe('PluginsStore', function () {
         name: 'Amazon SQS',
       });
 
-      PluginActions.updateSuccess.trigger('amazon-sqs');
+      PluginsStore.onUpdateSuccess('amazon-sqs');
 
       expect(PluginsStore.getState().plugins[0]).toMatchObject({
         id: 'amazon-sqs',
@@ -158,14 +157,14 @@ describe('PluginsStore', function () {
     });
 
     it('restores old plugin state when update has an error', function () {
-      PluginActions.update.trigger('amazon-sqs', {name: 'Amazon Sqs'});
+      PluginsStore.onUpdate('amazon-sqs', {name: 'Amazon Sqs'});
 
       expect(PluginsStore.getState().plugins[0]).toMatchObject({
         id: 'amazon-sqs',
         name: 'Amazon Sqs',
       });
 
-      PluginActions.updateError.trigger('amazon-sqs');
+      PluginsStore.onUpdateError('amazon-sqs');
 
       expect(PluginsStore.getState().plugins[0]).toMatchObject({
         id: 'amazon-sqs',