Skip to content

Commit

Permalink
playlist
Browse files Browse the repository at this point in the history
  • Loading branch information
Hendrixer committed Dec 14, 2021
1 parent 9d612d3 commit c47f17c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
8 changes: 5 additions & 3 deletions components/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
MdPlaylistAdd,
MdFavorite,
} from 'react-icons/md'
import { usePlaylist } from '../lib/hooks'

const navMenu = [
{
Expand Down Expand Up @@ -49,9 +50,10 @@ const musicMenu = [
},
]

const playlists = new Array(30).fill(1).map((_, i) => `Playlist ${i + 1}`)
// const playlists = new Array(30).fill(1).map((_, i) => `Playlist ${i + 1}`)

const Sidebar = () => {
const { playlists } = usePlaylist()
return (
<Box
width="100%"
Expand Down Expand Up @@ -108,10 +110,10 @@ const Sidebar = () => {
<Box height="66%" overflowY="auto" paddingY="20px">
<List spaceing={2}>
{playlists.map((playlist) => (
<ListItem paddingX="20px" key={playlist}>
<ListItem paddingX="20px" key={playlist.id}>
<LinkBox>
<NextLink href="/" passHref>
<LinkOverlay>{playlist}</LinkOverlay>
<LinkOverlay>{playlist.name}</LinkOverlay>
</NextLink>
</LinkBox>
</ListItem>
Expand Down
5 changes: 5 additions & 0 deletions lib/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ export default function fetcher(url: string, data = undefined) {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
}).then((res) => {
if (res.status > 399 && res.status < 200) {
throw new Error()
}
return res.json()
})
}
21 changes: 21 additions & 0 deletions lib/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import useSWR from 'swr'
import fetcher from './fetcher'

export const useMe = () => {
const { data, error } = useSWR('/me', fetcher)

return {
user: data,
isLoading: !data && !error,
isError: error,
}
}

export const usePlaylist = () => {
const { data, error } = useSWR('/playlist', fetcher)
return {
playlists: (data as any) || [],
isLoading: !data && !error,
isError: error,
}
}
15 changes: 15 additions & 0 deletions pages/api/playlist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import prisma from '../../lib/prisma'
import { validateRoute } from '../../lib/auth'

export default validateRoute(async (req, res, user) => {
const playlists = await prisma.playlist.findMany({
where: {
userId: user.id,
},
orderBy: {
name: 'asc',
},
})

res.json(playlists)
})

0 comments on commit c47f17c

Please sign in to comment.