import _ from 'lodash'; import PropTypes from 'prop-types'; import React, { Component } from 'react'; import hasDifferentItemsOrOrder from 'Utilities/Object/hasDifferentItemsOrOrder'; import { align, icons, sortDirections } from 'Helpers/Props'; import LoadingIndicator from 'Components/Loading/LoadingIndicator'; import PageContent from 'Components/Page/PageContent'; import PageContentBodyConnector from 'Components/Page/PageContentBodyConnector'; import PageJumpBar from 'Components/Page/PageJumpBar'; import TableOptionsModalWrapper from 'Components/Table/TableOptions/TableOptionsModalWrapper'; import PageToolbar from 'Components/Page/Toolbar/PageToolbar'; import PageToolbarSeparator from 'Components/Page/Toolbar/PageToolbarSeparator'; import PageToolbarSection from 'Components/Page/Toolbar/PageToolbarSection'; import PageToolbarButton from 'Components/Page/Toolbar/PageToolbarButton'; import NoSeries from 'Series/NoSeries'; import SeriesIndexTableConnector from './Table/SeriesIndexTableConnector'; import SeriesIndexTableOptionsConnector from './Table/SeriesIndexTableOptionsConnector'; import SeriesIndexPosterOptionsModal from './Posters/Options/SeriesIndexPosterOptionsModal'; import SeriesIndexPostersConnector from './Posters/SeriesIndexPostersConnector'; import SeriesIndexOverviewOptionsModal from './Overview/Options/SeriesIndexOverviewOptionsModal'; import SeriesIndexOverviewsConnector from './Overview/SeriesIndexOverviewsConnector'; import SeriesIndexFilterMenu from './Menus/SeriesIndexFilterMenu'; import SeriesIndexSortMenu from './Menus/SeriesIndexSortMenu'; import SeriesIndexViewMenu from './Menus/SeriesIndexViewMenu'; import SeriesIndexFooterConnector from './SeriesIndexFooterConnector'; import styles from './SeriesIndex.css'; function getViewComponent(view) { if (view === 'posters') { return SeriesIndexPostersConnector; } if (view === 'overview') { return SeriesIndexOverviewsConnector; } return SeriesIndexTableConnector; } class SeriesIndex extends Component { // // Lifecycle constructor(props, context) { super(props, context); this.state = { contentBody: null, jumpBarItems: { order: [] }, jumpToCharacter: null, isPosterOptionsModalOpen: false, isOverviewOptionsModalOpen: false, isRendered: false }; } componentDidMount() { this.setJumpBarItems(); } componentDidUpdate(prevProps) { const { items, sortKey, sortDirection, scrollTop } = this.props; if (sortKey !== prevProps.sortKey || sortDirection !== prevProps.sortDirection || hasDifferentItemsOrOrder(prevProps.items, items) ) { this.setJumpBarItems(); } if (this.state.jumpToCharacter != null && scrollTop !== prevProps.scrollTop) { this.setState({ jumpToCharacter: null }); } } // // Control setContentBodyRef = (ref) => { this.setState({ contentBody: ref }); } setJumpBarItems() { const { items, sortKey, sortDirection } = this.props; // Reset if not sorting by sortTitle if (sortKey !== 'sortTitle') { this.setState({ jumpBarItems: { order: [] } }); return; } const characters = _.reduce(items, (acc, item) => { let char = item.sortTitle.charAt(0); if (!isNaN(char)) { char = '#'; } if (char in acc) { acc[char] = acc[char] + 1; } else { acc[char] = 1; } return acc; }, {}); const order = Object.keys(characters).sort(); // Reverse if sorting descending if (sortDirection === sortDirections.DESCENDING) { order.reverse(); } const jumpBarItems = { characters, order }; this.setState({ jumpBarItems }); } // // Listeners onPosterOptionsPress = () => { this.setState({ isPosterOptionsModalOpen: true }); } onPosterOptionsModalClose = () => { this.setState({ isPosterOptionsModalOpen: false }); } onOverviewOptionsPress = () => { this.setState({ isOverviewOptionsModalOpen: true }); } onOverviewOptionsModalClose = () => { this.setState({ isOverviewOptionsModalOpen: false }); } onJumpBarItemPress = (jumpToCharacter) => { this.setState({ jumpToCharacter }); } onRender = () => { this.setState({ isRendered: true }, () => { const { scrollTop, isSmallScreen } = this.props; if (isSmallScreen) { // Seems to result in the view being off by 125px (distance to the top of the page) // document.documentElement.scrollTop = document.body.scrollTop = scrollTop; // This works, but then jumps another 1px after scrolling document.documentElement.scrollTop = scrollTop; } }); } onScroll = ({ scrollTop }) => { this.props.onScroll({ scrollTop }); } // // Render render() { const { isFetching, isPopulated, error, totalItems, items, columns, selectedFilterKey, filters, customFilters, sortKey, sortDirection, view, isRefreshingSeries, isRssSyncExecuting, scrollTop, onSortSelect, onFilterSelect, onViewSelect, onRefreshSeriesPress, onRssSyncPress, ...otherProps } = this.props; const { contentBody, jumpBarItems, jumpToCharacter, isPosterOptionsModalOpen, isOverviewOptionsModalOpen, isRendered } = this.state; const ViewComponent = getViewComponent(view); const isLoaded = !!(!error && isPopulated && items.length && contentBody); const hasNoSeries = !totalItems; return ( { view === 'table' ? : null } { view === 'posters' ? : null } { view === 'overview' ? : null } { (view === 'posters' || view === 'overview') && }
{ isFetching && !isPopulated && } { !isFetching && !!error &&
Unable to load series
} { isLoaded &&
} { !error && isPopulated && !items.length && }
{ isLoaded && !!jumpBarItems.order.length && }
); } } SeriesIndex.propTypes = { isFetching: PropTypes.bool.isRequired, isPopulated: PropTypes.bool.isRequired, error: PropTypes.object, totalItems: PropTypes.number.isRequired, items: PropTypes.arrayOf(PropTypes.object).isRequired, columns: PropTypes.arrayOf(PropTypes.object).isRequired, selectedFilterKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired, filters: PropTypes.arrayOf(PropTypes.object).isRequired, customFilters: PropTypes.arrayOf(PropTypes.object).isRequired, sortKey: PropTypes.string, sortDirection: PropTypes.oneOf(sortDirections.all), view: PropTypes.string.isRequired, isRefreshingSeries: PropTypes.bool.isRequired, isRssSyncExecuting: PropTypes.bool.isRequired, scrollTop: PropTypes.number.isRequired, isSmallScreen: PropTypes.bool.isRequired, onSortSelect: PropTypes.func.isRequired, onFilterSelect: PropTypes.func.isRequired, onViewSelect: PropTypes.func.isRequired, onRefreshSeriesPress: PropTypes.func.isRequired, onRssSyncPress: PropTypes.func.isRequired, onScroll: PropTypes.func.isRequired }; export default SeriesIndex;