{"size": 1119, "ext": "js", "max_stars_repo_path": "libraries/common/actions/modal/showModal.js", "max_stars_repo_name": "aaron-martin/pwa", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "libraries/common/actions/modal/showModal.js", "max_issues_repo_name": "aaron-martin/pwa", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "libraries/common/actions/modal/showModal.js", "max_forks_repo_name": "aaron-martin/pwa", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 20.7222222222, "max_line_length": 81, "alphanum_fraction": 0.6505808758}
import { createModal } from '../../action-creators/modal';
import promiseMap from './promiseMap';

let modalCount = 0;

/**
 * Creates a runtime unique modal id.
 * @returns {number}
 */
const getModalId = () => {
  modalCount += 1;
  return modalCount;
};

/**
 * The modal defaults.
 * @type {Object}
 */
const defaultModalOptions = {
  id: null,
  type: null, // In this case the template should display a fallback dialog type.
  title: '',
  confirm: 'modal.confirm',
  dismiss: 'modal.dismiss',
  params: {}, // Any parameters for special modal types go here.
};

/**
 * Dispatches the createModal action creator and returns
 * a promise that will be resolved when the modal closes.
 * @param {Object} options The modal options.
 * @return {Function} A Redux thunk.
 */
const showModal = options => (dispatch) => {
  const id = getModalId();

  const enrichedOptions = {
    ...defaultModalOptions,
    ...options,
    id,
  };

  dispatch(createModal(enrichedOptions));

  return new Promise((resolve, reject) => {
    promiseMap.set(id, {
      resolve,
      reject,
    });
  });
};

export default showModal;
