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 { executeCommand } from 'Store/Actions/commandActions'; import { clearQueueDetails, fetchQueueDetails } from 'Store/Actions/queueActions'; import * as wantedActions from 'Store/Actions/wantedActions'; import createCommandExecutingSelector from 'Store/Selectors/createCommandExecutingSelector'; import hasDifferentItems from 'Utilities/Object/hasDifferentItems'; import selectUniqueIds from 'Utilities/Object/selectUniqueIds'; import { registerPagePopulator, unregisterPagePopulator } from 'Utilities/pagePopulator'; import Missing from './Missing'; function createMapStateToProps() { return createSelector( (state) => state.wanted.missing, (state) => state.authors, createCommandExecutingSelector(commandNames.MISSING_BOOK_SEARCH), (missing, authors, isSearchingForMissingBooks) => { return { isAuthorFetching: authors.isFetching, isAuthorPopulated: authors.isPopulated, isSearchingForMissingBooks, isSaving: missing.items.filter((m) => m.isSaving).length > 1, ...missing }; } ); } const mapDispatchToProps = { ...wantedActions, executeCommand, fetchQueueDetails, clearQueueDetails }; class MissingConnector extends Component { // // Lifecycle componentDidMount() { const { useCurrentPage, fetchMissing, gotoMissingFirstPage } = this.props; registerPagePopulator(this.repopulate, ['bookFileUpdated', 'bookFileDeleted']); if (useCurrentPage) { fetchMissing(); } else { gotoMissingFirstPage(); } } componentDidUpdate(prevProps) { if (hasDifferentItems(prevProps.items, this.props.items)) { const bookIds = selectUniqueIds(this.props.items, 'id'); this.props.fetchQueueDetails({ bookIds }); } } componentWillUnmount() { unregisterPagePopulator(this.repopulate); this.props.clearMissing(); this.props.clearQueueDetails(); } // // Control repopulate = () => { this.props.fetchMissing(); }; // // Listeners onFirstPagePress = () => { this.props.gotoMissingFirstPage(); }; onPreviousPagePress = () => { this.props.gotoMissingPreviousPage(); }; onNextPagePress = () => { this.props.gotoMissingNextPage(); }; onLastPagePress = () => { this.props.gotoMissingLastPage(); }; onPageSelect = (page) => { this.props.gotoMissingPage({ page }); }; onSortPress = (sortKey) => { this.props.setMissingSort({ sortKey }); }; onFilterSelect = (selectedFilterKey) => { this.props.setMissingFilter({ selectedFilterKey }); }; onTableOptionChange = (payload) => { this.props.setMissingTableOption(payload); if (payload.pageSize) { this.props.gotoMissingFirstPage(); } }; onSearchSelectedPress = (selected) => { this.props.executeCommand({ name: commandNames.BOOK_SEARCH, bookIds: selected }); }; onSearchAllMissingPress = () => { this.props.executeCommand({ name: commandNames.MISSING_BOOK_SEARCH }); }; // // Render render() { return ( ); } } MissingConnector.propTypes = { useCurrentPage: PropTypes.bool.isRequired, items: PropTypes.arrayOf(PropTypes.object).isRequired, fetchMissing: PropTypes.func.isRequired, gotoMissingFirstPage: PropTypes.func.isRequired, gotoMissingPreviousPage: PropTypes.func.isRequired, gotoMissingNextPage: PropTypes.func.isRequired, gotoMissingLastPage: PropTypes.func.isRequired, gotoMissingPage: PropTypes.func.isRequired, setMissingSort: PropTypes.func.isRequired, setMissingFilter: PropTypes.func.isRequired, setMissingTableOption: PropTypes.func.isRequired, clearMissing: PropTypes.func.isRequired, executeCommand: PropTypes.func.isRequired, fetchQueueDetails: PropTypes.func.isRequired, clearQueueDetails: PropTypes.func.isRequired }; export default withCurrentPage( connect(createMapStateToProps, mapDispatchToProps)(MissingConnector) );