Lidarr/frontend/src/Activity/Blocklist/BlocklistConnector.js

156 lines
4.0 KiB
JavaScript
Raw Normal View History

2017-09-04 02:20:56 +00:00
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { createSelector } from 'reselect';
2020-09-07 01:33:10 +00:00
import * as commandNames from 'Commands/commandNames';
import withCurrentPage from 'Components/withCurrentPage';
2021-08-19 21:35:06 +00:00
import * as blocklistActions from 'Store/Actions/blocklistActions';
2017-09-04 02:20:56 +00:00
import { executeCommand } from 'Store/Actions/commandActions';
2020-09-07 01:33:10 +00:00
import createCommandExecutingSelector from 'Store/Selectors/createCommandExecutingSelector';
import { registerPagePopulator, unregisterPagePopulator } from 'Utilities/pagePopulator';
2021-08-19 21:35:06 +00:00
import Blocklist from './Blocklist';
2017-09-04 02:20:56 +00:00
function createMapStateToProps() {
return createSelector(
2021-08-19 21:35:06 +00:00
(state) => state.blocklist,
(state) => state.artist,
2021-08-19 21:35:06 +00:00
createCommandExecutingSelector(commandNames.CLEAR_BLOCKLIST),
(blocklist, artist, isClearingBlocklistExecuting) => {
2017-09-04 02:20:56 +00:00
return {
isArtistFetching: artist.isFetching,
isArtistPopulated: artist.isPopulated,
2021-08-19 21:35:06 +00:00
isClearingBlocklistExecuting,
...blocklist
2017-09-04 02:20:56 +00:00
};
}
);
}
const mapDispatchToProps = {
2021-08-19 21:35:06 +00:00
...blocklistActions,
2017-09-04 02:20:56 +00:00
executeCommand
};
2021-08-19 21:35:06 +00:00
class BlocklistConnector extends Component {
2017-09-04 02:20:56 +00:00
//
// Lifecycle
componentDidMount() {
const {
useCurrentPage,
2021-08-19 21:35:06 +00:00
fetchBlocklist,
gotoBlocklistFirstPage
} = this.props;
registerPagePopulator(this.repopulate);
if (useCurrentPage) {
2021-08-19 21:35:06 +00:00
fetchBlocklist();
} else {
2021-08-19 21:35:06 +00:00
gotoBlocklistFirstPage();
}
2017-09-04 02:20:56 +00:00
}
componentDidUpdate(prevProps) {
2021-08-19 21:35:06 +00:00
if (prevProps.isClearingBlocklistExecuting && !this.props.isClearingBlocklistExecuting) {
this.props.gotoBlocklistFirstPage();
2017-09-04 02:20:56 +00:00
}
}
2017-10-07 06:47:28 +00:00
componentWillUnmount() {
2021-08-19 21:35:06 +00:00
this.props.clearBlocklist();
2017-10-07 06:47:28 +00:00
unregisterPagePopulator(this.repopulate);
}
//
// Control
repopulate = () => {
2021-08-19 21:35:06 +00:00
this.props.fetchBlocklist();
2021-12-24 18:18:14 +00:00
};
2017-09-04 02:20:56 +00:00
//
// Listeners
onFirstPagePress = () => {
2021-08-19 21:35:06 +00:00
this.props.gotoBlocklistFirstPage();
2021-12-24 18:18:14 +00:00
};
2017-09-04 02:20:56 +00:00
onPreviousPagePress = () => {
2021-08-19 21:35:06 +00:00
this.props.gotoBlocklistPreviousPage();
2021-12-24 18:18:14 +00:00
};
2017-09-04 02:20:56 +00:00
onNextPagePress = () => {
2021-08-19 21:35:06 +00:00
this.props.gotoBlocklistNextPage();
2021-12-24 18:18:14 +00:00
};
2017-09-04 02:20:56 +00:00
onLastPagePress = () => {
2021-08-19 21:35:06 +00:00
this.props.gotoBlocklistLastPage();
2021-12-24 18:18:14 +00:00
};
2017-09-04 02:20:56 +00:00
onPageSelect = (page) => {
2021-08-19 21:35:06 +00:00
this.props.gotoBlocklistPage({ page });
2021-12-24 18:18:14 +00:00
};
2017-09-04 02:20:56 +00:00
2020-10-11 22:28:32 +00:00
onRemoveSelected = (ids) => {
2021-08-19 21:35:06 +00:00
this.props.removeBlocklistItems({ ids });
2021-12-24 18:18:14 +00:00
};
2020-10-11 22:28:32 +00:00
2017-09-04 02:20:56 +00:00
onSortPress = (sortKey) => {
2021-08-19 21:35:06 +00:00
this.props.setBlocklistSort({ sortKey });
2021-12-24 18:18:14 +00:00
};
2017-09-04 02:20:56 +00:00
onTableOptionChange = (payload) => {
2021-08-19 21:35:06 +00:00
this.props.setBlocklistTableOption(payload);
2017-09-04 02:20:56 +00:00
if (payload.pageSize) {
2021-08-19 21:35:06 +00:00
this.props.gotoBlocklistFirstPage();
2017-09-04 02:20:56 +00:00
}
2021-12-24 18:18:14 +00:00
};
2017-09-04 02:20:56 +00:00
2021-08-19 21:35:06 +00:00
onClearBlocklistPress = () => {
this.props.executeCommand({ name: commandNames.CLEAR_BLOCKLIST });
2021-12-24 18:18:14 +00:00
};
2017-09-04 02:20:56 +00:00
//
// Render
render() {
return (
2021-08-19 21:35:06 +00:00
<Blocklist
2017-09-04 02:20:56 +00:00
onFirstPagePress={this.onFirstPagePress}
onPreviousPagePress={this.onPreviousPagePress}
onNextPagePress={this.onNextPagePress}
onLastPagePress={this.onLastPagePress}
onPageSelect={this.onPageSelect}
2020-10-11 22:28:32 +00:00
onRemoveSelected={this.onRemoveSelected}
2017-09-04 02:20:56 +00:00
onSortPress={this.onSortPress}
onTableOptionChange={this.onTableOptionChange}
2021-08-19 21:35:06 +00:00
onClearBlocklistPress={this.onClearBlocklistPress}
2017-09-04 02:20:56 +00:00
{...this.props}
/>
);
}
}
2021-08-19 21:35:06 +00:00
BlocklistConnector.propTypes = {
useCurrentPage: PropTypes.bool.isRequired,
2021-08-19 21:35:06 +00:00
isClearingBlocklistExecuting: PropTypes.bool.isRequired,
2017-09-04 02:20:56 +00:00
items: PropTypes.arrayOf(PropTypes.object).isRequired,
2021-08-19 21:35:06 +00:00
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,
2017-09-04 02:20:56 +00:00
executeCommand: PropTypes.func.isRequired
};
export default withCurrentPage(
2021-08-19 21:35:06 +00:00
connect(createMapStateToProps, mapDispatchToProps)(BlocklistConnector)
);