From 5f694329db1d25804177a73cdfc185dcf18ea43c Mon Sep 17 00:00:00 2001 From: catarak Date: Wed, 24 Aug 2016 16:06:28 -0400 Subject: [PATCH] delete files, only client side --- client/modules/IDE/actions/files.js | 5 ++-- client/modules/IDE/components/FileNode.js | 3 ++- client/modules/IDE/reducers/files.js | 31 +++++++++++++++-------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/client/modules/IDE/actions/files.js b/client/modules/IDE/actions/files.js index 21b7d779..03d44d6c 100644 --- a/client/modules/IDE/actions/files.js +++ b/client/modules/IDE/actions/files.js @@ -153,9 +153,10 @@ export function updateFileName(id, name) { }; } -export function deleteFile(id) { +export function deleteFile(id, parentId) { return { type: ActionTypes.DELETE_FILE, - id + id, + parentId }; } diff --git a/client/modules/IDE/components/FileNode.js b/client/modules/IDE/components/FileNode.js index 60d5bd02..2ab8eb64 100644 --- a/client/modules/IDE/components/FileNode.js +++ b/client/modules/IDE/components/FileNode.js @@ -105,7 +105,7 @@ 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.deleteFile(this.props.id, this.props.parentId); this.props.resetSelectedFile(); } }} @@ -135,6 +135,7 @@ export class FileNode extends React.Component { FileNode.propTypes = { id: PropTypes.string.isRequired, + parentId: PropTypes.string, children: PropTypes.array, name: PropTypes.string.isRequired, isSelected: PropTypes.bool, diff --git a/client/modules/IDE/reducers/files.js b/client/modules/IDE/reducers/files.js index 01bc755a..52e9b889 100644 --- a/client/modules/IDE/reducers/files.js +++ b/client/modules/IDE/reducers/files.js @@ -92,15 +92,17 @@ const files = (state, action) => { case ActionTypes.RESET_PROJECT: return initialState(); case ActionTypes.CREATE_FILE: // eslint-disable-line - const newState = state.map((file) => { - if (file.id === action.parentId) { - const newFile = Object.assign({}, file); - newFile.children = [...newFile.children, action.id]; - return newFile; - } - return file; - }); - return [...newState, { name: action.name, id: action.id, _id: action._id, content: action.content, url: action.url }]; + { + const newState = state.map((file) => { + if (file.id === action.parentId) { + const newFile = Object.assign({}, file); + newFile.children = [...newFile.children, action.id]; + return newFile; + } + return file; + }); + return [...newState, { name: action.name, id: action.id, _id: action._id, content: action.content, url: action.url }]; + } case ActionTypes.SHOW_FILE_OPTIONS: return state.map(file => { if (file.id !== action.id) { @@ -126,7 +128,16 @@ const files = (state, action) => { return Object.assign({}, file, { name: action.name }); }); case ActionTypes.DELETE_FILE: - return state.filter(file => file.id !== action.id); + { + const newState = state.map((file) => { + if (file.id === action.parentId) { + const newChildren = file.children.filter(child => child !== action.id); + return { ...file, children: newChildren }; + } + return file; + }); + return newState.filter(file => file.id !== action.id); + } case ActionTypes.SHOW_EDIT_FILE_NAME: return state.map(file => { if (file.id !== action.id) {