modalStore.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import Reflux from 'reflux';
  2. import {ModalOptions, ModalRenderProps} from 'sentry/actionCreators/modal';
  3. import ModalActions from 'sentry/actions/modalActions';
  4. type Renderer = (renderProps: ModalRenderProps) => React.ReactNode;
  5. type ModalStoreState = {
  6. options: ModalOptions;
  7. renderer: Renderer | null;
  8. };
  9. type ModalStoreInterface = {
  10. get(): ModalStoreState;
  11. init(): void;
  12. onCloseModal(): void;
  13. onOpenModal(renderer: Renderer, options: ModalOptions): void;
  14. reset(): void;
  15. };
  16. const storeConfig: Reflux.StoreDefinition & ModalStoreInterface = {
  17. init() {
  18. this.reset();
  19. this.listenTo(ModalActions.closeModal, this.onCloseModal);
  20. this.listenTo(ModalActions.openModal, this.onOpenModal);
  21. },
  22. get() {
  23. return this.state;
  24. },
  25. reset() {
  26. this.state = {
  27. renderer: null,
  28. options: {},
  29. } as ModalStoreState;
  30. },
  31. onCloseModal() {
  32. this.reset();
  33. this.trigger(this.state);
  34. },
  35. onOpenModal(renderer: Renderer, options: ModalOptions) {
  36. this.state = {renderer, options};
  37. this.trigger(this.state);
  38. },
  39. };
  40. const ModalStore = Reflux.createStore(storeConfig) as Reflux.Store & ModalStoreInterface;
  41. export default ModalStore;