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),
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

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) {
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 => {