2016-07-14 00:53:56 +02:00
|
|
|
import axios from 'axios';
|
2016-08-24 20:22:10 +02:00
|
|
|
import objectID from 'bson-objectid';
|
2016-10-22 22:42:43 +02:00
|
|
|
import blobUtil from 'blob-util';
|
2016-11-30 01:18:11 +01:00
|
|
|
import { reset } from 'redux-form';
|
2017-02-22 20:29:35 +01:00
|
|
|
import * as ActionTypes from '../../../constants';
|
2020-03-24 23:07:58 +01:00
|
|
|
import { setUnsavedChanges, closeNewFolderModal, closeNewFileModal } from './ide';
|
2019-04-24 19:32:23 +02:00
|
|
|
import { setProjectSavedTime } from './project';
|
2016-07-14 00:53:56 +02:00
|
|
|
|
2018-08-24 23:41:23 +02:00
|
|
|
const __process = (typeof global !== 'undefined' ? global : window).process;
|
|
|
|
const ROOT_URL = __process.env.API_URL;
|
2016-06-22 21:58:23 +02:00
|
|
|
|
2016-07-21 04:52:09 +02:00
|
|
|
function appendToFilename(filename, string) {
|
|
|
|
const dotIndex = filename.lastIndexOf('.');
|
|
|
|
if (dotIndex === -1) return filename + string;
|
|
|
|
return filename.substring(0, dotIndex) + string + filename.substring(dotIndex);
|
|
|
|
}
|
|
|
|
|
2016-09-03 20:52:51 +02:00
|
|
|
function createUniqueName(name, parentId, files) {
|
|
|
|
const siblingFiles = files.find(file => file.id === parentId)
|
|
|
|
.children.map(childFileId => files.find(file => file.id === childFileId));
|
2016-07-21 04:52:09 +02:00
|
|
|
let testName = name;
|
|
|
|
let index = 1;
|
2017-02-22 20:29:35 +01:00
|
|
|
let existingName = siblingFiles.find(file => name === file.name);
|
2016-07-21 04:52:09 +02:00
|
|
|
|
|
|
|
while (existingName) {
|
|
|
|
testName = appendToFilename(name, `-${index}`);
|
2017-02-22 20:29:35 +01:00
|
|
|
index += 1;
|
2016-09-03 20:52:51 +02:00
|
|
|
existingName = siblingFiles.find((file) => testName === file.name); // eslint-disable-line
|
2016-07-21 04:52:09 +02:00
|
|
|
}
|
|
|
|
return testName;
|
|
|
|
}
|
|
|
|
|
2019-02-25 18:57:10 +01:00
|
|
|
export function updateFileContent(id, content) {
|
2016-06-24 00:29:55 +02:00
|
|
|
return {
|
2016-07-07 19:50:52 +02:00
|
|
|
type: ActionTypes.UPDATE_FILE_CONTENT,
|
2019-02-25 18:57:10 +01:00
|
|
|
id,
|
2016-06-24 00:29:55 +02:00
|
|
|
content
|
|
|
|
};
|
|
|
|
}
|
2016-07-13 22:13:28 +02:00
|
|
|
|
2016-07-14 00:53:56 +02:00
|
|
|
export function createFile(formProps) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2019-10-08 22:39:47 +02:00
|
|
|
const { parentId } = state.ide;
|
2016-07-14 00:53:56 +02:00
|
|
|
if (state.project.id) {
|
|
|
|
const postParams = {
|
2016-09-03 20:52:51 +02:00
|
|
|
name: createUniqueName(formProps.name, parentId, state.files),
|
2016-08-25 17:25:22 +02:00
|
|
|
url: formProps.url,
|
2016-08-25 23:32:27 +02:00
|
|
|
content: formProps.content || '',
|
2016-09-03 00:11:27 +02:00
|
|
|
parentId,
|
|
|
|
children: []
|
2016-07-14 00:53:56 +02:00
|
|
|
};
|
|
|
|
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
|
2017-02-22 20:29:35 +01:00
|
|
|
.then((response) => {
|
2016-07-14 00:53:56 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.CREATE_FILE,
|
2019-04-24 19:32:23 +02:00
|
|
|
...response.data.updatedFile,
|
2016-08-30 20:39:37 +02:00
|
|
|
parentId
|
2016-07-14 00:53:56 +02:00
|
|
|
});
|
2019-04-24 19:32:23 +02:00
|
|
|
dispatch(setProjectSavedTime(response.data.project.updatedAt));
|
2020-03-24 23:07:58 +01:00
|
|
|
dispatch(closeNewFileModal());
|
2016-11-30 01:18:11 +01:00
|
|
|
dispatch(reset('new-file'));
|
|
|
|
// dispatch({
|
|
|
|
// type: ActionTypes.HIDE_MODAL
|
|
|
|
// });
|
2016-10-24 22:42:15 +02:00
|
|
|
dispatch(setUnsavedChanges(true));
|
2016-07-14 00:53:56 +02:00
|
|
|
})
|
2020-04-25 16:48:39 +02:00
|
|
|
.catch((error) => {
|
|
|
|
const { response } = error;
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
});
|
|
|
|
});
|
2016-07-14 00:53:56 +02:00
|
|
|
} else {
|
2016-08-24 20:22:10 +02:00
|
|
|
const id = objectID().toHexString();
|
2016-07-14 00:53:56 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.CREATE_FILE,
|
2016-09-03 20:52:51 +02:00
|
|
|
name: createUniqueName(formProps.name, parentId, state.files),
|
2016-08-24 20:22:10 +02:00
|
|
|
id,
|
|
|
|
_id: id,
|
2016-08-25 06:18:28 +02:00
|
|
|
url: formProps.url,
|
2016-08-25 23:32:27 +02:00
|
|
|
content: formProps.content || '',
|
2016-09-03 00:11:27 +02:00
|
|
|
parentId,
|
|
|
|
children: []
|
2016-07-14 00:53:56 +02:00
|
|
|
});
|
2016-11-30 01:18:11 +01:00
|
|
|
dispatch(reset('new-file'));
|
|
|
|
// dispatch({
|
|
|
|
// type: ActionTypes.HIDE_MODAL
|
|
|
|
// });
|
2016-10-24 22:42:15 +02:00
|
|
|
dispatch(setUnsavedChanges(true));
|
2020-03-24 23:07:58 +01:00
|
|
|
dispatch(closeNewFileModal());
|
2016-07-14 00:53:56 +02:00
|
|
|
}
|
2016-07-13 22:13:28 +02:00
|
|
|
};
|
|
|
|
}
|
2016-08-03 21:11:59 +02:00
|
|
|
|
2016-08-30 05:23:10 +02:00
|
|
|
export function createFolder(formProps) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2019-10-08 22:39:47 +02:00
|
|
|
const { parentId } = state.ide;
|
2016-08-30 05:23:10 +02:00
|
|
|
if (state.project.id) {
|
|
|
|
const postParams = {
|
2016-09-03 20:52:51 +02:00
|
|
|
name: createUniqueName(formProps.name, parentId, state.files),
|
2016-08-30 05:23:10 +02:00
|
|
|
content: '',
|
2016-08-30 20:39:37 +02:00
|
|
|
children: [],
|
|
|
|
parentId,
|
2016-08-30 05:23:10 +02:00
|
|
|
fileType: 'folder'
|
|
|
|
};
|
|
|
|
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
|
2017-02-22 20:29:35 +01:00
|
|
|
.then((response) => {
|
2016-08-30 05:23:10 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.CREATE_FILE,
|
2019-04-24 19:32:23 +02:00
|
|
|
...response.data.updatedFile,
|
2016-08-30 20:39:37 +02:00
|
|
|
parentId
|
2016-08-30 05:23:10 +02:00
|
|
|
});
|
2019-04-24 19:32:23 +02:00
|
|
|
dispatch(setProjectSavedTime(response.data.project.updatedAt));
|
2020-03-24 23:07:58 +01:00
|
|
|
dispatch(closeNewFolderModal());
|
2016-08-30 05:23:10 +02:00
|
|
|
})
|
2020-04-25 16:48:39 +02:00
|
|
|
.catch((error) => {
|
|
|
|
const { response } = error;
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
});
|
|
|
|
});
|
2016-08-30 05:23:10 +02:00
|
|
|
} else {
|
|
|
|
const id = objectID().toHexString();
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.CREATE_FILE,
|
2016-09-03 20:52:51 +02:00
|
|
|
name: createUniqueName(formProps.name, parentId, state.files),
|
2016-08-30 05:23:10 +02:00
|
|
|
id,
|
|
|
|
_id: id,
|
|
|
|
content: '',
|
|
|
|
// TODO pass parent id from File Tree
|
2016-08-30 20:39:37 +02:00
|
|
|
parentId,
|
|
|
|
fileType: 'folder',
|
|
|
|
children: []
|
2016-08-30 05:23:10 +02:00
|
|
|
});
|
2020-03-24 23:07:58 +01:00
|
|
|
dispatch(closeNewFolderModal());
|
2016-08-30 05:23:10 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-10 18:42:33 +02:00
|
|
|
export function updateFileName(id, name) {
|
2020-04-08 01:04:00 +02:00
|
|
|
return (dispatch) => {
|
2020-04-10 18:42:33 +02:00
|
|
|
dispatch(setUnsavedChanges(true));
|
2020-04-08 01:04:00 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.UPDATE_FILE_NAME,
|
|
|
|
id,
|
|
|
|
name
|
|
|
|
});
|
2016-08-03 21:11:59 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-24 22:06:28 +02:00
|
|
|
export function deleteFile(id, parentId) {
|
2016-08-24 23:59:15 +02:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2016-08-25 00:52:08 +02:00
|
|
|
if (state.project.id) {
|
|
|
|
const deleteConfig = {
|
|
|
|
params: {
|
2016-08-24 23:59:15 +02:00
|
|
|
parentId
|
2016-08-25 00:52:08 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
axios.delete(`${ROOT_URL}/projects/${state.project.id}/files/${id}`, deleteConfig, { withCredentials: true })
|
|
|
|
.then(() => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.DELETE_FILE,
|
|
|
|
id,
|
|
|
|
parentId
|
|
|
|
});
|
|
|
|
})
|
2020-04-25 16:48:39 +02:00
|
|
|
.catch((error) => {
|
|
|
|
const { response } = error;
|
2016-08-25 00:52:08 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
});
|
2016-08-24 23:59:15 +02:00
|
|
|
});
|
2016-08-25 00:52:08 +02:00
|
|
|
} else {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.DELETE_FILE,
|
|
|
|
id,
|
|
|
|
parentId
|
|
|
|
});
|
|
|
|
}
|
2016-08-03 21:11:59 +02:00
|
|
|
};
|
|
|
|
}
|
2016-08-31 00:46:59 +02:00
|
|
|
|
|
|
|
export function showFolderChildren(id) {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.SHOW_FOLDER_CHILDREN,
|
|
|
|
id
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hideFolderChildren(id) {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.HIDE_FOLDER_CHILDREN,
|
|
|
|
id
|
|
|
|
};
|
|
|
|
}
|
2016-10-22 22:42:43 +02:00
|
|
|
|
|
|
|
export function setBlobUrl(file, blobURL) {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.SET_BLOB_URL,
|
2019-02-25 18:57:10 +01:00
|
|
|
id: file.id,
|
2016-10-22 22:42:43 +02:00
|
|
|
blobURL
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getBlobUrl(file) {
|
|
|
|
if (file.blobUrl) {
|
|
|
|
blobUtil.revokeObjectURL(file.blobUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
const fileBlob = blobUtil.createBlob([file.content], { type: 'text/plain' });
|
|
|
|
const blobURL = blobUtil.createObjectURL(fileBlob);
|
|
|
|
return blobURL;
|
|
|
|
}
|