brutal-legends/src/containers/album-list.ts

30 lines
1,000 B
TypeScript
Raw Normal View History

2020-03-05 08:07:46 +01:00
import { connect } from "react-redux";
import AlbumList from "../components/album-list";
import { selectAlbum } from "../actions";
import { Album, State } from "../interfaces";
const atos = (o: Album) =>
[o.artist, o.title, o.songs.join(" "), o.year].join(" ").toLowerCase();
2020-03-05 21:48:45 +01:00
const getAlbums = (albums: Array<Object>, filter: string, sortKey: string) => {
2020-03-05 08:07:46 +01:00
if (filter) {
const term = filter.toLowerCase();
2020-03-05 21:48:45 +01:00
albums = albums.filter((album: Album) => atos(album).match(term));
2020-03-05 08:07:46 +01:00
}
2020-03-05 21:48:45 +01:00
return [...albums].sort((a: Album, b: Album) =>
a[sortKey] > b[sortKey] ? 1 : -1
);
2020-03-05 08:07:46 +01:00
};
const mapStateToProps = (state: State) => ({
2020-03-05 21:48:45 +01:00
albums: getAlbums(state.albums, state.visibilityFilter, state.sortKey),
blurred: "id" in state.selectedAlbum,
sortKey: state.sortKey
2020-03-05 08:07:46 +01:00
});
const mapDispatchToProps = (dispatch: Function) => ({
handleOnClick: (album: Album) => dispatch(selectAlbum(album))
});
export default connect(mapStateToProps, mapDispatchToProps)(AlbumList);