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

407 lines
11 KiB
JavaScript
Raw Normal View History

2017-09-04 02:20:56 +00:00
import _ from 'lodash';
import PropTypes from 'prop-types';
import React, { Component } from 'react';
2020-09-07 01:33:10 +00:00
import NoArtist from 'Artist/NoArtist';
2017-09-04 02:20:56 +00:00
import LoadingIndicator from 'Components/Loading/LoadingIndicator';
import PageContent from 'Components/Page/PageContent';
import PageContentBody from 'Components/Page/PageContentBody';
2017-09-04 02:20:56 +00:00
import PageJumpBar from 'Components/Page/PageJumpBar';
import PageToolbar from 'Components/Page/Toolbar/PageToolbar';
import PageToolbarButton from 'Components/Page/Toolbar/PageToolbarButton';
2020-09-07 01:33:10 +00:00
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';
2020-09-07 01:33:10 +00:00
import ArtistIndexBannersConnector from './Banners/ArtistIndexBannersConnector';
import ArtistIndexBannerOptionsModal from './Banners/Options/ArtistIndexBannerOptionsModal';
2017-09-04 02:20:56 +00:00
import ArtistIndexFilterMenu from './Menus/ArtistIndexFilterMenu';
import ArtistIndexSortMenu from './Menus/ArtistIndexSortMenu';
import ArtistIndexViewMenu from './Menus/ArtistIndexViewMenu';
2020-09-07 01:33:10 +00:00
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';
2017-09-04 02:20:56 +00:00
import styles from './ArtistIndex.css';
function getViewComponent(view) {
if (view === 'posters') {
return ArtistIndexPostersConnector;
}
2017-09-17 06:24:15 +00:00
if (view === 'banners') {
return ArtistIndexBannersConnector;
}
if (view === 'overview') {
return ArtistIndexOverviewsConnector;
}
2017-09-04 02:20:56 +00:00
return ArtistIndexTableConnector;
}
class ArtistIndex extends Component {
//
// Lifecycle
constructor(props, context) {
super(props, context);
this.state = {
scroller: null,
jumpBarItems: { order: [] },
2018-03-15 01:28:46 +00:00
jumpToCharacter: null,
2017-09-04 02:20:56 +00:00
isPosterOptionsModalOpen: false,
2017-09-17 06:24:15 +00:00
isBannerOptionsModalOpen: false,
isOverviewOptionsModalOpen: false
2017-09-04 02:20:56 +00:00
};
}
componentDidMount() {
this.setJumpBarItems();
}
componentDidUpdate(prevProps) {
const {
items,
sortKey,
sortDirection
2017-09-04 02:20:56 +00:00
} = this.props;
if (sortKey !== prevProps.sortKey ||
sortDirection !== prevProps.sortDirection ||
hasDifferentItemsOrOrder(prevProps.items, items)
2017-09-04 02:20:56 +00:00
) {
this.setJumpBarItems();
}
2018-03-15 01:28:46 +00:00
if (this.state.jumpToCharacter != null) {
2018-03-15 01:28:46 +00:00
this.setState({ jumpToCharacter: null });
}
2017-09-04 02:20:56 +00:00
}
//
// Control
setScrollerRef = (ref) => {
this.setState({ scroller: ref });
2017-09-04 02:20:56 +00:00
}
setJumpBarItems() {
const {
items,
sortKey,
sortDirection
} = this.props;
2017-09-16 20:22:06 +00:00
// Reset if not sorting by sortName
2017-09-04 02:20:56 +00:00
if (sortKey !== 'sortName') {
this.setState({ jumpBarItems: { order: [] } });
2017-09-04 02:20:56 +00:00
return;
}
const characters = _.reduce(items, (acc, item) => {
let char = item.sortName.charAt(0);
2017-09-04 02:20:56 +00:00
if (!isNaN(char)) {
char = '#';
}
if (char in acc) {
acc[char] = acc[char] + 1;
2017-09-04 02:20:56 +00:00
} else {
acc[char] = 1;
2017-09-04 02:20:56 +00:00
}
return acc;
}, {});
const order = Object.keys(characters).sort();
2017-09-04 02:20:56 +00:00
// Reverse if sorting descending
if (sortDirection === sortDirections.DESCENDING) {
order.reverse();
2017-09-04 02:20:56 +00:00
}
const jumpBarItems = {
characters,
order
};
this.setState({ jumpBarItems });
2017-09-04 02:20:56 +00:00
}
//
// Listeners
onPosterOptionsPress = () => {
this.setState({ isPosterOptionsModalOpen: true });
}
onPosterOptionsModalClose = () => {
this.setState({ isPosterOptionsModalOpen: false });
}
2017-09-17 06:24:15 +00:00
onBannerOptionsPress = () => {
this.setState({ isBannerOptionsModalOpen: true });
}
onBannerOptionsModalClose = () => {
this.setState({ isBannerOptionsModalOpen: false });
}
onOverviewOptionsPress = () => {
this.setState({ isOverviewOptionsModalOpen: true });
}
onOverviewOptionsModalClose = () => {
this.setState({ isOverviewOptionsModalOpen: false });
}
2018-03-15 01:28:46 +00:00
onJumpBarItemPress = (jumpToCharacter) => {
this.setState({ jumpToCharacter });
2017-09-04 02:20:56 +00:00
}
//
// Render
render() {
const {
isFetching,
isPopulated,
error,
totalItems,
2017-09-04 02:20:56 +00:00
items,
columns,
2018-03-15 01:28:46 +00:00
selectedFilterKey,
filters,
customFilters,
2017-09-04 02:20:56 +00:00
sortKey,
sortDirection,
view,
2017-09-16 20:22:06 +00:00
isRefreshingArtist,
2017-09-04 02:20:56 +00:00
isRssSyncExecuting,
onScroll,
2017-09-04 02:20:56 +00:00
onSortSelect,
onFilterSelect,
onViewSelect,
onRefreshArtistPress,
2017-09-04 02:20:56 +00:00
onRssSyncPress,
...otherProps
} = this.props;
const {
scroller,
2017-09-04 02:20:56 +00:00
jumpBarItems,
2018-03-15 01:28:46 +00:00
jumpToCharacter,
2017-09-04 02:20:56 +00:00
isPosterOptionsModalOpen,
2017-09-17 06:24:15 +00:00
isBannerOptionsModalOpen,
isOverviewOptionsModalOpen
2017-09-04 02:20:56 +00:00
} = this.state;
const ViewComponent = getViewComponent(view);
const isLoaded = !!(!error && isPopulated && items.length && scroller);
const hasNoArtist = !totalItems;
2017-09-04 02:20:56 +00:00
return (
<PageContent>
<PageToolbar>
<PageToolbarSection>
<PageToolbarButton
label="Update all"
iconName={icons.REFRESH}
spinningName={icons.REFRESH}
2017-09-16 20:22:06 +00:00
isSpinning={isRefreshingArtist}
onPress={onRefreshArtistPress}
2017-09-04 02:20:56 +00:00
/>
<PageToolbarButton
label="RSS Sync"
iconName={icons.RSS}
isSpinning={isRssSyncExecuting}
2017-11-26 20:09:45 +00:00
isDisabled={hasNoArtist}
2017-09-04 02:20:56 +00:00
onPress={onRssSyncPress}
/>
</PageToolbarSection>
<PageToolbarSection
alignContent={align.RIGHT}
collapseButtons={false}
>
{
view === 'table' ?
<TableOptionsModalWrapper
{...otherProps}
columns={columns}
optionsComponent={ArtistIndexTableOptionsConnector}
>
<PageToolbarButton
label="Options"
iconName={icons.TABLE}
/>
</TableOptionsModalWrapper> :
null
}
2017-09-04 02:20:56 +00:00
{
view === 'posters' ?
2017-09-04 02:20:56 +00:00
<PageToolbarButton
label="Options"
iconName={icons.POSTER}
2017-11-26 20:09:45 +00:00
isDisabled={hasNoArtist}
2017-09-04 02:20:56 +00:00
onPress={this.onPosterOptionsPress}
/> :
null
2017-09-04 02:20:56 +00:00
}
2017-09-17 06:24:15 +00:00
{
view === 'banners' ?
2017-09-17 06:24:15 +00:00
<PageToolbarButton
label="Options"
iconName={icons.POSTER}
2017-11-26 20:09:45 +00:00
isDisabled={hasNoArtist}
2017-09-17 06:24:15 +00:00
onPress={this.onBannerOptionsPress}
/> :
null
2017-09-17 06:24:15 +00:00
}
{
view === 'overview' ?
<PageToolbarButton
label="Options"
iconName={icons.OVERVIEW}
isDisabled={hasNoArtist}
onPress={this.onOverviewOptionsPress}
/> :
null
}
<PageToolbarSeparator />
2017-09-17 06:24:15 +00:00
2017-09-04 02:20:56 +00:00
<ArtistIndexViewMenu
view={view}
2017-11-26 20:09:45 +00:00
isDisabled={hasNoArtist}
2017-09-04 02:20:56 +00:00
onViewSelect={onViewSelect}
/>
<ArtistIndexSortMenu
sortKey={sortKey}
sortDirection={sortDirection}
2017-11-26 20:09:45 +00:00
isDisabled={hasNoArtist}
2017-09-04 02:20:56 +00:00
onSortSelect={onSortSelect}
/>
<ArtistIndexFilterMenu
2018-03-15 01:28:46 +00:00
selectedFilterKey={selectedFilterKey}
filters={filters}
customFilters={customFilters}
2017-11-26 20:09:45 +00:00
isDisabled={hasNoArtist}
2017-09-04 02:20:56 +00:00
onFilterSelect={onFilterSelect}
/>
</PageToolbarSection>
</PageToolbar>
<div className={styles.pageContentBodyWrapper}>
<PageContentBody
registerScroller={this.setScrollerRef}
2017-09-04 02:20:56 +00:00
className={styles.contentBody}
innerClassName={styles[`${view}InnerContentBody`]}
onScroll={onScroll}
2017-09-04 02:20:56 +00:00
>
{
isFetching && !isPopulated &&
<LoadingIndicator />
}
{
!isFetching && !!error &&
<div className={styles.errorMessage}>
{getErrorMessage(error, 'Failed to load artist from API')}
</div>
2017-09-04 02:20:56 +00:00
}
{
isLoaded &&
<div className={styles.contentBodyContainer}>
<ViewComponent
scroller={scroller}
items={items}
filters={filters}
sortKey={sortKey}
sortDirection={sortDirection}
2018-03-15 01:28:46 +00:00
jumpToCharacter={jumpToCharacter}
2017-09-04 02:20:56 +00:00
{...otherProps}
/>
<ArtistIndexFooterConnector />
2017-09-04 02:20:56 +00:00
</div>
}
{
!error && isPopulated && !items.length &&
<NoArtist totalItems={totalItems} />
2017-09-04 02:20:56 +00:00
}
</PageContentBody>
2017-09-04 02:20:56 +00:00
{
isLoaded && !!jumpBarItems.order.length &&
2017-09-04 02:20:56 +00:00
<PageJumpBar
items={jumpBarItems}
onItemPress={this.onJumpBarItemPress}
/>
}
</div>
<ArtistIndexPosterOptionsModal
isOpen={isPosterOptionsModalOpen}
onModalClose={this.onPosterOptionsModalClose}
/>
2017-09-17 06:24:15 +00:00
<ArtistIndexBannerOptionsModal
isOpen={isBannerOptionsModalOpen}
onModalClose={this.onBannerOptionsModalClose}
/>
<ArtistIndexOverviewOptionsModal
isOpen={isOverviewOptionsModalOpen}
onModalClose={this.onOverviewOptionsModalClose}
2017-09-17 06:24:15 +00:00
/>
2017-09-04 02:20:56 +00:00
</PageContent>
);
}
}
ArtistIndex.propTypes = {
isFetching: PropTypes.bool.isRequired,
isPopulated: PropTypes.bool.isRequired,
error: PropTypes.object,
totalItems: PropTypes.number.isRequired,
2017-09-04 02:20:56 +00:00
items: PropTypes.arrayOf(PropTypes.object).isRequired,
columns: PropTypes.arrayOf(PropTypes.object).isRequired,
selectedFilterKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
2018-03-15 01:28:46 +00:00
filters: PropTypes.arrayOf(PropTypes.object).isRequired,
customFilters: PropTypes.arrayOf(PropTypes.object).isRequired,
2017-09-04 02:20:56 +00:00
sortKey: PropTypes.string,
sortDirection: PropTypes.oneOf(sortDirections.all),
view: PropTypes.string.isRequired,
2017-09-16 20:22:06 +00:00
isRefreshingArtist: PropTypes.bool.isRequired,
2017-09-04 02:20:56 +00:00
isRssSyncExecuting: PropTypes.bool.isRequired,
isSmallScreen: PropTypes.bool.isRequired,
onSortSelect: PropTypes.func.isRequired,
onFilterSelect: PropTypes.func.isRequired,
onViewSelect: PropTypes.func.isRequired,
onRefreshArtistPress: PropTypes.func.isRequired,
2017-09-04 02:20:56 +00:00
onRssSyncPress: PropTypes.func.isRequired,
onScroll: PropTypes.func.isRequired
};
export default ArtistIndex;