You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
2.6 KiB
116 lines
2.6 KiB
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 NotFound from 'Components/NotFound';
|
|
import { fetchAlbums, clearAlbums } from 'Store/Actions/albumActions';
|
|
import LoadingIndicator from 'Components/Loading/LoadingIndicator';
|
|
import AlbumDetailsConnector from './AlbumDetailsConnector';
|
|
|
|
function createMapStateToProps() {
|
|
return createSelector(
|
|
(state, { match }) => match,
|
|
(state) => state.albums,
|
|
(match, albums) => {
|
|
const foreignAlbumId = match.params.foreignAlbumId;
|
|
const isAlbumsFetching = albums.isFetching;
|
|
const isAlbumsPopulated = albums.isPopulated;
|
|
|
|
return {
|
|
foreignAlbumId,
|
|
isAlbumsFetching,
|
|
isAlbumsPopulated
|
|
};
|
|
}
|
|
);
|
|
}
|
|
|
|
const mapDispatchToProps = {
|
|
push,
|
|
fetchAlbums,
|
|
clearAlbums
|
|
};
|
|
|
|
class AlbumDetailsPageConnector extends Component {
|
|
|
|
constructor(props) {
|
|
super(props);
|
|
this.state = { hasMounted: false };
|
|
}
|
|
//
|
|
// Lifecycle
|
|
|
|
componentDidMount() {
|
|
this.populate();
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
this.unpopulate();
|
|
}
|
|
|
|
//
|
|
// Control
|
|
|
|
populate = () => {
|
|
const foreignAlbumId = this.props.foreignAlbumId;
|
|
this.setState({ hasMounted: true });
|
|
this.props.fetchAlbums({ foreignAlbumId });
|
|
}
|
|
|
|
unpopulate = () => {
|
|
this.props.clearAlbums();
|
|
}
|
|
|
|
//
|
|
// Render
|
|
|
|
render() {
|
|
const {
|
|
foreignAlbumId,
|
|
isAlbumsFetching,
|
|
isAlbumsPopulated
|
|
} = this.props;
|
|
|
|
if (!foreignAlbumId) {
|
|
return (
|
|
<NotFound
|
|
message="Sorry, that album cannot be found."
|
|
/>
|
|
);
|
|
}
|
|
|
|
if (isAlbumsFetching || !this.state.hasMounted) {
|
|
return (
|
|
<LoadingIndicator />
|
|
);
|
|
}
|
|
|
|
if (!isAlbumsFetching && !isAlbumsPopulated) {
|
|
return (
|
|
<LoadingIndicator />
|
|
);
|
|
}
|
|
|
|
if (!isAlbumsFetching && isAlbumsPopulated && this.state.hasMounted) {
|
|
return (
|
|
<AlbumDetailsConnector
|
|
foreignAlbumId={foreignAlbumId}
|
|
/>
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
AlbumDetailsPageConnector.propTypes = {
|
|
foreignAlbumId: PropTypes.string,
|
|
match: PropTypes.shape({ params: PropTypes.shape({ foreignAlbumId: PropTypes.string.isRequired }).isRequired }).isRequired,
|
|
push: PropTypes.func.isRequired,
|
|
fetchAlbums: PropTypes.func.isRequired,
|
|
clearAlbums: PropTypes.func.isRequired,
|
|
isAlbumsFetching: PropTypes.bool.isRequired,
|
|
isAlbumsPopulated: PropTypes.bool.isRequired
|
|
};
|
|
|
|
export default connect(createMapStateToProps, mapDispatchToProps)(AlbumDetailsPageConnector);
|