reset selected file after deleting

This commit is contained in:
catarak 2016-08-24 18:52:08 -04:00
parent 3dfed2a11e
commit cd0dcc9184
3 changed files with 37 additions and 21 deletions

View File

@ -157,23 +157,32 @@ export function updateFileName(id, name) {
export function deleteFile(id, parentId) {
return (dispatch, getState) => {
const state = getState();
const deleteConfig = {
params: {
parentId
}
};
axios.delete(`${ROOT_URL}/projects/${state.project.id}/files/${id}`, deleteConfig, { withCredentials: true })
.then(() => {
dispatch({
type: ActionTypes.DELETE_FILE,
id,
if (state.project.id) {
const deleteConfig = {
params: {
parentId
}
};
axios.delete(`${ROOT_URL}/projects/${state.project.id}/files/${id}`, deleteConfig, { withCredentials: true })
.then(() => {
dispatch({
type: ActionTypes.DELETE_FILE,
id,
parentId
});
})
.catch(response => {
dispatch({
type: ActionTypes.ERROR,
error: response.data
});
});
})
.catch(response => dispatch({
type: ActionTypes.ERROR,
error: response.data
})
);
} else {
dispatch({
type: ActionTypes.DELETE_FILE,
id,
parentId
});
}
};
}

View File

@ -37,10 +37,14 @@ export function setSelectedFile(fileId) {
};
}
export function resetSelectedFile() {
export function resetSelectedFile(previousId) {
return (dispatch, getState) => {
const state = getState();
setSelectedFile(state.files[1].id);
const newId = state.files.find(file => file.name !== 'root' && file.id !== previousId).id;
dispatch({
type: ActionTypes.SET_SELECTED_FILE,
selectedFile: newId
});
};
}

View File

@ -19,7 +19,9 @@ export class FileNode extends React.Component {
handleFileClick(e) {
e.stopPropagation();
this.props.setSelectedFile(this.props.id);
if (!this.isDeleting) {
this.props.setSelectedFile(this.props.id);
}
}
handleFileNameChange(event) {
@ -105,8 +107,9 @@ export class FileNode extends React.Component {
<a
onClick={() => {
if (window.confirm(`Are you sure you want to delete ${this.props.name}?`)) {
this.props.deleteFile(this.props.id, this.props.parentId);
this.props.resetSelectedFile();
this.isDeleting = true;
this.props.resetSelectedFile(this.props.id);
setTimeout(() => this.props.deleteFile(this.props.id, this.props.parentId), 0);
}
}}
>