pluginsStore.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import Reflux from 'reflux';
  2. import PluginActions from 'sentry/actions/pluginActions';
  3. import {Plugin} from 'sentry/types';
  4. type PluginStoreInterface = {
  5. plugins: Map<string, Plugin> | null;
  6. state: {
  7. error: Error | null;
  8. loading: boolean;
  9. pageLinks: string | null;
  10. plugins: Plugin[];
  11. };
  12. updating: Map<string, Plugin>;
  13. };
  14. const defaultState = {
  15. loading: true,
  16. plugins: [],
  17. error: null,
  18. pageLinks: null,
  19. };
  20. const storeConfig: Reflux.StoreDefinition & PluginStoreInterface = {
  21. plugins: null,
  22. state: {...defaultState},
  23. updating: new Map(),
  24. reset() {
  25. // reset our state
  26. this.plugins = null;
  27. this.state = {...defaultState};
  28. this.updating = new Map();
  29. return this.state;
  30. },
  31. getInitialState() {
  32. return this.getState();
  33. },
  34. getState() {
  35. const {plugins: _plugins, ...state} = this.state;
  36. return {
  37. ...state,
  38. plugins: this.plugins ? Array.from(this.plugins.values()) : [],
  39. };
  40. },
  41. init() {
  42. this.reset();
  43. this.listenTo(PluginActions.fetchAll, this.onFetchAll);
  44. this.listenTo(PluginActions.fetchAllSuccess, this.onFetchAllSuccess);
  45. this.listenTo(PluginActions.fetchAllError, this.onFetchAllError);
  46. this.listenTo(PluginActions.update, this.onUpdate);
  47. this.listenTo(PluginActions.updateSuccess, this.onUpdateSuccess);
  48. this.listenTo(PluginActions.updateError, this.onUpdateError);
  49. },
  50. triggerState() {
  51. this.trigger(this.getState());
  52. },
  53. onFetchAll({resetLoading}: {resetLoading?: boolean} = {}) {
  54. if (resetLoading) {
  55. this.state.loading = true;
  56. this.state.error = null;
  57. this.plugins = null;
  58. }
  59. this.triggerState();
  60. },
  61. onFetchAllSuccess(data: Plugin[], {pageLinks}: {pageLinks?: string}) {
  62. this.plugins = new Map(data.map(plugin => [plugin.id, plugin]));
  63. this.state.pageLinks = pageLinks || null;
  64. this.state.loading = false;
  65. this.triggerState();
  66. },
  67. onFetchAllError(err) {
  68. this.plugins = null;
  69. this.state.loading = false;
  70. this.state.error = err;
  71. this.triggerState();
  72. },
  73. onUpdate(id: string, updateObj: Partial<Plugin>) {
  74. if (!this.plugins) {
  75. return;
  76. }
  77. const plugin = this.plugins.get(id);
  78. if (!plugin) {
  79. return;
  80. }
  81. const newPlugin = {
  82. ...plugin,
  83. ...updateObj,
  84. };
  85. this.plugins.set(id, newPlugin);
  86. this.updating.set(id, plugin);
  87. this.triggerState();
  88. },
  89. onUpdateSuccess(id: string, _updateObj: Partial<Plugin>) {
  90. this.updating.delete(id);
  91. },
  92. onUpdateError(id: string, _updateObj: Partial<Plugin>, err) {
  93. const origPlugin = this.updating.get(id);
  94. if (!origPlugin || !this.plugins) {
  95. return;
  96. }
  97. this.plugins.set(id, origPlugin);
  98. this.updating.delete(id);
  99. this.state.error = err;
  100. this.triggerState();
  101. },
  102. };
  103. const PluginStore = Reflux.createStore(storeConfig) as Reflux.Store &
  104. PluginStoreInterface;
  105. export default PluginStore;