Skip to content

Commit

Permalink
Created Genre, Playlists, Search Pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Ifechukwudaniel committed Dec 15, 2023
1 parent a35724b commit 102e078
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 3 deletions.
66 changes: 66 additions & 0 deletions packages/nextjs/pages/genre/[category].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { useEffect, useState } from "react";
import { customGet } from "../../utils/beat-bridge/customGet";
import { isAuthenticated } from "../../utils/beat-bridge/isAuthenticated";
import { GetServerSideProps } from "next";
import { getSession } from "next-auth/react";
import DashboardLayout from "~~/components/dashboard/DashboardLayout";
import Heading from "~~/components/spotify/Heading";
import Layout from "~~/components/spotify/Layout";
import PlaylistList from "~~/components/spotify/PlaylistList";
import { MySession } from "~~/types/session";
import { PlaylistType } from "~~/types/spotify";

interface IProps {
categoryName?: string;
playlists: {
items: PlaylistType[];
};
}

export default function CategoryPlaylists({ categoryName, playlists }: IProps) {
const [capitalizedCategory, setCapitalizedCategory] = useState("");

useEffect(() => {
if (categoryName) {
const afterName = categoryName
.split(" ")
.map(i => i[0].toUpperCase() + i.slice(1))
.join(" ");
setCapitalizedCategory(afterName);
}
}, [categoryName]);

return (
<DashboardLayout>
<Layout title={`Spotify - ${capitalizedCategory}`}>
<Heading text={categoryName || ""} className="capitalize" />
<PlaylistList playlists={playlists?.items} />
</Layout>
</DashboardLayout>
);
}

export const getServerSideProps: GetServerSideProps = async ctx => {
const session = (await getSession(ctx)) as MySession | null;

if (!(await isAuthenticated(session))) {
return {
redirect: {
destination: "/login",
permanent: false,
},
};
}

const categoryId = ctx.params?.category;
const category = await customGet(`https://api.spotify.com/v1/browse/categories/${categoryId}`, session);
const playlists = await customGet(
`https://api.spotify.com/v1/browse/categories/${categoryId}/playlists?limit=50`,
session,
);

const categoryName = category.name?.toString().split("_").join(" ") || "";

console.log(playlists.playlists);
return { props: { categoryName, playlists: playlists.playlists } };
};
87 changes: 87 additions & 0 deletions packages/nextjs/pages/playlists/[playlistId].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import Layout from "../../components/spotify/Layout";
import TracksTable from "../../components/spotify/TracksTable";
import { PlaylistType } from "../../types/spotify";
import { customGet } from "../../utils/beat-bridge/customGet";
import { isAuthenticated } from "../../utils/beat-bridge/isAuthenticated";
import parse from "html-react-parser";
import { GetServerSideProps } from "next";
import { getSession } from "next-auth/react";
import { RiMusic2Fill } from "react-icons/ri";
import DashboardLayout from "~~/components/dashboard/DashboardLayout";
import { MySession } from "~~/types/session";

interface IProps {
playlist: PlaylistType;
}

export default function Playlist({ playlist }: IProps) {
return (
<DashboardLayout>
<Layout title={`Beat Bridge - ${playlist?.name}`}>
<div className="flex items-end gap-6">
{playlist && (
<>
{playlist.images && playlist.images?.length > 0 ? (
<img src={playlist.images[0].url || ""} alt={playlist.name} className="object-contain w-60 h-60 " />
) : (
<div className="w-full h-40">
<RiMusic2Fill className="w-full h-full bg-paper " />
</div>
)}
<div className="flex flex-col gap-3">
<h5 className="text-xs font-bold uppercase">{playlist.type}</h5>
<h2 className="text-5xl font-bold">{playlist.name}</h2>

<p className="w-full text-xs leading-5">{parse(playlist.description || "")}</p>

<div className="flex items-center gap-5 text-sm">
<span className="font-bold">{playlist.owner?.display_name}</span>
{playlist?.followers?.total ? (
playlist?.followers?.total > 0 && (
<span className="text-gray">
{playlist.followers?.total.toLocaleString()} {playlist.followers?.total > 1 ? "likes" : "like"}
</span>
)
) : (
<span className="text-gray">0 likes</span>
)}
{playlist?.tracks?.items && playlist?.tracks?.items?.length > 0 && (
<span className="text-gray">{playlist.tracks.total.toLocaleString()} songs</span>
)}{" "}
</div>
</div>
</>
)}
</div>

<div className="mt-5">
<TracksTable
tracks={
playlist?.tracks?.items
? playlist.tracks.items.filter(item => item.track !== null).map(item => item.track)
: []
}
/>
</div>
</Layout>
</DashboardLayout>
);
}

export const getServerSideProps: GetServerSideProps = async ctx => {
const session = (await getSession(ctx)) as MySession | null;

if (!(await isAuthenticated(session))) {
return {
redirect: {
destination: "/login",
permanent: false,
},
};
}

const playlistId = ctx.params?.playlistId;
const playlist = await customGet(`https://api.spotify.com/v1/playlists/${playlistId}`, session);

return { props: { playlist: playlist } };
};
42 changes: 42 additions & 0 deletions packages/nextjs/pages/playlists/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { GetServerSideProps } from "next";
import { getSession } from "next-auth/react";
import DashboardLayout from "~~/components/dashboard/DashboardLayout";
import Heading from "~~/components/spotify/Heading";
import Layout from "~~/components/spotify/Layout";
import PlaylistList from "~~/components/spotify/PlaylistList";
import { MySession } from "~~/types/session";
import { PlaylistType } from "~~/types/spotify";
import { customGet } from "~~/utils/beat-bridge/customGet";
import { isAuthenticated } from "~~/utils/beat-bridge/isAuthenticated";

interface IProps {
userPlaylist: PlaylistType[];
}

export default function FollowedArtists({ userPlaylist }: IProps) {
return (
<DashboardLayout>
<Layout title="Beat Bridge - Your Playlist">
<Heading text="Your Playlist" />
<PlaylistList playlists={userPlaylist} />
</Layout>
</DashboardLayout>
);
}

export const getServerSideProps: GetServerSideProps = async ctx => {
const session = (await getSession(ctx)) as MySession | null;

if (!(await isAuthenticated(session))) {
return {
redirect: {
destination: "/login",
permanent: false,
},
};
}

const userPlaylist = await customGet(`https://api.spotify.com/v1/me/playlists?limit=50`, session);

return { props: { userPlaylist: userPlaylist.items } };
};
2 changes: 1 addition & 1 deletion packages/nextjs/pages/search/[query]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Fragment } from "react";
import Link from "next/link";
import { useSpotify } from "../../../context/SpotifyContext";
import { GetServerSideProps } from "next";
import { getSession } from "next-auth/react";
import DashboardLayout from "~~/components/dashboard/DashboardLayout";
Expand All @@ -9,6 +8,7 @@ import ArtistList from "~~/components/spotify/ArtistList";
import Heading from "~~/components/spotify/Heading";
import Layout from "~~/components/spotify/Layout";
import PlaylistList from "~~/components/spotify/PlaylistList";
import { useSpotify } from "~~/context/SpotifyContext";
import { MySession } from "~~/types/session";
import { SearchResults, Track } from "~~/types/spotify";
import { customGet } from "~~/utils/beat-bridge/customGet";
Expand Down
3 changes: 2 additions & 1 deletion packages/nextjs/pages/search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const getServerSideProps: GetServerSideProps = async ctx => {
};
}

const categories = await customGet("https://api.spotify.com/v1/browse/categories?limit=50&country=IN", session);
// const country = ctx.req.headers.get("cf-ipcountry") ?? "";
const categories = await customGet("https://api.spotify.com/v1/browse/categories?limit=50&country=NG", session);
return { props: { categories } };
};
2 changes: 1 addition & 1 deletion packages/nextjs/types/spotify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface PlaylistType {
description?: string;
id: string;
followers?: {
total?: number;
total: number;
};
images?: [Image];
name: string;
Expand Down

0 comments on commit 102e078

Please sign in to comment.