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) { export function deleteFile(id, parentId) {
return (dispatch, getState) => { return (dispatch, getState) => {
const state = getState(); const state = getState();
const deleteConfig = { if (state.project.id) {
params: { const deleteConfig = {
parentId params: {
}
};
axios.delete(`${ROOT_URL}/projects/${state.project.id}/files/${id}`, deleteConfig, { withCredentials: true })
.then(() => {
dispatch({
type: ActionTypes.DELETE_FILE,
id,
parentId 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
});
}); });
}) } else {
.catch(response => dispatch({ dispatch({
type: ActionTypes.ERROR, type: ActionTypes.DELETE_FILE,
error: response.data 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,7 +19,9 @@ export class FileNode extends React.Component {
handleFileClick(e) { handleFileClick(e) {
e.stopPropagation(); e.stopPropagation();
this.props.setSelectedFile(this.props.id); if (!this.isDeleting) {
this.props.setSelectedFile(this.props.id);
}
} }
handleFileNameChange(event) { handleFileNameChange(event) {
@ -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);
} }
}} }}
> >