Handle click on album

Click on album should dispatch an SELECT_ALBUM action.

This is done by map to AlbumList, and providing a property to each
album. It could also had been done by introducing a container for Album,
but IMHO that's not motivated.
This commit is contained in:
Anders Ytterström 2019-04-23 15:01:42 +02:00
parent 60e70f4b34
commit 1ceb248df9
3 changed files with 19 additions and 5 deletions

View file

@ -5,11 +5,18 @@ export default class AlbumList extends Component {
render() { render() {
const { const {
albums, albums,
handleOnClick,
blurred,
} = this.props; } = this.props;
const classNames = blurred ? 'blur' : ''
return ( return (
<div id="albums"> <div id="albums" className={classNames}>
{albums.map(album => ( {albums.map(album => (
<Album key={album.id} {...album} /> <Album
key={album.id}
album={album}
handleOnClick={handleOnClick}
/>
))} ))}
</div> </div>
); );

View file

@ -2,6 +2,7 @@ import React, { Component } from 'react';
export default class Album extends Component { export default class Album extends Component {
render() { render() {
const { album, handleOnClick } = this.props
const { const {
id, id,
artist, artist,
@ -10,11 +11,11 @@ export default class Album extends Component {
year, year,
img, img,
purchased_on, purchased_on,
} = this.props; } = album;
const imagePath = `assets/covers/${img}`; const imagePath = `assets/covers/${img}`;
const song = songs.join(', '); const song = songs.join(', ');
return ( return (
<article> <article onClick={() => handleOnClick(album)}>
<figure> <figure>
<img src={imagePath} alt="cover" /> <img src={imagePath} alt="cover" />
</figure> </figure>

View file

@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import AlbumList from '../components/album-list'; import AlbumList from '../components/album-list';
import { selectAlbum } from '../actions'
const getAlbums = (albums, filter) => { const getAlbums = (albums, filter) => {
const atos = o => [o.artist, o.title, o.songs.join(' '), o.year].join(' ').toLowerCase(); const atos = o => [o.artist, o.title, o.songs.join(' '), o.year].join(' ').toLowerCase();
@ -13,6 +14,11 @@ const getAlbums = (albums, filter) => {
const mapStateToProps = state => ({ const mapStateToProps = state => ({
albums: getAlbums(state.albums, state.visibilityFilter), albums: getAlbums(state.albums, state.visibilityFilter),
blurred: "id" in state.selectedAlbum,
}); });
export default connect(mapStateToProps)(AlbumList); const mapDispatchToProps = dispatch => ({
handleOnClick: album => dispatch(selectAlbum(album)),
});
export default connect(mapStateToProps, mapDispatchToProps)(AlbumList);