Last active
December 2, 2015 17:57
-
-
Save arackaf/424caaf1d3af5a75b393 to your computer and use it in GitHub Desktop.
Desktop React component with state-based modal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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