import * as types from './types';

import devcms from '@devcms/sdk';

const siteSuccess = site => ({
  type: types.SITE_SUCCESS,
  site: site,
});

const siteError = error => ({
  type: types.SITE_ERROR,
  error: error
});

const getSite = () => {
  return (dispatch) => devcms.admin.site.get()
    .then(site => dispatch(siteSuccess(site)))
    .catch(error => dispatch(siteError(error)));
};

const install = ({ site_name, email, first_name, last_name, password }) => {
  return (dispatch) => devcms.admin.site.create({
    site_name, email, first_name, last_name, password
  }).then(site => dispatch(siteSuccess(site.installed)))
    .catch(error => dispatch(siteError(error)));
};

export { getSite, install };
