diff --git a/client/modules/IDE/actions/files.js b/client/modules/IDE/actions/files.js index b436034ea9..dffd109c74 100644 --- a/client/modules/IDE/actions/files.js +++ b/client/modules/IDE/actions/files.js @@ -26,6 +26,7 @@ function createUniqueName(name, parentId, files) { index += 1; existingName = siblingFiles.find((file) => testName === file.name); // eslint-disable-line } + console.log(testName); return testName; } @@ -43,14 +44,68 @@ export function createFile(formProps) { const selectedFile = state.files.find(file => file.isSelectedFile); const rootFile = state.files.find(file => file.name === 'root'); let parentId; + const relativePath = formProps.name; + const relativePathSplit = relativePath.split('/'); + const parentFolders = relativePath.length === 1 ? [] : relativePathSplit.slice(0, -1); + const fileName = relativePathSplit.slice(-1)[0]; if (selectedFile.fileType === 'folder') { parentId = selectedFile.id; } else { parentId = rootFile.id; } + parentId = parentFolders.reduce((parentFolderId, currentFolderName) => { + const { files } = state; + const parentFolderObject = files.find(file => file.id === parentFolderId); + const childFoldersObject = parentFolderObject.children + .map(childFileId => files.find(file => file.id === childFileId)); + const currentFolderObject = childFoldersObject.find(childFile => childFile.name === currentFolderName); + const currentFolderId = currentFolderObject ? currentFolderObject.id : undefined; + + if (!currentFolderId) { + if (state.project.id) { + const postParams = { + name: createUniqueName(currentFolderName, parentFolderId, getState().files), + content: '', + children: [], + parentId: parentFolderId, + fileType: 'folder' + }; + axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true }) + .then((response) => { + dispatch({ + type: ActionTypes.CREATE_FILE, + ...response.data, + parentId: parentFolderId + }); + }) + .catch(response => dispatch({ + type: ActionTypes.ERROR, + error: response.data + })); + /* return __; */ + } else { + const id = objectID().toHexString(); + dispatch({ + type: ActionTypes.CREATE_FILE, + name: createUniqueName(currentFolderName, parentFolderId, getState().files), + /* name: currentFolderName, */ + id, + _id: id, + content: '', + // TODO pass parent id from File Tree + parentId: parentFolderId, + fileType: 'folder', + children: [] + }); + return id; + } + } + return currentFolderId; + }, parentId); + console.log(parentId); if (state.project.id) { const postParams = { - name: createUniqueName(formProps.name, parentId, state.files), + name: createUniqueName(fileName, parentId, getState().files), url: formProps.url, content: formProps.content || '', parentId, @@ -77,7 +132,7 @@ export function createFile(formProps) { const id = objectID().toHexString(); dispatch({ type: ActionTypes.CREATE_FILE, - name: createUniqueName(formProps.name, parentId, state.files), + name: createUniqueName(fileName, parentId, getState().files), id, _id: id, url: formProps.url, @@ -100,14 +155,66 @@ export function createFolder(formProps) { const selectedFile = state.files.find(file => file.isSelectedFile); const rootFile = state.files.find(file => file.name === 'root'); let parentId; + const relativePath = formProps.name; + const relativePathSplit = relativePath.split('/'); + const parentFolders = relativePath.length === 1 ? [] : relativePathSplit.slice(0, -1); + const fileName = relativePathSplit.slice(-1)[0]; if (selectedFile.fileType === 'folder') { parentId = selectedFile.id; } else { parentId = rootFile.id; } + parentId = parentFolders.reduce((parentFolderId, currentFolderName) => { + const { files } = state; + const parentFolderObject = files.find(file => file.id === parentFolderId); + const childFoldersObject = parentFolderObject.children + .map(childFileId => files.find(file => file.id === childFileId)); + const currentFolderObject = childFoldersObject.find(childFile => childFile.name === currentFolderName); + const currentFolderId = currentFolderObject ? currentFolderObject.id : undefined; + + if (!currentFolderId) { + if (state.project.id) { + const postParams = { + name: createUniqueName(currentFolderName, parentFolderId, getState().files), + content: '', + children: [], + parentId: parentFolderId, + fileType: 'folder' + }; + axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true }) + .then((response) => { + dispatch({ + type: ActionTypes.CREATE_FILE, + ...response.data, + parentId: parentFolderId + }); + }) + .catch(response => dispatch({ + type: ActionTypes.ERROR, + error: response.data + })); + /* return __; */ + } else { + const id = objectID().toHexString(); + dispatch({ + type: ActionTypes.CREATE_FILE, + name: createUniqueName(currentFolderName, parentFolderId, getState().files), + id, + _id: id, + content: '', + // TODO pass parent id from File Tree + parentId: parentFolderId, + fileType: 'folder', + children: [] + }); + return id; + } + } + return currentFolderId; + }, parentId); if (state.project.id) { const postParams = { - name: createUniqueName(formProps.name, parentId, state.files), + name: createUniqueName(fileName, parentId, getState().files), content: '', children: [], parentId, @@ -132,7 +239,7 @@ export function createFolder(formProps) { const id = objectID().toHexString(); dispatch({ type: ActionTypes.CREATE_FILE, - name: createUniqueName(formProps.name, parentId, state.files), + name: createUniqueName(fileName, parentId, getState().files), id, _id: id, content: '',