|
| 1 | +import React, { useContext, useEffect, useState } from 'react' |
| 2 | +import styled from 'styled-components/macro' |
| 3 | +// components |
| 4 | +import Frame from './frame' |
| 5 | +// context |
| 6 | +import { LoginContext, TokenContext } from '../../utils/context' |
| 7 | +// utils |
| 8 | +import reqWithToken from '../../utils/reqWithToken' |
| 9 | +// hooks |
| 10 | +import { useDimesions } from "../../hooks/useDimesions" |
| 11 | + |
| 12 | +const Container = styled.div` |
| 13 | + width: 100%; |
| 14 | + min-height: 256px; |
| 15 | + margin-bottom: 16px; |
| 16 | + h3 { |
| 17 | + font-size: 24px !important; |
| 18 | + } |
| 19 | + p { |
| 20 | + font-size: 14px; |
| 21 | + } |
| 22 | +`; |
| 23 | + |
| 24 | +const SectionWrapper = styled.div` |
| 25 | + display: grid; |
| 26 | + width: 100%; |
| 27 | + height: 100%; |
| 28 | + grid-template-columns: repeat(6, minmax(156px, 1fr)); |
| 29 | + column-gap: 24px; |
| 30 | +`; |
| 31 | + |
| 32 | +export default function CategoryContainer({ title = "FireyBoi", tag, id, country = "US", children }) { |
| 33 | + const auth = useContext(LoginContext); |
| 34 | + const spotifyToken = useContext(TokenContext); |
| 35 | + const [playlists, setPlaylists] = useState([]); |
| 36 | + const { items: tracks } = playlists; |
| 37 | + |
| 38 | + const [sectionsRef, dimensions] = useDimesions(); |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + if (auth) { |
| 42 | + const getPlaylists = async () => { |
| 43 | + const categoryPlaylists = reqWithToken(`https://api.spotify.com/v1/browse/categories/${id}/playlists?country=${country}&limit=8`, spotifyToken); |
| 44 | + try { |
| 45 | + const { data: { playlists } } = await categoryPlaylists(); |
| 46 | + setPlaylists(playlists); |
| 47 | + } catch (error) { |
| 48 | + console.log(error) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + getPlaylists(); |
| 53 | + } |
| 54 | + |
| 55 | + }, [auth, spotifyToken, id, country]) |
| 56 | + |
| 57 | + return children ? ( |
| 58 | + <Container> |
| 59 | + <SectionWrapper style={{ marginTop: "2px" }}> |
| 60 | + {children} |
| 61 | + </SectionWrapper> |
| 62 | + </Container> |
| 63 | + ) : ( |
| 64 | + <Container> |
| 65 | + <h3>{title}</h3> |
| 66 | + {tag && <p>{tag}</p>} |
| 67 | + {auth && ( |
| 68 | + <SectionWrapper ref={sectionsRef} style={{ marginTop: "12px", gridTemplateColumns: dimensions.width < 1206 ? `repeat(${Math.ceil(dimensions.width / 220)}, minmax(0, 1fr)` : `repeat(6, minmax(0, 1fr)` }}> |
| 69 | + {tracks && tracks.filter((playlist, index) => (dimensions.width < 1206 ? (index < Math.ceil(dimensions.width / 220)) : (index < 6))).map((playlist, index) => ( |
| 70 | + <Frame playlist={playlist} key={`playlist-${playlist.id}`} /> |
| 71 | + ))} |
| 72 | + </SectionWrapper> |
| 73 | + )} |
| 74 | + </Container> |
| 75 | + ) |
| 76 | +} |
0 commit comments