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

338 lines
9.8 KiB
JavaScript
Raw Normal View History

2016-06-23 22:29:55 +00:00
import { browserHistory } from 'react-router';
import axios from 'axios';
2017-02-17 19:53:48 +00:00
import objectID from 'bson-objectid';
2017-03-23 18:50:47 +00:00
import each from 'async/each';
import isEqual from 'lodash/isEqual';
import * as ActionTypes from '../../../constants';
import { showToast, setToastText } from './toast';
import {
setUnsavedChanges,
justOpenedProject,
resetJustOpenedProject,
showErrorModal
} from './ide';
import { clearState, saveState } from '../../../persistState';
2016-06-20 17:29:32 +00:00
const __process = (typeof global !== 'undefined' ? global : window).process;
const ROOT_URL = __process.env.API_URL;
2016-06-20 17:29:32 +00: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
};
}
export function projectSaveFail(error) {
return {
type: ActionTypes.PROJECT_SAVE_FAIL,
error
};
}
export function setNewProject(project) {
return {
type: ActionTypes.NEW_PROJECT,
project,
owner: project.user,
files: project.files
};
}
2016-06-20 17:29:32 +00:00
export function getProject(id) {
return (dispatch, getState) => {
dispatch(justOpenedProject());
2016-06-23 22:29:55 +00:00
axios.get(`${ROOT_URL}/projects/${id}`, { withCredentials: true })
.then((response) => {
dispatch(setProject(response.data));
dispatch(setUnsavedChanges(false));
2016-06-23 22:29:55 +00:00
})
.catch(response => dispatch({
type: ActionTypes.ERROR,
error: response.data
}));
};
2016-06-20 17:29:32 +00:00
}
export function persistState() {
return (dispatch, getState) => {
dispatch({
type: ActionTypes.PERSIST_STATE,
});
const state = getState();
saveState(state);
};
}
export function clearPersistedState() {
return (dispatch) => {
dispatch({
type: ActionTypes.CLEAR_PERSISTED_STATE,
});
clearState();
};
}
export function startSavingProject() {
return {
type: ActionTypes.START_SAVING_PROJECT
};
}
export function endSavingProject() {
return {
type: ActionTypes.END_SAVING_PROJECT
};
}
export function projectSaveSuccess() {
return {
type: ActionTypes.PROJECT_SAVE_SUCCESS
};
}
// want a function that will check for changes on the front end
function getSynchedProject(currentState, responseProject) {
let hasChanges = false;
const synchedProject = Object.assign({}, responseProject);
const currentFiles = currentState.files.map(({ name, children, content }) => ({ name, children, content }));
const responseFiles = responseProject.files.map(({ name, children, content }) => ({ name, children, content }));
if (!isEqual(currentFiles, responseFiles)) {
synchedProject.files = currentState.files;
hasChanges = true;
}
if (currentState.project.name !== responseProject.name) {
synchedProject.name = currentState.project.name;
hasChanges = true;
}
return {
synchedProject,
hasChanges
};
}
export function saveProject(selectedFile = null, autosave = false) {
2016-06-23 22:29:55 +00:00
return (dispatch, getState) => {
const state = getState();
if (state.project.isSaving) {
return Promise.resolve();
}
dispatch(startSavingProject());
if (state.user.id && state.project.owner && state.project.owner.id !== state.user.id) {
return Promise.reject();
}
2016-06-23 22:29:55 +00:00
const formParams = Object.assign({}, state.project);
2016-07-08 18:57:22 +00:00
formParams.files = [...state.files];
if (selectedFile) {
const fileToUpdate = formParams.files.find(file => file.id === selectedFile.id);
fileToUpdate.content = selectedFile.content;
}
2016-06-28 23:35:56 +00:00
if (state.project.id) {
return axios.put(`${ROOT_URL}/projects/${state.project.id}`, formParams, { withCredentials: true })
.then((response) => {
dispatch(endSavingProject());
dispatch(setUnsavedChanges(false));
const { hasChanges, synchedProject } = getSynchedProject(getState(), response.data);
if (hasChanges) {
dispatch(setUnsavedChanges(true));
}
dispatch(setProject(synchedProject));
dispatch(projectSaveSuccess());
2016-09-08 02:20:42 +00:00
if (!autosave) {
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 02:20:42 +00:00
}
2016-06-28 23:35:56 +00:00
})
.catch((response) => {
dispatch(endSavingProject());
if (response.status === 403) {
dispatch(showErrorModal('staleSession'));
} else if (response.status === 409) {
dispatch(showErrorModal('staleProject'));
} else {
dispatch(projectSaveFail(response.data));
}
});
}
return axios.post(`${ROOT_URL}/projects`, formParams, { withCredentials: true })
.then((response) => {
dispatch(endSavingProject());
const { hasChanges, synchedProject } = getSynchedProject(getState(), response.data);
if (hasChanges) {
dispatch(setNewProject(synchedProject));
dispatch(setUnsavedChanges(false));
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
dispatch(setUnsavedChanges(true));
} else {
dispatch(setNewProject(synchedProject));
dispatch(setUnsavedChanges(false));
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
}
dispatch(projectSaveSuccess());
if (!autosave) {
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.'));
}
}
})
.catch((response) => {
dispatch(endSavingProject());
if (response.status === 403) {
dispatch(showErrorModal('staleSession'));
} else {
dispatch(projectSaveFail(response.data));
}
});
2016-06-23 22:29:55 +00:00
};
2016-06-20 17:29:32 +00:00
}
2016-09-08 02:20:42 +00:00
export function autosaveProject() {
return (dispatch, getState) => {
saveProject(null, true)(dispatch, getState);
2016-09-08 02:20:42 +00:00
};
}
2016-06-20 17:29:32 +00:00
export function createProject() {
return (dispatch, getState) => {
const state = getState();
if (state.project.isSaving) {
Promise.resolve();
return;
}
dispatch(startSavingProject());
2016-06-23 22:29:55 +00:00
axios.post(`${ROOT_URL}/projects`, {}, { withCredentials: true })
.then((response) => {
dispatch(endSavingProject());
dispatch(setUnsavedChanges(false));
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
const { hasChanges, synchedProject } = getSynchedProject(getState(), response.data);
if (hasChanges) {
dispatch(setUnsavedChanges(true));
}
dispatch(setNewProject(synchedProject));
2016-06-23 22:29:55 +00:00
})
.catch((response) => {
dispatch(endSavingProject());
dispatch(projectSaveFail(response.data));
});
2016-06-23 22:29:55 +00:00
};
}
2016-07-15 17:11:50 +00:00
export function exportProjectAsZip(projectId) {
const win = window.open(`${ROOT_URL}/projects/${projectId}/zip`, '_blank');
win.focus();
2016-07-15 17:11:50 +00:00
}
2016-11-10 23:49:42 +00:00
export function resetProject() {
return {
type: ActionTypes.RESET_PROJECT
};
}
2016-11-10 23:49:42 +00:00
export function newProject() {
2017-01-05 20:27:57 +00:00
setTimeout(() => {
browserHistory.push('/');
}, 0);
2016-11-10 23:49:42 +00:00
return resetProject();
}
2017-02-17 19:53:48 +00: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 17:36:33 +00:00
export function cloneProject() {
return (dispatch, getState) => {
2017-01-17 18:20:42 +00:00
dispatch(setUnsavedChanges(false));
2016-07-15 17:36:33 +00:00
const state = getState();
2017-03-06 19:13:39 +00:00
const newFiles = state.files.map((file) => { // eslint-disable-line
return { ...file };
});
2017-03-23 18:50:47 +00:00
// generate new IDS for all files
2017-02-17 19:53:48 +00:00
const rootFile = newFiles.find(file => file.name === 'root');
const newRootFileId = objectID().toHexString();
rootFile.id = newRootFileId;
rootFile._id = newRootFileId;
generateNewIdsForChildren(rootFile, newFiles);
2017-03-23 18:50:47 +00:00
// duplicate all files hosted on S3
each(newFiles, (file, callback) => {
if (file.url && file.url.includes('amazonaws')) {
const formParams = {
url: file.url
};
2017-03-23 18:50:47 +00:00
axios.post(`${ROOT_URL}/S3/copy`, formParams, { withCredentials: true })
.then((response) => {
file.url = response.data.url;
callback(null);
});
} else {
callback(null);
}
}, (err) => {
// if not errors in duplicating the files on S3, then duplicate it
const formParams = Object.assign({}, { name: `${state.project.name} copy` }, { files: newFiles });
axios.post(`${ROOT_URL}/projects`, formParams, { withCredentials: true })
.then((response) => {
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
dispatch(setNewProject(response.data));
})
.catch(response => dispatch({
type: ActionTypes.PROJECT_SAVE_FAIL,
error: response.data
}));
});
2016-07-15 17:36:33 +00:00
};
}
export function showEditProjectName() {
return {
type: ActionTypes.SHOW_EDIT_PROJECT_NAME
};
}
export function hideEditProjectName() {
return {
type: ActionTypes.HIDE_EDIT_PROJECT_NAME
};
}
export function setProjectSavedTime(updatedAt) {
return {
type: ActionTypes.SET_PROJECT_SAVED_TIME,
value: updatedAt
};
}