delete files, only client side

This commit is contained in:
catarak 2016-08-24 16:06:28 -04:00
parent acd5aa8c83
commit 5f694329db
3 changed files with 26 additions and 13 deletions

View file

@ -153,9 +153,10 @@ export function updateFileName(id, name) {
}; };
} }
export function deleteFile(id) { export function deleteFile(id, parentId) {
return { return {
type: ActionTypes.DELETE_FILE, type: ActionTypes.DELETE_FILE,
id id,
parentId
}; };
} }

View file

@ -105,7 +105,7 @@ export class FileNode extends React.Component {
<a <a
onClick={() => { onClick={() => {
if (window.confirm(`Are you sure you want to delete ${this.props.name}?`)) { 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(); this.props.resetSelectedFile();
} }
}} }}
@ -135,6 +135,7 @@ export class FileNode extends React.Component {
FileNode.propTypes = { FileNode.propTypes = {
id: PropTypes.string.isRequired, id: PropTypes.string.isRequired,
parentId: PropTypes.string,
children: PropTypes.array, children: PropTypes.array,
name: PropTypes.string.isRequired, name: PropTypes.string.isRequired,
isSelected: PropTypes.bool, isSelected: PropTypes.bool,

View file

@ -92,6 +92,7 @@ const files = (state, action) => {
case ActionTypes.RESET_PROJECT: case ActionTypes.RESET_PROJECT:
return initialState(); return initialState();
case ActionTypes.CREATE_FILE: // eslint-disable-line case ActionTypes.CREATE_FILE: // eslint-disable-line
{
const newState = state.map((file) => { const newState = state.map((file) => {
if (file.id === action.parentId) { if (file.id === action.parentId) {
const newFile = Object.assign({}, file); const newFile = Object.assign({}, file);
@ -101,6 +102,7 @@ const files = (state, action) => {
return file; return file;
}); });
return [...newState, { name: action.name, id: action.id, _id: action._id, content: action.content, url: action.url }]; return [...newState, { name: action.name, id: action.id, _id: action._id, content: action.content, url: action.url }];
}
case ActionTypes.SHOW_FILE_OPTIONS: case ActionTypes.SHOW_FILE_OPTIONS:
return state.map(file => { return state.map(file => {
if (file.id !== action.id) { if (file.id !== action.id) {
@ -126,7 +128,16 @@ const files = (state, action) => {
return Object.assign({}, file, { name: action.name }); return Object.assign({}, file, { name: action.name });
}); });
case ActionTypes.DELETE_FILE: 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: case ActionTypes.SHOW_EDIT_FILE_NAME:
return state.map(file => { return state.map(file => {
if (file.id !== action.id) { if (file.id !== action.id) {