# All files / composer/__legacy-buffer-web-shared-components__/modal store.js

48.28% Statements 14/29
33.33% Branches 1/3
30% Functions 3/10
46.43% Lines 13/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57

    1x

    2x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x
    9x

    9x

    1x
     import { EventEmitter } from 'events';
    import AppDispatcher from '../../dispatcher';
    import ActionTypes from './actionTypes';

    const CHANGE_EVENT = 'change';

    const getInitialState = () => ({
      open: true,
      modalName: '',
      modalProps: {}
    });

    const state = getInitialState();

    const Store = Object.assign({}, EventEmitter.prototype, {
      emitChange: () => Store.emit(CHANGE_EVENT),
      addChangeListener: (callback) => Store.on(CHANGE_EVENT, callback),
      removeChangeListener: (callback) => Store.removeListener(CHANGE_EVENT, callback),
      getCurrentState: () => state,
      getInitialState
    });

    const closeModal = () => {
      state.open = false;
    };

    const openModal = () => {
      state.open = true;
    };

    const setModal = (modalName) => {
      state.modalName = modalName;
    };

    const setModalProps = (modalProps) => {
      state.modalProps = modalProps;
    };

    AppDispatcher.register((payload) => {
      switch (payload.action.actionType) {
        case ActionTypes.CLOSE_MODAL:
          closeModal();
          Store.emitChange();
          break;
        case ActionTypes.OPEN_MODAL:
          setModal(payload.action.name);
          setModalProps(payload.action.props);
          openModal();
          Store.emitChange();
          break;
        default:
          break;
      }
    });

    module.exports = Store;