Lidarr/frontend/src/Artist/Index/ArtistIndexConnector.js

167 lines
4.3 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';
import dimensions from 'Styles/Variables/dimensions';
import createCommandSelector from 'Store/Selectors/createCommandSelector';
import createDimensionsSelector from 'Store/Selectors/createDimensionsSelector';
import { fetchArtist } from 'Store/Actions/artistActions';
2017-09-04 02:20:56 +00:00
import scrollPositions from 'Store/scrollPositions';
import { setArtistSort, setArtistFilter, setArtistView } from 'Store/Actions/artistIndexActions';
import { executeCommand } from 'Store/Actions/commandActions';
import * as commandNames from 'Commands/commandNames';
import withScrollPosition from 'Components/withScrollPosition';
import ArtistIndex from './ArtistIndex';
const POSTERS_PADDING = 15;
const POSTERS_PADDING_SMALL_SCREEN = 5;
2017-09-17 06:24:15 +00:00
const BANNERS_PADDING = 15;
const BANNERS_PADDING_SMALL_SCREEN = 5;
2017-09-04 02:20:56 +00:00
const TABLE_PADDING = parseInt(dimensions.pageContentBodyPadding);
const TABLE_PADDING_SMALL_SCREEN = parseInt(dimensions.pageContentBodyPaddingSmallScreen);
// If the scrollTop is greater than zero it needs to be offset
// by the padding so when it is set initially so it is correct
// after React Virtualized takes the padding into account.
function getScrollTop(view, scrollTop, isSmallScreen) {
if (scrollTop === 0) {
return 0;
}
let padding = isSmallScreen ? TABLE_PADDING_SMALL_SCREEN : TABLE_PADDING;
if (view === 'posters') {
padding = isSmallScreen ? POSTERS_PADDING_SMALL_SCREEN : POSTERS_PADDING;
}
2017-09-17 06:24:15 +00:00
if (view === 'banners') {
padding = isSmallScreen ? BANNERS_PADDING_SMALL_SCREEN : BANNERS_PADDING;
}
2017-09-04 02:20:56 +00:00
return scrollTop + padding;
}
function createMapStateToProps() {
return createSelector(
(state) => state.series,
2017-09-16 20:22:06 +00:00
(state) => state.artistIndex,
createCommandSelector(commandNames.REFRESH_ARTIST),
2017-09-04 02:20:56 +00:00
createCommandSelector(commandNames.RSS_SYNC),
createDimensionsSelector(),
2017-09-16 20:22:06 +00:00
(series, artistIndex, isRefreshingArtist, isRssSyncExecuting, dimensionsState) => {
2017-09-04 02:20:56 +00:00
return {
2017-09-16 20:22:06 +00:00
isRefreshingArtist,
2017-09-04 02:20:56 +00:00
isRssSyncExecuting,
isSmallScreen: dimensionsState.isSmallScreen,
...series,
2017-09-16 20:22:06 +00:00
...artistIndex
2017-09-04 02:20:56 +00:00
};
}
);
}
const mapDispatchToProps = {
fetchArtist,
setArtistSort,
setArtistFilter,
setArtistView,
executeCommand
};
class ArtistIndexConnector extends Component {
//
// Lifecycle
constructor(props, context) {
super(props, context);
const {
view,
scrollTop,
isSmallScreen
} = props;
this.state = {
scrollTop: getScrollTop(view, scrollTop, isSmallScreen)
};
}
componentDidMount() {
this.props.fetchArtist();
}
//
// Listeners
onSortSelect = (sortKey) => {
this.props.setArtistSort({ sortKey });
}
onFilterSelect = (filterKey, filterValue, filterType) => {
this.props.setArtistFilter({ filterKey, filterValue, filterType });
}
onViewSelect = (view) => {
// Reset the scroll position before changing the view
this.setState({ scrollTop: 0 }, () => {
this.props.setArtistView({ view });
});
}
onScroll = ({ scrollTop }) => {
this.setState({
scrollTop
}, () => {
2017-09-16 20:22:06 +00:00
scrollPositions.artistIndex = scrollTop;
2017-09-04 02:20:56 +00:00
});
}
onRefreshArtistPress = () => {
2017-09-04 02:20:56 +00:00
this.props.executeCommand({
name: commandNames.REFRESH_ARTIST
2017-09-04 02:20:56 +00:00
});
}
onRssSyncPress = () => {
this.props.executeCommand({
name: commandNames.RSS_SYNC
});
}
//
// Render
render() {
return (
<ArtistIndex
{...this.props}
scrollTop={this.state.scrollTop}
onSortSelect={this.onSortSelect}
onFilterSelect={this.onFilterSelect}
onViewSelect={this.onViewSelect}
onScroll={this.onScroll}
onRefreshArtistPress={this.onRefreshArtistPress}
2017-09-04 02:20:56 +00:00
onRssSyncPress={this.onRssSyncPress}
/>
);
}
}
ArtistIndexConnector.propTypes = {
2017-10-07 06:21:06 +00:00
isSmallScreen: PropTypes.bool.isRequired,
2017-09-04 02:20:56 +00:00
view: PropTypes.string.isRequired,
scrollTop: PropTypes.number.isRequired,
fetchArtist: PropTypes.func.isRequired,
setArtistSort: PropTypes.func.isRequired,
setArtistFilter: PropTypes.func.isRequired,
setArtistView: PropTypes.func.isRequired,
executeCommand: PropTypes.func.isRequired
};
export default withScrollPosition(
connect(createMapStateToProps, mapDispatchToProps)(ArtistIndexConnector),
2017-09-16 20:22:06 +00:00
'artistIndex'
2017-09-04 02:20:56 +00:00
);