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

93 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-06-22 21:58:23 +02:00
import * as ActionTypes from '../../../constants';
2016-06-24 00:29:55 +02:00
import { browserHistory } from 'react-router';
import axios from 'axios';
2016-06-20 19:29:32 +02:00
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
export function getProject(id) {
2016-06-24 00:29:55 +02:00
return (dispatch) => {
axios.get(`${ROOT_URL}/projects/${id}`, { withCredentials: true })
.then(response => {
browserHistory.push(`/projects/${id}`);
dispatch({
2016-06-29 01:35:56 +02:00
type: ActionTypes.SET_PROJECT,
2016-06-29 18:52:16 +02:00
project: response.data,
file: response.data.file
2016-06-24 00:29:55 +02:00
});
})
.catch(response => dispatch({
type: ActionTypes.ERROR,
error: response.data
}));
};
2016-06-20 19:29:32 +02:00
}
export function setProjectName(event) {
2016-06-24 00:29:55 +02:00
const name = event.target.textContent;
return {
type: ActionTypes.SET_PROJECT_NAME,
name
};
2016-06-20 19:29:32 +02:00
}
export function saveProject() {
2016-06-24 00:29:55 +02:00
return (dispatch, getState) => {
const state = getState();
const formParams = Object.assign({}, state.project);
formParams.file = state.file;
2016-06-29 01:35:56 +02:00
if (state.project.id) {
axios.put(`${ROOT_URL}/projects/${state.project.id}`, formParams, { withCredentials: true })
2016-06-24 00:29:55 +02:00
.then(() => {
dispatch({
type: ActionTypes.PROJECT_SAVE_SUCCESS
2016-06-29 01:35:56 +02:00
});
})
.catch((response) => dispatch({
type: ActionTypes.PROJECT_SAVE_FAIL,
error: response.data
}));
2016-06-24 00:29:55 +02:00
} else {
axios.post(`${ROOT_URL}/projects`, formParams, { withCredentials: true })
.then(response => {
browserHistory.push(`/projects/${response.data.id}`);
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,
error: response.data
}));
}
};
2016-06-20 19:29:32 +02:00
}
export function createProject() {
2016-06-24 00:29:55 +02:00
return (dispatch) => {
axios.post(`${ROOT_URL}/projects`, {}, { withCredentials: true })
.then(response => {
browserHistory.push(`/projects/${response.data.id}`);
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,
error: response.data
}));
};
}