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,6 +157,7 @@ export function updateFileName(id, name) {
export function deleteFile(id, parentId) { export function deleteFile(id, parentId) {
return (dispatch, getState) => { return (dispatch, getState) => {
const state = getState(); const state = getState();
if (state.project.id) {
const deleteConfig = { const deleteConfig = {
params: { params: {
parentId parentId
@ -170,10 +171,18 @@ export function deleteFile(id, parentId) {
parentId parentId
}); });
}) })
.catch(response => dispatch({ .catch(response => {
dispatch({
type: ActionTypes.ERROR, type: ActionTypes.ERROR,
error: response.data 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) => { return (dispatch, getState) => {
const state = 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,8 +19,10 @@ export class FileNode extends React.Component {
handleFileClick(e) { handleFileClick(e) {
e.stopPropagation(); e.stopPropagation();
if (!this.isDeleting) {
this.props.setSelectedFile(this.props.id); this.props.setSelectedFile(this.props.id);
} }
}
handleFileNameChange(event) { handleFileNameChange(event) {
this.props.updateFileName(this.props.id, event.target.value); this.props.updateFileName(this.props.id, event.target.value);
@ -105,8 +107,9 @@ 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.parentId); this.isDeleting = true;
this.props.resetSelectedFile(); this.props.resetSelectedFile(this.props.id);
setTimeout(() => this.props.deleteFile(this.props.id, this.props.parentId), 0);
} }
}} }}
> >