Lidarr/frontend/src/Artist/Details/ArtistDetailsPageConnector.js

118 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-09-07 01:33:10 +00:00
import { push } from 'connected-react-router';
2017-09-04 02:20:56 +00:00
import _ from 'lodash';
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { createSelector } from 'reselect';
import LoadingIndicator from 'Components/Loading/LoadingIndicator';
2017-09-04 02:20:56 +00:00
import NotFound from 'Components/NotFound';
2020-09-07 01:33:10 +00:00
import PageContent from 'Components/Page/PageContent';
import PageContentBody from 'Components/Page/PageContentBody';
import getErrorMessage from 'Utilities/Object/getErrorMessage';
import ArtistDetailsConnector from './ArtistDetailsConnector';
import styles from './ArtistDetails.css';
2017-09-04 02:20:56 +00:00
function createMapStateToProps() {
return createSelector(
(state, { match }) => match,
(state) => state.artist,
(match, artist) => {
const foreignArtistId = match.params.foreignArtistId;
const {
isFetching,
isPopulated,
error,
items
} = artist;
const artistIndex = _.findIndex(items, { foreignArtistId });
2017-09-04 02:20:56 +00:00
2017-09-16 20:22:06 +00:00
if (artistIndex > -1) {
2017-09-04 02:20:56 +00:00
return {
isFetching,
isPopulated,
foreignArtistId
2017-09-04 02:20:56 +00:00
};
}
return {
isFetching,
isPopulated,
error
};
2017-09-04 02:20:56 +00:00
}
);
}
const mapDispatchToProps = {
push
};
class ArtistDetailsPageConnector extends Component {
2017-09-04 02:20:56 +00:00
//
// Lifecycle
componentDidUpdate(prevProps) {
if (!this.props.foreignArtistId) {
this.props.push(`${window.Lidarr.urlBase}/`);
2017-09-04 02:20:56 +00:00
return;
}
}
//
// Render
render() {
const {
foreignArtistId,
isFetching,
isPopulated,
error
2017-09-04 02:20:56 +00:00
} = this.props;
if (isFetching && !isPopulated) {
return (
<PageContent title='loading'>
<PageContentBody>
<LoadingIndicator />
</PageContentBody>
</PageContent>
);
}
if (!isFetching && !!error) {
return (
<div className={styles.errorMessage}>
{getErrorMessage(error, 'Failed to load artist from API')}
</div>
);
}
if (!foreignArtistId) {
2017-09-04 02:20:56 +00:00
return (
<NotFound
2017-10-07 22:38:31 +00:00
message="Sorry, that artist cannot be found."
2017-09-04 02:20:56 +00:00
/>
);
}
return (
<ArtistDetailsConnector
foreignArtistId={foreignArtistId}
2017-09-04 02:20:56 +00:00
/>
);
}
}
ArtistDetailsPageConnector.propTypes = {
foreignArtistId: PropTypes.string,
isFetching: PropTypes.bool.isRequired,
isPopulated: PropTypes.bool.isRequired,
error: PropTypes.object,
match: PropTypes.shape({ params: PropTypes.shape({ foreignArtistId: PropTypes.string.isRequired }).isRequired }).isRequired,
2017-09-04 02:20:56 +00:00
push: PropTypes.func.isRequired
};
export default connect(createMapStateToProps, mapDispatchToProps)(ArtistDetailsPageConnector);