2016-06-24 00:29:55 +02:00
|
|
|
import { browserHistory } from 'react-router';
|
|
|
|
import axios from 'axios';
|
2017-02-17 20:53:48 +01:00
|
|
|
import objectID from 'bson-objectid';
|
2017-02-22 20:29:35 +01:00
|
|
|
import * as ActionTypes from '../../../constants';
|
2016-09-08 01:00:52 +02:00
|
|
|
import { showToast, setToastText } from './toast';
|
2017-01-13 23:17:31 +01:00
|
|
|
import { setUnsavedChanges,
|
|
|
|
justOpenedProject,
|
|
|
|
resetJustOpenedProject,
|
2017-01-24 21:29:25 +01:00
|
|
|
showErrorModal } from './ide';
|
2016-06-20 19:29:32 +02:00
|
|
|
|
2017-03-16 05:34:14 +01:00
|
|
|
const ROOT_URL = process.env.API_URL;
|
2016-06-20 19:29:32 +02:00
|
|
|
|
2017-03-02 20:38:29 +01:00
|
|
|
export function setProject(project) {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.SET_PROJECT,
|
|
|
|
project,
|
|
|
|
files: project.files,
|
|
|
|
owner: project.user
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setProjectName(name) {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.SET_PROJECT_NAME,
|
|
|
|
name
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-20 19:29:32 +02:00
|
|
|
export function getProject(id) {
|
2016-11-09 18:52:14 +01:00
|
|
|
return (dispatch, getState) => {
|
2016-10-28 01:45:09 +02:00
|
|
|
dispatch(justOpenedProject());
|
2016-06-24 00:29:55 +02:00
|
|
|
axios.get(`${ROOT_URL}/projects/${id}`, { withCredentials: true })
|
2017-02-22 20:29:35 +01:00
|
|
|
.then((response) => {
|
2017-03-02 20:38:29 +01:00
|
|
|
dispatch(setProject(response.data));
|
2016-09-21 00:27:10 +02:00
|
|
|
dispatch(setUnsavedChanges(false));
|
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
|
|
|
}
|
|
|
|
|
2016-10-19 18:36:40 +02:00
|
|
|
export function saveProject(autosave = false) {
|
2016-06-24 00:29:55 +02:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2016-08-12 19:31:34 +02:00
|
|
|
if (state.user.id && state.project.owner && state.project.owner.id !== state.user.id) {
|
|
|
|
return;
|
|
|
|
}
|
2016-06-24 00:29:55 +02:00
|
|
|
const formParams = Object.assign({}, state.project);
|
2016-07-08 20:57:22 +02:00
|
|
|
formParams.files = [...state.files];
|
2016-06-29 01:35:56 +02:00
|
|
|
if (state.project.id) {
|
|
|
|
axios.put(`${ROOT_URL}/projects/${state.project.id}`, formParams, { withCredentials: true })
|
2017-03-02 20:38:29 +01:00
|
|
|
.then((response) => {
|
2016-09-21 00:27:10 +02:00
|
|
|
dispatch(setUnsavedChanges(false));
|
2017-03-06 20:13:39 +01:00
|
|
|
console.log(response.data);
|
2017-03-02 20:38:29 +01:00
|
|
|
dispatch(setProject(response.data));
|
2016-06-24 00:29:55 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.PROJECT_SAVE_SUCCESS
|
2016-06-29 01:35:56 +02:00
|
|
|
});
|
2016-09-08 04:20:42 +02:00
|
|
|
if (!autosave) {
|
2016-10-28 01:45:09 +02:00
|
|
|
if (state.ide.justOpenedProject && state.preferences.autosave) {
|
|
|
|
dispatch(showToast(5500));
|
|
|
|
dispatch(setToastText('Project saved.'));
|
|
|
|
setTimeout(() => dispatch(setToastText('Autosave enabled.')), 1500);
|
|
|
|
dispatch(resetJustOpenedProject());
|
|
|
|
} else {
|
|
|
|
dispatch(showToast(1500));
|
|
|
|
dispatch(setToastText('Project saved.'));
|
|
|
|
}
|
2016-09-08 04:20:42 +02:00
|
|
|
}
|
2016-06-29 01:35:56 +02:00
|
|
|
})
|
2017-01-13 23:17:31 +01:00
|
|
|
.catch((response) => {
|
|
|
|
if (response.status === 403) {
|
2017-01-24 21:29:25 +01:00
|
|
|
dispatch(showErrorModal('staleSession'));
|
|
|
|
} else if (response.status === 409) {
|
|
|
|
dispatch(showErrorModal('staleProject'));
|
2017-01-13 23:17:31 +01:00
|
|
|
} else {
|
|
|
|
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 })
|
2017-02-22 20:29:35 +01:00
|
|
|
.then((response) => {
|
2016-09-21 00:27:10 +02:00
|
|
|
dispatch(setUnsavedChanges(false));
|
2017-03-02 20:38:29 +01:00
|
|
|
dispatch(setProject(response.data));
|
2016-12-01 23:12:34 +01:00
|
|
|
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
|
2016-06-24 00:29:55 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.NEW_PROJECT,
|
2017-01-24 21:29:25 +01:00
|
|
|
project: response.data,
|
2016-07-15 17:54:47 +02:00
|
|
|
owner: response.data.user,
|
2016-07-06 23:29:07 +02:00
|
|
|
files: response.data.files
|
2016-06-24 00:29:55 +02:00
|
|
|
});
|
2016-09-08 04:20:42 +02:00
|
|
|
if (!autosave) {
|
2016-10-28 01:45:09 +02:00
|
|
|
if (state.preferences.autosave) {
|
|
|
|
dispatch(showToast(5500));
|
|
|
|
dispatch(setToastText('Project saved.'));
|
|
|
|
setTimeout(() => dispatch(setToastText('Autosave enabled.')), 1500);
|
|
|
|
dispatch(resetJustOpenedProject());
|
|
|
|
} else {
|
|
|
|
dispatch(showToast(1500));
|
|
|
|
dispatch(setToastText('Project saved.'));
|
|
|
|
}
|
2016-09-08 01:12:01 +02:00
|
|
|
}
|
2016-06-24 00:29:55 +02:00
|
|
|
})
|
2017-02-22 20:29:35 +01:00
|
|
|
.catch((response) => {
|
2017-01-13 23:32:06 +01:00
|
|
|
if (response.status === 403) {
|
2017-01-24 21:29:25 +01:00
|
|
|
dispatch(showErrorModal('staleSession'));
|
2017-01-13 23:32:06 +01:00
|
|
|
} else {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.PROJECT_SAVE_FAIL,
|
|
|
|
error: response.data
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2016-06-24 00:29:55 +02:00
|
|
|
}
|
|
|
|
};
|
2016-06-20 19:29:32 +02:00
|
|
|
}
|
|
|
|
|
2016-09-08 04:20:42 +02:00
|
|
|
export function autosaveProject() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
saveProject(true)(dispatch, getState);
|
|
|
|
};
|
|
|
|
}
|
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 })
|
2017-02-22 20:29:35 +01:00
|
|
|
.then((response) => {
|
2016-12-01 23:12:34 +01:00
|
|
|
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
|
2016-06-24 00:29:55 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.NEW_PROJECT,
|
2017-01-24 21:29:25 +01:00
|
|
|
project: response.data,
|
2016-07-15 17:54:47 +02:00
|
|
|
owner: response.data.user,
|
2016-07-06 23:29:07 +02:00
|
|
|
files: response.data.files
|
2016-06-24 00:29:55 +02:00
|
|
|
});
|
2016-09-21 00:27:10 +02:00
|
|
|
dispatch(setUnsavedChanges(false));
|
2016-06-24 00:29:55 +02:00
|
|
|
})
|
|
|
|
.catch(response => dispatch({
|
|
|
|
type: ActionTypes.PROJECT_SAVE_FAIL,
|
|
|
|
error: response.data
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
}
|
2016-07-15 19:11:50 +02:00
|
|
|
|
2016-11-02 19:08:53 +01:00
|
|
|
export function exportProjectAsZip(projectId) {
|
|
|
|
const win = window.open(`${ROOT_URL}/projects/${projectId}/zip`, '_blank');
|
|
|
|
win.focus();
|
2016-07-15 19:11:50 +02:00
|
|
|
}
|
|
|
|
|
2016-11-11 00:49:42 +01:00
|
|
|
export function resetProject() {
|
2016-08-12 18:45:26 +02:00
|
|
|
return {
|
|
|
|
type: ActionTypes.RESET_PROJECT
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-11 00:49:42 +01:00
|
|
|
export function newProject() {
|
2017-01-05 21:27:57 +01:00
|
|
|
setTimeout(() => {
|
|
|
|
browserHistory.push('/');
|
|
|
|
}, 0);
|
2016-11-11 00:49:42 +01:00
|
|
|
return resetProject();
|
|
|
|
}
|
|
|
|
|
2017-02-17 20:53:48 +01:00
|
|
|
function generateNewIdsForChildren(file, files) {
|
|
|
|
const newChildren = [];
|
|
|
|
file.children.forEach((childId) => {
|
|
|
|
const child = files.find(childFile => childFile.id === childId);
|
|
|
|
const newId = objectID().toHexString();
|
|
|
|
child.id = newId;
|
|
|
|
child._id = newId;
|
|
|
|
newChildren.push(newId);
|
|
|
|
generateNewIdsForChildren(child, files);
|
|
|
|
});
|
|
|
|
file.children = newChildren; // eslint-disable-line
|
|
|
|
}
|
|
|
|
|
2016-07-15 19:36:33 +02:00
|
|
|
export function cloneProject() {
|
|
|
|
return (dispatch, getState) => {
|
2017-01-17 19:20:42 +01:00
|
|
|
dispatch(setUnsavedChanges(false));
|
2016-07-15 19:36:33 +02:00
|
|
|
const state = getState();
|
2017-03-06 20:13:39 +01:00
|
|
|
const newFiles = state.files.map((file) => { // eslint-disable-line
|
|
|
|
return { ...file };
|
|
|
|
});
|
|
|
|
|
2017-02-17 20:53:48 +01:00
|
|
|
const rootFile = newFiles.find(file => file.name === 'root');
|
|
|
|
const newRootFileId = objectID().toHexString();
|
|
|
|
rootFile.id = newRootFileId;
|
|
|
|
rootFile._id = newRootFileId;
|
|
|
|
generateNewIdsForChildren(rootFile, newFiles);
|
|
|
|
// const newFiles = state.files;
|
2017-01-31 21:18:40 +01:00
|
|
|
const formParams = Object.assign({}, { name: `${state.project.name} copy` }, { files: newFiles });
|
2016-07-15 19:36:33 +02:00
|
|
|
axios.post(`${ROOT_URL}/projects`, formParams, { withCredentials: true })
|
2017-02-22 20:29:35 +01:00
|
|
|
.then((response) => {
|
2016-12-01 23:12:34 +01:00
|
|
|
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
|
2017-03-06 20:13:39 +01:00
|
|
|
console.log(response.data);
|
2016-07-15 19:36:33 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.NEW_PROJECT,
|
2017-01-24 21:29:25 +01:00
|
|
|
project: response.data,
|
2016-07-15 19:36:33 +02:00
|
|
|
owner: response.data.user,
|
|
|
|
files: response.data.files
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(response => dispatch({
|
|
|
|
type: ActionTypes.PROJECT_SAVE_FAIL,
|
|
|
|
error: response.data
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-15 18:42:13 +02:00
|
|
|
export function showEditProjectName() {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.SHOW_EDIT_PROJECT_NAME
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hideEditProjectName() {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.HIDE_EDIT_PROJECT_NAME
|
|
|
|
};
|
|
|
|
}
|