2016-08-15 21:06:12 +00:00
|
|
|
import axios from 'axios';
|
2017-02-22 19:29:35 +00:00
|
|
|
import * as ActionTypes from '../../../constants';
|
2017-01-24 20:29:25 +00:00
|
|
|
import { showErrorModal, setPreviousPath } from './ide';
|
2017-01-18 21:48:16 +00:00
|
|
|
import { resetProject } from './project';
|
2016-08-15 21:06:12 +00:00
|
|
|
|
|
|
|
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
|
|
|
|
|
2016-08-17 19:53:25 +00:00
|
|
|
export function getProjects(username) {
|
2016-08-15 21:06:12 +00:00
|
|
|
return (dispatch) => {
|
2016-08-17 19:53:25 +00:00
|
|
|
let url;
|
|
|
|
if (username) {
|
|
|
|
url = `${ROOT_URL}/${username}/projects`;
|
|
|
|
} else {
|
|
|
|
url = `${ROOT_URL}/projects`;
|
|
|
|
}
|
|
|
|
axios.get(url, { withCredentials: true })
|
2017-02-22 19:29:35 +00:00
|
|
|
.then((response) => {
|
2016-08-15 21:06:12 +00:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.SET_PROJECTS,
|
|
|
|
projects: response.data
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(response => dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-12 18:24:53 +00:00
|
|
|
export function deleteProject(id) {
|
2017-01-18 21:48:16 +00:00
|
|
|
return (dispatch, getState) => {
|
2016-10-12 18:24:53 +00:00
|
|
|
axios.delete(`${ROOT_URL}/projects/${id}`, { withCredentials: true })
|
|
|
|
.then(() => {
|
2017-01-18 21:48:16 +00:00
|
|
|
const state = getState();
|
|
|
|
if (id === state.project.id) {
|
|
|
|
dispatch(resetProject());
|
|
|
|
dispatch(setPreviousPath('/'));
|
|
|
|
}
|
2016-10-12 18:24:53 +00:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.DELETE_PROJECT,
|
|
|
|
id
|
|
|
|
});
|
2017-01-13 22:32:06 +00:00
|
|
|
})
|
2017-02-22 19:29:35 +00:00
|
|
|
.catch((response) => {
|
2017-01-13 22:32:06 +00:00
|
|
|
if (response.status === 403) {
|
2017-01-24 20:29:25 +00:00
|
|
|
dispatch(showErrorModal('staleSession'));
|
2017-01-13 22:32:06 +00:00
|
|
|
} else {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
});
|
|
|
|
}
|
2016-10-12 18:24:53 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|