From 829ab4345366f92e9764b11ee3561395782a381e Mon Sep 17 00:00:00 2001 From: demmyhonore Date: Fri, 30 Apr 2021 14:43:57 +0200 Subject: [PATCH] feat(project): add category filtering to playlist --- src/screens/Playlist/Playlist.module.scss | 39 ++++++++---------- src/screens/Playlist/Playlist.tsx | 48 ++++++++++++++--------- src/utils/collection.ts | 14 +++++++ 3 files changed, 61 insertions(+), 40 deletions(-) create mode 100644 src/utils/collection.ts diff --git a/src/screens/Playlist/Playlist.module.scss b/src/screens/Playlist/Playlist.module.scss index 3e7f524e1..a29490995 100644 --- a/src/screens/Playlist/Playlist.module.scss +++ b/src/screens/Playlist/Playlist.module.scss @@ -1,27 +1,22 @@ - .playlist { - color: var(--primary-color); - font-family: var(--body-alt-font-family); - margin-top: 20px; - margin-left: 56px; - margin-right: 56px; - text-align: center; +.playlist { + color: var(--primary-color); + font-family: var(--body-alt-font-family); + margin-top: 20px; + margin-left: 56px; + margin-right: 56px; + text-align: center; - @media screen and (max-width: 720px) { - margin-left: 12px; - margin-right: 12px; - } + @media screen and (max-width: 720px) { + margin-left: 12px; + margin-right: 12px; } +} - .header { - display: flex; - align-items: center; +.header { + display: flex; + align-items: center; - > * + * { - margin-left: 24px; - } - } - - .grid { - display: grid; - grid-gap: 8px; + > * + * { + margin-left: 24px; } +} diff --git a/src/screens/Playlist/Playlist.tsx b/src/screens/Playlist/Playlist.tsx index c07453810..75afb6a61 100644 --- a/src/screens/Playlist/Playlist.tsx +++ b/src/screens/Playlist/Playlist.tsx @@ -1,16 +1,17 @@ -import React from 'react'; +import React, { useState } from 'react'; import usePlaylist from '../../hooks/usePlaylist'; -import useBreakpoint from '../../hooks/useBreakpoint'; +import { getCategoriesFromPlaylist, filterPlaylistCategory } from '../../utils/collection'; import Card from '../../components/Card/Card' +import Dropdown from '../../components/Dropdown/Dropdown' +import CardGrid from '../../components/CardGrid/CardGrid' import styles from './Playlist.module.scss'; -// temp data -const cols = { "xs": 2, "sm": 3, "md": 4, "lg": 5, "xl": 6 } +// TEMP DATA const playlistId = "sR5VypYk"; -type PlaylistMapArguments = { +type PlaylistDestructuredArguments = { mediaid: string; title: string; duration: number; @@ -19,29 +20,40 @@ type PlaylistMapArguments = { function Playlist() { const { isLoading, error, data: { title, playlist } = {} } = usePlaylist(playlistId) - const breakpoint = useBreakpoint(); + const [filter, setFilter] = useState('') if (isLoading) return

Loading...

if (error) return

No playlist found...

+ const categories = getCategoriesFromPlaylist(playlist) + const filteredPlaylist = filterPlaylistCategory(playlist, filter) + return (

{title}

-
- -
+ {categories.length && ( + )}
-
- {playlist.map(({ mediaid: mediaId, title, duration, image }: PlaylistMapArguments) => - '')} />)} +
+ + {filteredPlaylist.map(({ mediaid: mediaId, title, duration, image }: PlaylistDestructuredArguments) => + ( + '')} + />))} +
); diff --git a/src/utils/collection.ts b/src/utils/collection.ts new file mode 100644 index 000000000..1195398ee --- /dev/null +++ b/src/utils/collection.ts @@ -0,0 +1,14 @@ +import type { Playlist } from 'types/playlist'; + +const getCategoriesFromPlaylist = (playlist: Playlist) => + playlist.reduce((categories: string[], item) => categories.includes(item.genre) ? categories : categories.concat(item.genre), []) + + +const filterPlaylistCategory = (playlist: Playlist, filter: string) => { + if (!filter) return playlist + + return playlist.filter(({ genre }) => genre === filter) + +} + +export { getCategoriesFromPlaylist, filterPlaylistCategory }