add file deletion, client side

This commit is contained in:
catarak 2016-09-02 18:11:27 -04:00
parent 18839fde81
commit f2fedd8457
2 changed files with 26 additions and 12 deletions

View file

@ -82,8 +82,8 @@ export function createFile(formProps) {
name: createUniqueName(formProps.name, state.files), name: createUniqueName(formProps.name, state.files),
url: formProps.url, url: formProps.url,
content: formProps.content || '', 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 }) axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
.then(response => { .then(response => {
@ -115,8 +115,8 @@ export function createFile(formProps) {
_id: id, _id: id,
url: formProps.url, url: formProps.url,
content: formProps.content || '', content: formProps.content || '',
// TODO pass parent id from File Tree parentId,
parentId children: []
}); });
dispatch({ dispatch({
type: ActionTypes.HIDE_MODAL type: ActionTypes.HIDE_MODAL

View file

@ -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) { function deleteMany(state, ids) {
const newState = [...state]; const newState = [...state];
ids.forEach(id => { ids.forEach(id => {
@ -162,14 +174,16 @@ const files = (state, action) => {
}); });
case ActionTypes.DELETE_FILE: case ActionTypes.DELETE_FILE:
{ {
const newState = state.map((file) => { const newState = deleteMany(state, [action.id, ...getAllDescendantIds(state, action.id)]);
if (file.id === action.parentId) { return deleteChild(newState, action.parentId, action.id);
const newChildren = file.children.filter(child => child !== action.id); // const newState = state.map((file) => {
return { ...file, children: newChildren }; // if (file.id === action.parentId) {
} // const newChildren = file.children.filter(child => child !== action.id);
return file; // return { ...file, children: newChildren };
}); // }
return newState.filter(file => file.id !== action.id); // return file;
// });
// return newState.filter(file => file.id !== action.id);
} }
case ActionTypes.SHOW_EDIT_FILE_NAME: case ActionTypes.SHOW_EDIT_FILE_NAME:
return state.map(file => { return state.map(file => {