project.js 1.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
import service from '../../services';
import flash from '../../../flash';
import * as types from '../mutation_types';

// eslint-disable-next-line import/prefer-default-export
export const getProjectData = (
  { commit, state, dispatch },
  { namespace, projectId, force = false } = {},
) => new Promise((resolve, reject) => {
  if (!state.projects[`${namespace}/${projectId}`] || force) {
11
    commit(types.TOGGLE_LOADING, state);
12 13 14
    service.getProjectData(namespace, projectId)
    .then(res => res.data)
    .then((data) => {
15
      commit(types.TOGGLE_LOADING, state);
16 17 18 19 20 21 22 23 24 25 26 27
      commit(types.SET_PROJECT, { projectPath: `${namespace}/${projectId}`, project: data });
      if (!state.currentProjectId) commit(types.SET_CURRENT_PROJECT, `${namespace}/${projectId}`);
      resolve(data);
    })
    .catch(() => {
      flash('Error loading project data. Please try again.');
      reject(new Error(`Project not loaded ${namespace}/${projectId}`));
    });
  } else {
    resolve(state.projects[`${namespace}/${projectId}`]);
  }
});