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

77 lines
1.7 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';
import { connect } from 'react-redux';
import { createSelector } from 'reselect';
import { push } from 'connected-react-router';
import createAllArtistSelector from 'Store/Selectors/createAllArtistSelector';
2017-09-04 02:20:56 +00:00
import NotFound from 'Components/NotFound';
import ArtistDetailsConnector from './ArtistDetailsConnector';
2017-09-04 02:20:56 +00:00
function createMapStateToProps() {
return createSelector(
(state, { match }) => match,
createAllArtistSelector(),
2017-09-16 20:22:06 +00:00
(match, allArtists) => {
const foreignArtistId = match.params.foreignArtistId;
const artistIndex = _.findIndex(allArtists, { 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 {
foreignArtistId
2017-09-04 02:20:56 +00:00
};
}
return {};
}
);
}
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
2017-09-04 02:20:56 +00:00
} = this.props;
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,
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);