From 9866b2dbd9284566e411871b3555a836cc1c7e11 Mon Sep 17 00:00:00 2001 From: Kushagra Gour Date: Thu, 4 Oct 2018 15:41:05 +0530 Subject: [PATCH] Add file removal --- src/components/ContentWrap2.jsx | 15 +++++++++------ src/components/SidePane.jsx | 29 ++++++++++++++++++++++++++--- src/components/app.jsx | 11 +++++++++++ src/style.css | 14 +++++++++++--- 4 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/components/ContentWrap2.jsx b/src/components/ContentWrap2.jsx index 40df847..798cd9f 100644 --- a/src/components/ContentWrap2.jsx +++ b/src/components/ContentWrap2.jsx @@ -52,14 +52,16 @@ export default class ContentWrap2 extends Component { } } componentDidUpdate() { + const { currentItem } = this.props; + + // Select a new file if nothing is selected already or the selected file exists no more. if ( - this.props.currentItem && - this.props.currentItem.files && - !this.state.selectedFile + currentItem && + currentItem.files && + (!this.state.selectedFile || + !currentItem.files.includes(this.state.selectedFile)) ) { - this.setState({ - selectedFile: this.props.currentItem.files[0] - }); + this.fileSelectHandler(this.props.currentItem.files[0]); } // HACK: becuase its a DOM manipulation // window.logCountEl.textContent = this.logCount; @@ -438,6 +440,7 @@ export default class ContentWrap2 extends Component { selectedFile={this.state.selectedFile} onFileSelect={this.fileSelectHandler.bind(this)} onAddFile={this.props.onAddFile} + onRemoveFile={this.props.onRemoveFile} />
diff --git a/src/components/SidePane.jsx b/src/components/SidePane.jsx index 4d92b2c..239d192 100644 --- a/src/components/SidePane.jsx +++ b/src/components/SidePane.jsx @@ -64,8 +64,15 @@ export class SidePane extends Component { this.setState({ isEditing: false }); } } + removeFileClickHandler(file, e) { + e.stopPropagation(); + const answer = confirm(`Are you sure you want to delete ${file.name}?`); + if (answer) { + this.props.onRemoveFile(file); + } + } render() { - const { files, onFileSelect, selectedFile } = this.props; + const { files, onFileSelect, selectedFile, onRemoveFile } = this.props; return ( ))} diff --git a/src/components/app.jsx b/src/components/app.jsx index a7419d1..8f2f3c9 100644 --- a/src/components/app.jsx +++ b/src/components/app.jsx @@ -1191,6 +1191,16 @@ export default class App extends Component { } }); } + removeFileHandler(fileToRemove) { + this.setState({ + currentItem: { + ...this.state.currentItem, + files: this.state.currentItem.files.filter( + file => file !== fileToRemove + ) + } + }); + } render() { return ( @@ -1222,6 +1232,7 @@ export default class App extends Component { onEditorFocus={this.editorFocusHandler.bind(this)} onSplitUpdate={this.splitUpdateHandler.bind(this)} onAddFile={this.addFileHandler.bind(this)} + onRemoveFile={this.removeFileHandler.bind(this)} />