p5.js-web-editor/client/modules/IDE/actions/project.js

89 lines
2.2 KiB
JavaScript
Raw Normal View History

2016-06-22 19:58:23 +00:00
import * as ActionTypes from '../../../constants';
2016-06-20 17:46:01 +00:00
import { browserHistory } from 'react-router'
2016-06-20 17:29:32 +00:00
import axios from 'axios'
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
export function getProject(id) {
return function(dispatch) {
axios.get(`${ROOT_URL}/projects/${id}`, {withCredentials: true})
.then(response => {
browserHistory.push(`/projects/${id}`);
dispatch({
type: ActionTypes.SET_PROJECT_NAME,
project: response.data
})
})
.catch(response => dispatch({
type: ActionTypes.ERROR
}));
}
}
export function setProjectName(event) {
var name = event.target.textContent;
return {
type: ActionTypes.SET_PROJECT_NAME,
name: name
}
}
export function saveProject() {
return function(dispatch, getState) {
var state = getState();
var formParams = Object.assign({}, state.project);
formParams.file = state.file;
2016-06-20 20:29:08 +00:00
debugger;
2016-06-20 17:29:32 +00:00
if (state.id) {
axios.put(`${ROOT_URL}/projects/${state.id}`, formParams, {withCredentials: true})
.then(response => {
dispatch({
2016-06-20 22:05:04 +00:00
type: ActionTypes.PROJECT_SAVE_SUCCESS
2016-06-20 17:29:32 +00:00
})
.catch(response => dispatch({
type: ActionTypes.PROJECT_SAVE_FAIL
}));
})
}
else {
axios.post(`${ROOT_URL}/projects`, formParams, {withCredentials: true})
.then(response => {
2016-06-20 17:46:01 +00:00
browserHistory.push('/projects/' + response.data.id);
2016-06-20 17:29:32 +00:00
dispatch({
type: ActionTypes.NEW_PROJECT,
name: response.data.name,
id: response.data.id,
file: {
name: response.data.file.name,
content: response.data.file.content
}
});
})
.catch(response => dispatch({
type: ActionTypes.PROJECT_SAVE_FAIL
}));
}
}
}
export function createProject() {
return function(dispatch) {
axios.post(`${ROOT_URL}/projects`, {}, {withCredentials: true})
.then(response => {
2016-06-20 20:29:08 +00:00
browserHistory.push('/projects/' + response.data.id);
2016-06-20 17:29:32 +00:00
dispatch({
type: ActionTypes.NEW_PROJECT,
name: response.data.name,
id: response.data.id,
file: {
name: response.data.file.name,
content: response.data.file.content
}
});
})
.catch(response => dispatch({
type: ActionTypes.PROJECT_SAVE_FAIL
}));
}
}