import PropTypes from 'prop-types'; import React, { Component } from 'react'; import { connect } from 'react-redux'; import { createSelector } from 'reselect'; import * as commandNames from 'Commands/commandNames'; import withCurrentPage from 'Components/withCurrentPage'; import * as blocklistActions from 'Store/Actions/blocklistActions'; import { executeCommand } from 'Store/Actions/commandActions'; import createCommandExecutingSelector from 'Store/Selectors/createCommandExecutingSelector'; import { registerPagePopulator, unregisterPagePopulator } from 'Utilities/pagePopulator'; import Blocklist from './Blocklist'; function createMapStateToProps() { return createSelector( (state) => state.blocklist, (state) => state.artist, createCommandExecutingSelector(commandNames.CLEAR_BLOCKLIST), (blocklist, artist, isClearingBlocklistExecuting) => { return { isArtistFetching: artist.isFetching, isArtistPopulated: artist.isPopulated, isClearingBlocklistExecuting, ...blocklist }; } ); } const mapDispatchToProps = { ...blocklistActions, executeCommand }; class BlocklistConnector extends Component { // // Lifecycle componentDidMount() { const { useCurrentPage, fetchBlocklist, gotoBlocklistFirstPage } = this.props; registerPagePopulator(this.repopulate); if (useCurrentPage) { fetchBlocklist(); } else { gotoBlocklistFirstPage(); } } componentDidUpdate(prevProps) { if (prevProps.isClearingBlocklistExecuting && !this.props.isClearingBlocklistExecuting) { this.props.gotoBlocklistFirstPage(); } } componentWillUnmount() { this.props.clearBlocklist(); unregisterPagePopulator(this.repopulate); } // // Control repopulate = () => { this.props.fetchBlocklist(); }; // // Listeners onFirstPagePress = () => { this.props.gotoBlocklistFirstPage(); }; onPreviousPagePress = () => { this.props.gotoBlocklistPreviousPage(); }; onNextPagePress = () => { this.props.gotoBlocklistNextPage(); }; onLastPagePress = () => { this.props.gotoBlocklistLastPage(); }; onPageSelect = (page) => { this.props.gotoBlocklistPage({ page }); }; onRemoveSelected = (ids) => { this.props.removeBlocklistItems({ ids }); }; onSortPress = (sortKey) => { this.props.setBlocklistSort({ sortKey }); }; onTableOptionChange = (payload) => { this.props.setBlocklistTableOption(payload); if (payload.pageSize) { this.props.gotoBlocklistFirstPage(); } }; onClearBlocklistPress = () => { this.props.executeCommand({ name: commandNames.CLEAR_BLOCKLIST }); }; // // Render render() { return ( ); } } BlocklistConnector.propTypes = { useCurrentPage: PropTypes.bool.isRequired, isClearingBlocklistExecuting: PropTypes.bool.isRequired, items: PropTypes.arrayOf(PropTypes.object).isRequired, fetchBlocklist: PropTypes.func.isRequired, gotoBlocklistFirstPage: PropTypes.func.isRequired, gotoBlocklistPreviousPage: PropTypes.func.isRequired, gotoBlocklistNextPage: PropTypes.func.isRequired, gotoBlocklistLastPage: PropTypes.func.isRequired, gotoBlocklistPage: PropTypes.func.isRequired, removeBlocklistItems: PropTypes.func.isRequired, setBlocklistSort: PropTypes.func.isRequired, setBlocklistTableOption: PropTypes.func.isRequired, clearBlocklist: PropTypes.func.isRequired, executeCommand: PropTypes.func.isRequired }; export default withCurrentPage( connect(createMapStateToProps, mapDispatchToProps)(BlocklistConnector) );