Skip to content

Instantly share code, notes, and snippets.

@arackaf
Last active December 2, 2015 17:57
Show Gist options
  • Save arackaf/424caaf1d3af5a75b393 to your computer and use it in GitHub Desktop.
Save arackaf/424caaf1d3af5a75b393 to your computer and use it in GitHub Desktop.
Desktop React component with state-based modal
const editSubjects = Symbol('editSubjectStateCollection');
class BookViewListDesktop extends React.Component{
constructor(){
super();
this.state = {
booksSubjectsModalShown: false,
editSubjectsFor: [],
subjectsAdding: [],
subjectsRemoving: []
};
}
closeEditBooksSubjectsModal(){
this.setState({ booksSubjectsModalShown: false });
}
singleBookSubjectsModal(book){
this.setState({
booksSubjectsModalShown: true,
editSubjectsFor: [book],
subjectsAdding: [],
subjectsRemoving: []
});
}
multiBookSubjectsModal(){
this.setState({
booksSubjectsModalShown: true,
editSubjectsFor: this.props.bookList.filter(b => b.selected),
subjectsAdding: [],
subjectsRemoving: []
});
}
toggleAddSubjectPending(subject, toggledOn){
this[editSubjects](subject, toggledOn, 'subjectsAdding');
}
toggleRemoveSubjectPending(subject, toggledOn){
this[editSubjects](subject, toggledOn, 'subjectsRemoving');
}
[editSubjects](subject, toggledOn, stateName){
let updated = this.state[stateName].concat();
if (toggledOn){
updated.push(subject);
} else {
updated = updated.filter(s => s._id !== subject._id);
}
this.setState({ [stateName]: updated });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment