indicatorStore.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import Reflux from 'reflux';
  2. import {Indicator} from 'app/actionCreators/indicator';
  3. import IndicatorActions from 'app/actions/indicatorActions';
  4. import {t} from 'app/locale';
  5. type IndicatorStoreInterface = {
  6. init(): void;
  7. get(): Indicator[];
  8. addSuccess(message: string): Indicator;
  9. addError(message?: string): Indicator;
  10. /**
  11. * Appends a message to be displayed in list of indicators
  12. *
  13. * @param message Toast message to be displayed
  14. * @param type One of ['error', 'success', '']
  15. * @param options Options object
  16. */
  17. append(
  18. message: string,
  19. type: Indicator['type'],
  20. options?: Indicator['options']
  21. ): Indicator;
  22. /**
  23. * When this method is called directly via older parts of the application,
  24. * we want to maintain the old behavior in that it is replaced (and not queued up)
  25. *
  26. * @param message Toast message to be displayed
  27. * @param type One of ['error', 'success', '']
  28. * @param options Options object
  29. */
  30. add(
  31. message: string,
  32. type?: Indicator['type'],
  33. options?: Indicator['options']
  34. ): Indicator;
  35. /**
  36. * Alias for add()
  37. */
  38. addMessage(
  39. message: string,
  40. type: Indicator['type'],
  41. options?: Indicator['options']
  42. ): Indicator;
  43. /**
  44. * Remove all current indicators.
  45. */
  46. clear(): void;
  47. /**
  48. * Remove an indicator
  49. */
  50. remove(indicator: Indicator): void;
  51. };
  52. type Internals = {
  53. items: any[];
  54. lastId: number;
  55. };
  56. const storeConfig: Reflux.StoreDefinition & Internals & IndicatorStoreInterface = {
  57. items: [],
  58. lastId: 0,
  59. init() {
  60. this.items = [];
  61. this.lastId = 0;
  62. this.listenTo(IndicatorActions.append, this.append);
  63. this.listenTo(IndicatorActions.replace, this.add);
  64. this.listenTo(IndicatorActions.remove, this.remove);
  65. this.listenTo(IndicatorActions.clear, this.clear);
  66. },
  67. get() {
  68. return this.items;
  69. },
  70. addSuccess(message) {
  71. return this.add(message, 'success', {duration: 2000});
  72. },
  73. addError(message = t('An error occurred')) {
  74. return this.add(message, 'error', {duration: 2000});
  75. },
  76. addMessage(message, type, {append, ...options} = {}) {
  77. const indicator: Indicator = {
  78. id: this.lastId++,
  79. message,
  80. type,
  81. options,
  82. clearId: null,
  83. };
  84. if (options.duration) {
  85. indicator.clearId = window.setTimeout(() => {
  86. this.remove(indicator);
  87. }, options.duration);
  88. }
  89. const newItems = append ? [...this.items, indicator] : [indicator];
  90. this.items = newItems;
  91. this.trigger(this.items);
  92. return indicator;
  93. },
  94. append(message, type, options) {
  95. return this.addMessage(message, type, {
  96. ...options,
  97. append: true,
  98. });
  99. },
  100. add(message, type = 'loading', options = {}) {
  101. return this.addMessage(message, type, {
  102. ...options,
  103. append: false,
  104. });
  105. },
  106. clear() {
  107. this.items = [];
  108. this.trigger(this.items);
  109. },
  110. remove(indicator) {
  111. if (!indicator) {
  112. return;
  113. }
  114. this.items = this.items.filter(item => item !== indicator);
  115. if (indicator.clearId) {
  116. window.clearTimeout(indicator.clearId);
  117. indicator.clearId = null;
  118. }
  119. this.trigger(this.items);
  120. },
  121. };
  122. const IndicatorStore = Reflux.createStore(storeConfig) as Reflux.Store &
  123. IndicatorStoreInterface;
  124. export default IndicatorStore;