From f2fedd8457098ec1e15f4a199cc2c95917dfef2a Mon Sep 17 00:00:00 2001 From: catarak Date: Fri, 2 Sep 2016 18:11:27 -0400 Subject: [PATCH] add file deletion, client side --- client/modules/IDE/actions/files.js | 8 ++++---- client/modules/IDE/reducers/files.js | 30 ++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/client/modules/IDE/actions/files.js b/client/modules/IDE/actions/files.js index 6c75fa59..3b8dbe61 100644 --- a/client/modules/IDE/actions/files.js +++ b/client/modules/IDE/actions/files.js @@ -82,8 +82,8 @@ export function createFile(formProps) { name: createUniqueName(formProps.name, state.files), url: formProps.url, content: formProps.content || '', - // TODO pass parent id to API, once there are folders - parentId + parentId, + children: [] }; axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true }) .then(response => { @@ -115,8 +115,8 @@ export function createFile(formProps) { _id: id, url: formProps.url, content: formProps.content || '', - // TODO pass parent id from File Tree - parentId + parentId, + children: [] }); dispatch({ type: ActionTypes.HIDE_MODAL diff --git a/client/modules/IDE/reducers/files.js b/client/modules/IDE/reducers/files.js index 8ada98c8..ffff6764 100644 --- a/client/modules/IDE/reducers/files.js +++ b/client/modules/IDE/reducers/files.js @@ -76,6 +76,18 @@ function getAllDescendantIds(state, nodeId) { ), []); } +function deleteChild(state, parentId, id) { + const newState = state.map((file) => { + if (file.id === parentId) { + const newFile = Object.assign({}, file); + newFile.children = newFile.children.filter(child => child !== id); + return newFile; + } + return file; + }); + return newState; +} + function deleteMany(state, ids) { const newState = [...state]; ids.forEach(id => { @@ -162,14 +174,16 @@ const files = (state, action) => { }); case ActionTypes.DELETE_FILE: { - 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); + const newState = deleteMany(state, [action.id, ...getAllDescendantIds(state, action.id)]); + return deleteChild(newState, action.parentId, 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 => {