2016-06-24 00:29:55 +02:00
|
|
|
import { browserHistory } from 'react-router';
|
2017-02-17 20:53:48 +01:00
|
|
|
import objectID from 'bson-objectid';
|
2017-03-23 19:50:47 +01:00
|
|
|
import each from 'async/each';
|
2019-04-17 20:08:33 +02:00
|
|
|
import isEqual from 'lodash/isEqual';
|
2020-06-08 12:29:24 +02:00
|
|
|
import apiClient from '../../../utils/apiClient';
|
|
|
|
import getConfig from '../../../utils/getConfig';
|
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';
|
2019-01-16 23:56:18 +01:00
|
|
|
import {
|
|
|
|
setUnsavedChanges,
|
2017-01-13 23:17:31 +01:00
|
|
|
justOpenedProject,
|
|
|
|
resetJustOpenedProject,
|
2019-06-19 22:21:25 +02:00
|
|
|
showErrorModal,
|
|
|
|
setPreviousPath
|
2019-01-16 23:56:18 +01:00
|
|
|
} from './ide';
|
2017-04-20 20:05:15 +02:00
|
|
|
import { clearState, saveState } from '../../../persistState';
|
2016-06-20 19:29:32 +02:00
|
|
|
|
2020-06-08 12:29:24 +02:00
|
|
|
const ROOT_URL = getConfig('API_URL');
|
2020-07-16 23:00:50 +02:00
|
|
|
const S3_BUCKET_URL_BASE = getConfig('S3_BUCKET_URL_BASE');
|
|
|
|
const S3_BUCKET = getConfig('S3_BUCKET');
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-17 20:08:33 +02:00
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-26 23:35:13 +02:00
|
|
|
export function getProject(id, username) {
|
2016-11-09 18:52:14 +01:00
|
|
|
return (dispatch, getState) => {
|
2016-10-28 01:45:09 +02:00
|
|
|
dispatch(justOpenedProject());
|
2020-06-08 12:29:24 +02:00
|
|
|
apiClient.get(`/${username}/projects/${id}`)
|
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
|
|
|
})
|
2020-04-25 16:48:39 +02:00
|
|
|
.catch((error) => {
|
|
|
|
const { response } = error;
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
});
|
|
|
|
});
|
2016-06-24 00:29:55 +02:00
|
|
|
};
|
2016-06-20 19:29:32 +02:00
|
|
|
}
|
|
|
|
|
2017-04-20 20:05:15 +02: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();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-17 20:08:33 +02:00
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-17 19:53:22 +02:00
|
|
|
export function saveProject(selectedFile = null, autosave = false, mobile = false) {
|
2016-06-24 00:29:55 +02:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2019-04-17 20:08:33 +02:00
|
|
|
if (state.project.isSaving) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
dispatch(startSavingProject());
|
2016-08-12 19:31:34 +02:00
|
|
|
if (state.user.id && state.project.owner && state.project.owner.id !== state.user.id) {
|
2017-05-03 17:46:12 +02:00
|
|
|
return Promise.reject();
|
2016-08-12 19:31:34 +02:00
|
|
|
}
|
2016-06-24 00:29:55 +02:00
|
|
|
const formParams = Object.assign({}, state.project);
|
2020-04-16 15:55:58 +02:00
|
|
|
formParams.files = [...state.files];
|
2020-04-08 01:29:20 +02:00
|
|
|
|
2019-02-25 22:45:20 +01:00
|
|
|
if (selectedFile) {
|
|
|
|
const fileToUpdate = formParams.files.find(file => file.id === selectedFile.id);
|
|
|
|
fileToUpdate.content = selectedFile.content;
|
|
|
|
}
|
2016-06-29 01:35:56 +02:00
|
|
|
if (state.project.id) {
|
2020-06-08 12:29:24 +02:00
|
|
|
return apiClient.put(`/projects/${state.project.id}`, formParams)
|
2017-03-02 20:38:29 +01:00
|
|
|
.then((response) => {
|
2019-04-17 20:08:33 +02:00
|
|
|
dispatch(endSavingProject());
|
|
|
|
dispatch(setUnsavedChanges(false));
|
|
|
|
const { hasChanges, synchedProject } = getSynchedProject(getState(), response.data);
|
|
|
|
if (hasChanges) {
|
2018-02-20 20:44:07 +01:00
|
|
|
dispatch(setUnsavedChanges(true));
|
|
|
|
}
|
2019-04-17 20:08:33 +02:00
|
|
|
dispatch(setProject(synchedProject));
|
|
|
|
dispatch(projectSaveSuccess());
|
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));
|
2020-07-31 15:20:42 +02:00
|
|
|
dispatch(setToastText('Toast.SketchSaved'));
|
|
|
|
setTimeout(() => dispatch(setToastText('Toast.AutosaveEnabled')), 1500);
|
2016-10-28 01:45:09 +02:00
|
|
|
dispatch(resetJustOpenedProject());
|
|
|
|
} else {
|
|
|
|
dispatch(showToast(1500));
|
2020-07-31 15:20:42 +02:00
|
|
|
dispatch(setToastText('Toast.SketchSaved'));
|
2016-10-28 01:45:09 +02:00
|
|
|
}
|
2016-09-08 04:20:42 +02:00
|
|
|
}
|
2016-06-29 01:35:56 +02:00
|
|
|
})
|
2020-04-25 16:48:39 +02:00
|
|
|
.catch((error) => {
|
|
|
|
const { response } = error;
|
2019-04-17 20:08:33 +02:00
|
|
|
dispatch(endSavingProject());
|
2020-07-31 15:20:42 +02:00
|
|
|
dispatch(setToastText('Toast.SketchFailedSave'));
|
2020-06-23 18:41:10 +02:00
|
|
|
dispatch(showToast(1500));
|
2017-01-13 23:17:31 +01:00
|
|
|
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 {
|
2019-04-17 20:08:33 +02:00
|
|
|
dispatch(projectSaveFail(response.data));
|
2017-01-13 23:17:31 +01:00
|
|
|
}
|
|
|
|
});
|
2017-05-03 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2020-06-08 12:29:24 +02:00
|
|
|
return apiClient.post('/projects', formParams)
|
2017-05-03 17:46:12 +02:00
|
|
|
.then((response) => {
|
2019-04-17 20:08:33 +02:00
|
|
|
dispatch(endSavingProject());
|
|
|
|
const { hasChanges, synchedProject } = getSynchedProject(getState(), response.data);
|
2020-08-17 19:53:22 +02:00
|
|
|
|
|
|
|
dispatch(setNewProject(synchedProject));
|
|
|
|
dispatch(setUnsavedChanges(false));
|
|
|
|
browserHistory.push(`${mobile ? '/mobile' : ''}/${response.data.user.username}/sketches/${response.data.id}`);
|
|
|
|
|
2019-04-17 20:08:33 +02:00
|
|
|
if (hasChanges) {
|
|
|
|
dispatch(setUnsavedChanges(true));
|
|
|
|
}
|
2020-08-17 19:53:22 +02:00
|
|
|
|
2019-04-17 20:08:33 +02:00
|
|
|
dispatch(projectSaveSuccess());
|
2017-05-03 17:46:12 +02:00
|
|
|
if (!autosave) {
|
|
|
|
if (state.preferences.autosave) {
|
|
|
|
dispatch(showToast(5500));
|
2020-07-31 15:20:42 +02:00
|
|
|
dispatch(setToastText('Toast.SketchSaved'));
|
|
|
|
setTimeout(() => dispatch(setToastText('Toast.AutosaveEnabled')), 1500);
|
2017-05-03 17:46:12 +02:00
|
|
|
dispatch(resetJustOpenedProject());
|
2017-01-13 23:32:06 +01:00
|
|
|
} else {
|
2017-05-03 17:46:12 +02:00
|
|
|
dispatch(showToast(1500));
|
2020-07-31 15:20:42 +02:00
|
|
|
dispatch(setToastText('Toast.SketchSaved'));
|
2017-01-13 23:32:06 +01:00
|
|
|
}
|
2017-05-03 17:46:12 +02:00
|
|
|
}
|
|
|
|
})
|
2020-04-25 16:48:39 +02:00
|
|
|
.catch((error) => {
|
|
|
|
const { response } = error;
|
2019-04-17 20:08:33 +02:00
|
|
|
dispatch(endSavingProject());
|
2020-07-31 15:20:42 +02:00
|
|
|
dispatch(setToastText('Toast.SketchFailedSave'));
|
2020-06-23 18:41:10 +02:00
|
|
|
dispatch(showToast(1500));
|
2017-05-03 17:46:12 +02:00
|
|
|
if (response.status === 403) {
|
|
|
|
dispatch(showErrorModal('staleSession'));
|
|
|
|
} else {
|
2019-04-17 20:08:33 +02:00
|
|
|
dispatch(projectSaveFail(response.data));
|
2017-05-03 17:46:12 +02:00
|
|
|
}
|
|
|
|
});
|
2016-06-24 00:29:55 +02:00
|
|
|
};
|
2016-06-20 19:29:32 +02:00
|
|
|
}
|
|
|
|
|
2020-08-17 19:53:22 +02:00
|
|
|
export function autosaveProject(mobile = false) {
|
2016-09-08 04:20:42 +02:00
|
|
|
return (dispatch, getState) => {
|
2020-08-17 19:53:22 +02:00
|
|
|
saveProject(null, true, mobile)(dispatch, getState);
|
2016-09-08 04:20:42 +02:00
|
|
|
};
|
|
|
|
}
|
2016-06-20 19:29:32 +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
|
|
|
|
}
|
|
|
|
|
2019-06-19 22:21:25 +02:00
|
|
|
export function cloneProject(id) {
|
2016-07-15 19:36:33 +02:00
|
|
|
return (dispatch, getState) => {
|
2017-01-17 19:20:42 +01:00
|
|
|
dispatch(setUnsavedChanges(false));
|
2019-06-19 22:21:25 +02:00
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
if (!id) {
|
|
|
|
resolve(getState());
|
|
|
|
} else {
|
2020-06-08 12:29:24 +02:00
|
|
|
apiClient.get(`/projects/${id}`)
|
2019-06-19 22:21:25 +02:00
|
|
|
.then(res => res.json())
|
|
|
|
.then(data => resolve({
|
|
|
|
files: data.files,
|
|
|
|
project: {
|
|
|
|
name: data.name
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}).then((state) => {
|
|
|
|
const newFiles = state.files.map((file) => { // eslint-disable-line
|
|
|
|
return { ...file };
|
|
|
|
});
|
2017-03-06 20:13:39 +01:00
|
|
|
|
2019-06-19 22:21:25 +02:00
|
|
|
// generate new IDS for all files
|
|
|
|
const rootFile = newFiles.find(file => file.name === 'root');
|
|
|
|
const newRootFileId = objectID().toHexString();
|
|
|
|
rootFile.id = newRootFileId;
|
|
|
|
rootFile._id = newRootFileId;
|
|
|
|
generateNewIdsForChildren(rootFile, newFiles);
|
2017-03-23 19:50:47 +01:00
|
|
|
|
2019-06-19 22:21:25 +02:00
|
|
|
// duplicate all files hosted on S3
|
|
|
|
each(newFiles, (file, callback) => {
|
2020-07-16 23:00:50 +02:00
|
|
|
if (file.url && (file.url.includes(S3_BUCKET_URL_BASE) || file.url.includes(S3_BUCKET))) {
|
2019-06-19 22:21:25 +02:00
|
|
|
const formParams = {
|
|
|
|
url: file.url
|
|
|
|
};
|
2020-06-08 12:29:24 +02:00
|
|
|
apiClient.post('/S3/copy', formParams)
|
2019-06-19 22:21:25 +02:00
|
|
|
.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 });
|
2020-06-08 12:29:24 +02:00
|
|
|
apiClient.post('/projects', formParams)
|
2017-03-23 19:23:54 +01:00
|
|
|
.then((response) => {
|
2019-06-19 22:21:25 +02:00
|
|
|
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
|
|
|
|
dispatch(setNewProject(response.data));
|
|
|
|
})
|
2020-04-25 16:48:39 +02:00
|
|
|
.catch((error) => {
|
|
|
|
const { response } = error;
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.PROJECT_SAVE_FAIL,
|
|
|
|
error: response.data
|
|
|
|
});
|
|
|
|
});
|
2019-06-19 22:21:25 +02:00
|
|
|
});
|
2017-03-23 19:23:54 +01:00
|
|
|
});
|
2016-07-15 19:36:33 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
2019-04-24 19:32:23 +02:00
|
|
|
|
|
|
|
export function setProjectSavedTime(updatedAt) {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.SET_PROJECT_SAVED_TIME,
|
|
|
|
value: updatedAt
|
|
|
|
};
|
|
|
|
}
|
2019-06-19 22:21:25 +02:00
|
|
|
|
|
|
|
export function changeProjectName(id, newName) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2020-06-08 12:29:24 +02:00
|
|
|
apiClient.put(`/projects/${id}`, { name: newName })
|
2019-06-19 22:21:25 +02:00
|
|
|
.then((response) => {
|
|
|
|
if (response.status === 200) {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.RENAME_PROJECT,
|
|
|
|
payload: { id: response.data.id, name: response.data.name }
|
|
|
|
});
|
|
|
|
if (state.project.id === response.data.id) {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.SET_PROJECT_NAME,
|
|
|
|
name: response.data.name
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-04-25 16:48:39 +02:00
|
|
|
.catch((error) => {
|
|
|
|
const { response } = error;
|
2019-06-19 22:21:25 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.PROJECT_SAVE_FAIL,
|
|
|
|
error: response.data
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function deleteProject(id) {
|
|
|
|
return (dispatch, getState) => {
|
2020-06-08 12:29:24 +02:00
|
|
|
apiClient.delete(`/projects/${id}`)
|
2019-06-19 22:21:25 +02:00
|
|
|
.then(() => {
|
|
|
|
const state = getState();
|
|
|
|
if (id === state.project.id) {
|
|
|
|
dispatch(resetProject());
|
|
|
|
dispatch(setPreviousPath('/'));
|
|
|
|
}
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.DELETE_PROJECT,
|
|
|
|
id
|
|
|
|
});
|
|
|
|
})
|
2020-04-25 16:48:39 +02:00
|
|
|
.catch((error) => {
|
|
|
|
const { response } = error;
|
2019-06-19 22:21:25 +02:00
|
|
|
if (response.status === 403) {
|
|
|
|
dispatch(showErrorModal('staleSession'));
|
|
|
|
} else {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|