import _ from 'lodash'; import PropTypes from 'prop-types'; import React, { Component } from 'react'; import NoArtist from 'Artist/NoArtist'; import LoadingIndicator from 'Components/Loading/LoadingIndicator'; import PageContent from 'Components/Page/PageContent'; import PageContentBody from 'Components/Page/PageContentBody'; import PageJumpBar from 'Components/Page/PageJumpBar'; import PageToolbar from 'Components/Page/Toolbar/PageToolbar'; import PageToolbarButton from 'Components/Page/Toolbar/PageToolbarButton'; import PageToolbarSection from 'Components/Page/Toolbar/PageToolbarSection'; import PageToolbarSeparator from 'Components/Page/Toolbar/PageToolbarSeparator'; import TableOptionsModalWrapper from 'Components/Table/TableOptions/TableOptionsModalWrapper'; import { align, icons, sortDirections } from 'Helpers/Props'; import getErrorMessage from 'Utilities/Object/getErrorMessage'; import hasDifferentItemsOrOrder from 'Utilities/Object/hasDifferentItemsOrOrder'; import ArtistIndexFooterConnector from './ArtistIndexFooterConnector'; import ArtistIndexBannersConnector from './Banners/ArtistIndexBannersConnector'; import ArtistIndexBannerOptionsModal from './Banners/Options/ArtistIndexBannerOptionsModal'; import ArtistIndexFilterMenu from './Menus/ArtistIndexFilterMenu'; import ArtistIndexSortMenu from './Menus/ArtistIndexSortMenu'; import ArtistIndexViewMenu from './Menus/ArtistIndexViewMenu'; import ArtistIndexOverviewsConnector from './Overview/ArtistIndexOverviewsConnector'; import ArtistIndexOverviewOptionsModal from './Overview/Options/ArtistIndexOverviewOptionsModal'; import ArtistIndexPostersConnector from './Posters/ArtistIndexPostersConnector'; import ArtistIndexPosterOptionsModal from './Posters/Options/ArtistIndexPosterOptionsModal'; import ArtistIndexTableConnector from './Table/ArtistIndexTableConnector'; import ArtistIndexTableOptionsConnector from './Table/ArtistIndexTableOptionsConnector'; import styles from './ArtistIndex.css'; function getViewComponent(view) { if (view === 'posters') { return ArtistIndexPostersConnector; } if (view === 'banners') { return ArtistIndexBannersConnector; } if (view === 'overview') { return ArtistIndexOverviewsConnector; } return ArtistIndexTableConnector; } class ArtistIndex extends Component { // // Lifecycle constructor(props, context) { super(props, context); this.state = { scroller: null, jumpBarItems: { order: [] }, jumpToCharacter: null, isPosterOptionsModalOpen: false, isBannerOptionsModalOpen: false, isOverviewOptionsModalOpen: false }; } componentDidMount() { this.setJumpBarItems(); } componentDidUpdate(prevProps) { const { items, sortKey, sortDirection } = this.props; if (sortKey !== prevProps.sortKey || sortDirection !== prevProps.sortDirection || hasDifferentItemsOrOrder(prevProps.items, items) ) { this.setJumpBarItems(); } if (this.state.jumpToCharacter != null) { this.setState({ jumpToCharacter: null }); } } // // Control setScrollerRef = (ref) => { this.setState({ scroller: ref }); } setJumpBarItems() { const { items, sortKey, sortDirection } = this.props; // Reset if not sorting by sortName if (sortKey !== 'sortName') { this.setState({ jumpBarItems: { order: [] } }); return; } const characters = _.reduce(items, (acc, item) => { let char = item.sortName.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 }); } onBannerOptionsPress = () => { this.setState({ isBannerOptionsModalOpen: true }); } onBannerOptionsModalClose = () => { this.setState({ isBannerOptionsModalOpen: false }); } onOverviewOptionsPress = () => { this.setState({ isOverviewOptionsModalOpen: true }); } onOverviewOptionsModalClose = () => { this.setState({ isOverviewOptionsModalOpen: false }); } onJumpBarItemPress = (jumpToCharacter) => { this.setState({ jumpToCharacter }); } // // Render render() { const { isFetching, isPopulated, error, totalItems, items, columns, selectedFilterKey, filters, customFilters, sortKey, sortDirection, view, isRefreshingArtist, isRssSyncExecuting, onScroll, onSortSelect, onFilterSelect, onViewSelect, onRefreshArtistPress, onRssSyncPress, ...otherProps } = this.props; const { scroller, jumpBarItems, jumpToCharacter, isPosterOptionsModalOpen, isBannerOptionsModalOpen, isOverviewOptionsModalOpen } = this.state; const ViewComponent = getViewComponent(view); const isLoaded = !!(!error && isPopulated && items.length && scroller); const hasNoArtist = !totalItems; return ( { view === 'table' ? : null } { view === 'posters' ? : null } { view === 'banners' ? : null } { view === 'overview' ? : null }
{ isFetching && !isPopulated && } { !isFetching && !!error &&
{getErrorMessage(error, 'Failed to load artist from API')}
} { isLoaded &&
} { !error && isPopulated && !items.length && }
{ isLoaded && !!jumpBarItems.order.length && }
); } } ArtistIndex.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, isRefreshingArtist: PropTypes.bool.isRequired, isRssSyncExecuting: PropTypes.bool.isRequired, isSmallScreen: PropTypes.bool.isRequired, onSortSelect: PropTypes.func.isRequired, onFilterSelect: PropTypes.func.isRequired, onViewSelect: PropTypes.func.isRequired, onRefreshArtistPress: PropTypes.func.isRequired, onRssSyncPress: PropTypes.func.isRequired, onScroll: PropTypes.func.isRequired }; export default ArtistIndex;