From cd0dcc918480a343dcb5312c7b9bad89537b9bad Mon Sep 17 00:00:00 2001 From: catarak Date: Wed, 24 Aug 2016 18:52:08 -0400 Subject: [PATCH] reset selected file after deleting --- client/modules/IDE/actions/files.js | 41 ++++++++++++++--------- client/modules/IDE/actions/ide.js | 8 +++-- client/modules/IDE/components/FileNode.js | 9 +++-- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/client/modules/IDE/actions/files.js b/client/modules/IDE/actions/files.js index e5ffa68d..638aca56 100644 --- a/client/modules/IDE/actions/files.js +++ b/client/modules/IDE/actions/files.js @@ -157,23 +157,32 @@ export function updateFileName(id, name) { export function deleteFile(id, parentId) { return (dispatch, getState) => { const state = getState(); - const deleteConfig = { - params: { - parentId - } - }; - axios.delete(`${ROOT_URL}/projects/${state.project.id}/files/${id}`, deleteConfig, { withCredentials: true }) - .then(() => { - dispatch({ - type: ActionTypes.DELETE_FILE, - id, + if (state.project.id) { + const deleteConfig = { + params: { parentId + } + }; + axios.delete(`${ROOT_URL}/projects/${state.project.id}/files/${id}`, deleteConfig, { withCredentials: true }) + .then(() => { + dispatch({ + type: ActionTypes.DELETE_FILE, + id, + parentId + }); + }) + .catch(response => { + dispatch({ + type: ActionTypes.ERROR, + error: response.data + }); }); - }) - .catch(response => dispatch({ - type: ActionTypes.ERROR, - error: response.data - }) - ); + } else { + dispatch({ + type: ActionTypes.DELETE_FILE, + id, + parentId + }); + } }; } diff --git a/client/modules/IDE/actions/ide.js b/client/modules/IDE/actions/ide.js index b758c915..da636d05 100644 --- a/client/modules/IDE/actions/ide.js +++ b/client/modules/IDE/actions/ide.js @@ -37,10 +37,14 @@ export function setSelectedFile(fileId) { }; } -export function resetSelectedFile() { +export function resetSelectedFile(previousId) { return (dispatch, getState) => { const state = getState(); - setSelectedFile(state.files[1].id); + const newId = state.files.find(file => file.name !== 'root' && file.id !== previousId).id; + dispatch({ + type: ActionTypes.SET_SELECTED_FILE, + selectedFile: newId + }); }; } diff --git a/client/modules/IDE/components/FileNode.js b/client/modules/IDE/components/FileNode.js index 2ab8eb64..6048aefa 100644 --- a/client/modules/IDE/components/FileNode.js +++ b/client/modules/IDE/components/FileNode.js @@ -19,7 +19,9 @@ export class FileNode extends React.Component { handleFileClick(e) { e.stopPropagation(); - this.props.setSelectedFile(this.props.id); + if (!this.isDeleting) { + this.props.setSelectedFile(this.props.id); + } } handleFileNameChange(event) { @@ -105,8 +107,9 @@ export class FileNode extends React.Component { { if (window.confirm(`Are you sure you want to delete ${this.props.name}?`)) { - this.props.deleteFile(this.props.id, this.props.parentId); - this.props.resetSelectedFile(); + this.isDeleting = true; + this.props.resetSelectedFile(this.props.id); + setTimeout(() => this.props.deleteFile(this.props.id, this.props.parentId), 0); } }} >