├── .gitignore
├── README.md
├── package-lock.json
├── package.json
├── public
├── favicon.ico
├── index.html
├── logo192.png
├── logo512.png
├── manifest.json
└── robots.txt
└── src
├── App.css
├── App.js
├── Body.css
├── Body.js
├── Footer.css
├── Footer.js
├── Header.css
├── Header.js
├── Login.css
├── Login.js
├── Player.css
├── Player.js
├── Sidebar.css
├── Sidebar.js
├── SidebarOption.css
├── SidebarOption.js
├── SongRow.css
├── SongRow.js
├── StateProvider.js
├── index.css
├── index.js
├── reducer.js
├── serviceWorker.js
└── spotify.js
/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 |
8 | # testing
9 | /coverage
10 |
11 | # production
12 | /build
13 |
14 | # misc
15 | .DS_Store
16 | .env.local
17 | .env.development.local
18 | .env.test.local
19 | .env.production.local
20 |
21 | npm-debug.log*
22 | yarn-debug.log*
23 | yarn-error.log*
24 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
2 |
3 | ## Available Scripts
4 |
5 | In the project directory, you can run:
6 |
7 | ### `npm start`
8 |
9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11 |
12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console.
14 |
15 | ### `npm test`
16 |
17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19 |
20 | ### `npm run build`
21 |
22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance.
24 |
25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed!
27 |
28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29 |
30 | ### `npm run eject`
31 |
32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33 |
34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
35 |
36 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
37 |
38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
39 |
40 | ## Learn More
41 |
42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43 |
44 | To learn React, check out the [React documentation](https://reactjs.org/).
45 |
46 | ### Code Splitting
47 |
48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
49 |
50 | ### Analyzing the Bundle Size
51 |
52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
53 |
54 | ### Making a Progressive Web App
55 |
56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
57 |
58 | ### Advanced Configuration
59 |
60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
61 |
62 | ### Deployment
63 |
64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
65 |
66 | ### `npm run build` fails to minify
67 |
68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
69 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "spotify-clone",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@material-ui/core": "^4.11.0",
7 | "@material-ui/icons": "^4.9.1",
8 | "@testing-library/jest-dom": "^4.2.4",
9 | "@testing-library/react": "^9.5.0",
10 | "@testing-library/user-event": "^7.2.1",
11 | "react": "^16.13.1",
12 | "react-dom": "^16.13.1",
13 | "react-scripts": "3.4.1",
14 | "react-spotify-player": "^1.0.4",
15 | "spotify-web-api-js": "^1.5.0"
16 | },
17 | "scripts": {
18 | "start": "react-scripts start",
19 | "build": "react-scripts build",
20 | "test": "react-scripts test",
21 | "eject": "react-scripts eject"
22 | },
23 | "eslintConfig": {
24 | "extends": "react-app"
25 | },
26 | "browserslist": {
27 | "production": [
28 | ">0.2%",
29 | "not dead",
30 | "not op_mini all"
31 | ],
32 | "development": [
33 | "last 1 chrome version",
34 | "last 1 firefox version",
35 | "last 1 safari version"
36 | ]
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CleverProgrammers/spotify-clone/5f6021203f314c3ea209d397898620d3f8d8bc68/public/favicon.ico
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | React App
28 |
29 |
30 | You need to enable JavaScript to run this app.
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/public/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CleverProgrammers/spotify-clone/5f6021203f314c3ea209d397898620d3f8d8bc68/public/logo192.png
--------------------------------------------------------------------------------
/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CleverProgrammers/spotify-clone/5f6021203f314c3ea209d397898620d3f8d8bc68/public/logo512.png
--------------------------------------------------------------------------------
/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | },
10 | {
11 | "src": "logo192.png",
12 | "type": "image/png",
13 | "sizes": "192x192"
14 | },
15 | {
16 | "src": "logo512.png",
17 | "type": "image/png",
18 | "sizes": "512x512"
19 | }
20 | ],
21 | "start_url": ".",
22 | "display": "standalone",
23 | "theme_color": "#000000",
24 | "background_color": "#ffffff"
25 | }
26 |
--------------------------------------------------------------------------------
/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/src/App.css:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CleverProgrammers/spotify-clone/5f6021203f314c3ea209d397898620d3f8d8bc68/src/App.css
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import React, { useEffect } from "react";
2 | import SpotifyWebApi from "spotify-web-api-js";
3 | import { useStateValue } from "./StateProvider";
4 | import Player from "./Player";
5 | import { getTokenFromResponse } from "./spotify";
6 | import "./App.css";
7 | import Login from "./Login";
8 |
9 | const s = new SpotifyWebApi();
10 |
11 | function App() {
12 | const [{ token }, dispatch] = useStateValue();
13 |
14 | useEffect(() => {
15 | // Set token
16 | const hash = getTokenFromResponse();
17 | window.location.hash = "";
18 | let _token = hash.access_token;
19 |
20 | if (_token) {
21 | s.setAccessToken(_token);
22 |
23 | dispatch({
24 | type: "SET_TOKEN",
25 | token: _token,
26 | });
27 |
28 | s.getPlaylist("37i9dQZEVXcJZyENOWUFo7").then((response) =>
29 | dispatch({
30 | type: "SET_DISCOVER_WEEKLY",
31 | discover_weekly: response,
32 | })
33 | );
34 |
35 | s.getMyTopArtists().then((response) =>
36 | dispatch({
37 | type: "SET_TOP_ARTISTS",
38 | top_artists: response,
39 | })
40 | );
41 |
42 | dispatch({
43 | type: "SET_SPOTIFY",
44 | spotify: s,
45 | });
46 |
47 | s.getMe().then((user) => {
48 | dispatch({
49 | type: "SET_USER",
50 | user,
51 | });
52 | });
53 |
54 | s.getUserPlaylists().then((playlists) => {
55 | dispatch({
56 | type: "SET_PLAYLISTS",
57 | playlists,
58 | });
59 | });
60 | }
61 | }, [token, dispatch]);
62 |
63 | return (
64 |
65 | {!token &&
}
66 | {token &&
}
67 |
68 | );
69 | }
70 |
71 | export default App;
72 |
--------------------------------------------------------------------------------
/src/Body.css:
--------------------------------------------------------------------------------
1 | .body {
2 | padding: 30px;
3 | width: 100%;
4 | height: 100vh;
5 | overflow-y: overlay;
6 | flex: 0.8;
7 | color: white;
8 | background: linear-gradient(transparent, rgba(0, 0, 0, 1));
9 | background-color: rgb(91, 87, 115);
10 | }
11 |
12 | .body::-webkit-scrollbar {
13 | display: none;
14 | }
15 |
16 | .body__info {
17 | display: flex;
18 | align-items: flex-end;
19 | padding: 10px;
20 | }
21 |
22 | .body__shuffle {
23 | font-size: 80px !important;
24 | margin-left: 50px;
25 | margin-top: 20px;
26 | margin-bottom: 20px;
27 | }
28 |
29 | .body__icons {
30 | display: flex;
31 | align-items: center;
32 | }
33 |
34 | .body__icons > .MuiSvgIcon-root {
35 | margin-right: 20px;
36 | }
37 |
38 | .body__shuffle:hover {
39 | transition: 100ms transform ease-in;
40 | transform: scale(1.08);
41 | }
42 |
43 | .body__background {
44 | position: absolute;
45 | top: 13vh;
46 | right: 0;
47 | opacity: 0.4;
48 | height: 80%;
49 | width: 80%;
50 | background-color: black;
51 | }
52 |
53 | .body__songs {
54 | z-index: 1;
55 | margin: 20px -30px;
56 | }
57 |
58 | .body__info > img {
59 | height: 20vw;
60 | margin: 0 20px;
61 | box-shadow: 0 4px 60px rgba(0, 0, 0, 0.5);
62 | }
63 |
64 | .body__infoText {
65 | flex: 1;
66 | }
67 |
68 | .body__infoText > h2 {
69 | font-size: 48px;
70 | margin-bottom: 10px;
71 | }
72 |
73 | .body__infoText > p {
74 | font-size: 14px;
75 | }
76 |
--------------------------------------------------------------------------------
/src/Body.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import "./Body.css";
3 | import Header from "./Header";
4 | import { useStateValue } from "./StateProvider";
5 | import SongRow from "./SongRow";
6 | import PlayCircleFilledIcon from "@material-ui/icons/PlayCircleFilled";
7 | import FavoriteIcon from "@material-ui/icons/Favorite";
8 | import MoreHorizIcon from "@material-ui/icons/MoreHoriz";
9 |
10 | function Body({ spotify }) {
11 | const [{ discover_weekly }, dispatch] = useStateValue();
12 |
13 | const playPlaylist = (id) => {
14 | spotify
15 | .play({
16 | context_uri: `spotify:playlist:37i9dQZEVXcJZyENOWUFo7`,
17 | })
18 | .then((res) => {
19 | spotify.getMyCurrentPlayingTrack().then((r) => {
20 | dispatch({
21 | type: "SET_ITEM",
22 | item: r.item,
23 | });
24 | dispatch({
25 | type: "SET_PLAYING",
26 | playing: true,
27 | });
28 | });
29 | });
30 | };
31 |
32 | const playSong = (id) => {
33 | spotify
34 | .play({
35 | uris: [`spotify:track:${id}`],
36 | })
37 | .then((res) => {
38 | spotify.getMyCurrentPlayingTrack().then((r) => {
39 | dispatch({
40 | type: "SET_ITEM",
41 | item: r.item,
42 | });
43 | dispatch({
44 | type: "SET_PLAYING",
45 | playing: true,
46 | });
47 | });
48 | });
49 | };
50 |
51 | return (
52 |
53 |
54 |
55 |
56 |
57 |
58 |
PLAYLIST
59 |
Discover Weekly
60 |
{discover_weekly?.description}
61 |
62 |
63 |
64 |
65 |
73 |
74 | {discover_weekly?.tracks.items.map((item) => (
75 |
76 | ))}
77 |
78 |
79 | );
80 | }
81 |
82 | export default Body;
83 |
--------------------------------------------------------------------------------
/src/Footer.css:
--------------------------------------------------------------------------------
1 | .footer {
2 | position: fixed;
3 | display: flex;
4 | justify-content: space-between;
5 | bottom: 0;
6 | padding: 20px;
7 | height: 65px;
8 | width: 97%;
9 | background-color: #282828;
10 | }
11 |
12 | .footer__right * .MuiSlider-root {
13 | color: green;
14 | }
15 | .footer__left {
16 | display: flex;
17 | align-items: center;
18 | color: white;
19 | width: 300px;
20 | flex: 0.3;
21 | }
22 |
23 | .footer__green {
24 | color: #1ed15e;
25 | }
26 |
27 | .footer__center {
28 | flex: 0.4;
29 | max-width: 300px;
30 | padding: 0 100px;
31 | color: white;
32 | display: flex;
33 | align-items: center;
34 | justify-content: space-between;
35 | }
36 |
37 | .footer__icon:hover,
38 | .footer__green:hover {
39 | transition: transform 0.2s ease-in-out;
40 | transform: scale(1.2) !important;
41 | }
42 |
43 | .footer__right {
44 | display: flex;
45 | justify-content: space-between;
46 | align-items: center;
47 | color: white;
48 | flex: 0.3;
49 | }
50 |
51 | .footer__albumLogo {
52 | height: 60px;
53 | width: 60px;
54 | margin-right: 20px;
55 | object-fit: contain;
56 | }
57 |
58 | .footer__songInfo > h4 {
59 | margin-bottom: 5px;
60 | }
61 |
62 | .footer__songInfo > p {
63 | font-size: 12px;
64 | }
65 |
--------------------------------------------------------------------------------
/src/Footer.js:
--------------------------------------------------------------------------------
1 | import React, { useEffect, useState } from "react";
2 | import { useStateValue } from "./StateProvider";
3 | import PlayCircleOutlineIcon from "@material-ui/icons/PlayCircleOutline";
4 | import SkipPreviousIcon from "@material-ui/icons/SkipPrevious";
5 | import SkipNextIcon from "@material-ui/icons/SkipNext";
6 | import ShuffleIcon from "@material-ui/icons/Shuffle";
7 | import RepeatIcon from "@material-ui/icons/Repeat";
8 | import VolumeDownIcon from "@material-ui/icons/VolumeDown";
9 | import PauseCircleOutlineIcon from "@material-ui/icons/PauseCircleOutline";
10 | import PlaylistPlayIcon from "@material-ui/icons/PlaylistPlay";
11 | import "./Footer.css";
12 | import { Grid, Slider } from "@material-ui/core";
13 |
14 | function Footer({ spotify }) {
15 | const [{ token, item, playing }, dispatch] = useStateValue();
16 |
17 | useEffect(() => {
18 | spotify.getMyCurrentPlaybackState().then((r) => {
19 | console.log(r);
20 |
21 | dispatch({
22 | type: "SET_PLAYING",
23 | playing: r.is_playing,
24 | });
25 |
26 | dispatch({
27 | type: "SET_ITEM",
28 | item: r.item,
29 | });
30 | });
31 | }, [spotify]);
32 |
33 | const handlePlayPause = () => {
34 | if (playing) {
35 | spotify.pause();
36 | dispatch({
37 | type: "SET_PLAYING",
38 | playing: false,
39 | });
40 | } else {
41 | spotify.play();
42 | dispatch({
43 | type: "SET_PLAYING",
44 | playing: true,
45 | });
46 | }
47 | };
48 |
49 | const skipNext = () => {
50 | spotify.skipToNext();
51 | spotify.getMyCurrentPlayingTrack().then((r) => {
52 | dispatch({
53 | type: "SET_ITEM",
54 | item: r.item,
55 | });
56 | dispatch({
57 | type: "SET_PLAYING",
58 | playing: true,
59 | });
60 | });
61 | };
62 |
63 | const skipPrevious = () => {
64 | spotify.skipToPrevious();
65 | spotify.getMyCurrentPlayingTrack().then((r) => {
66 | dispatch({
67 | type: "SET_ITEM",
68 | item: r.item,
69 | });
70 | dispatch({
71 | type: "SET_PLAYING",
72 | playing: true,
73 | });
74 | });
75 | };
76 |
77 | return (
78 |
79 |
80 |
85 | {item ? (
86 |
87 |
{item.name}
88 |
{item.artists.map((artist) => artist.name).join(", ")}
89 |
90 | ) : (
91 |
92 |
No song is playing
93 |
...
94 |
95 | )}
96 |
97 |
98 |
99 |
100 |
101 | {playing ? (
102 |
107 | ) : (
108 |
113 | )}
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 | );
132 | }
133 |
134 | export default Footer;
135 |
--------------------------------------------------------------------------------
/src/Header.css:
--------------------------------------------------------------------------------
1 | .header {
2 | display: flex;
3 | justify-content: space-between;
4 | margin-bottom: 30px;
5 | }
6 |
7 | .header__right {
8 | display: flex;
9 | align-items: center;
10 | }
11 |
12 | .header__left {
13 | flex: 0.5;
14 | display: flex;
15 | background-color: white;
16 | padding: 10px;
17 | border-radius: 30px;
18 | color: gray;
19 | align-items: center;
20 | }
21 |
22 | .header__left > input {
23 | border: none;
24 | width: 100%;
25 | }
26 |
27 | .header__right > h4 {
28 | margin-left: 10px;
29 | }
30 |
--------------------------------------------------------------------------------
/src/Header.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import "./Header.css";
3 | import { useStateValue } from "./StateProvider";
4 | import { Avatar } from "@material-ui/core";
5 | import SearchIcon from "@material-ui/icons/Search";
6 | import { useEffect } from "react";
7 |
8 | function Header({ spotify }) {
9 | const [{ user }, dispatch] = useStateValue();
10 |
11 | return (
12 |
13 |
14 |
15 |
19 |
20 |
21 |
22 |
{user?.display_name}
23 |
24 |
25 | );
26 | }
27 |
28 | export default Header;
29 |
--------------------------------------------------------------------------------
/src/Login.css:
--------------------------------------------------------------------------------
1 | .login {
2 | display: grid;
3 | background-color: black;
4 | height: 100vh;
5 | place-items: center;
6 | }
7 |
8 | .login > img {
9 | height: 200px;
10 | }
11 |
12 | .login > a {
13 | color: white;
14 | text-decoration: none;
15 | padding: 20px;
16 | border-radius: 99px;
17 | font-weight: 800;
18 | background-color: #1db954;
19 | }
20 |
--------------------------------------------------------------------------------
/src/Login.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import "./Login.css";
3 | import { accessUrl } from "./spotify";
4 |
5 | function Login() {
6 | return (
7 |
14 | );
15 | }
16 |
17 | export default Login;
18 |
--------------------------------------------------------------------------------
/src/Player.css:
--------------------------------------------------------------------------------
1 | .player {
2 | display: flex;
3 | flex-direction: column;
4 | }
5 |
6 | .player__body {
7 | display: flex;
8 | flex-grow: 1;
9 | }
10 |
--------------------------------------------------------------------------------
/src/Player.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import Footer from "./Footer";
3 | import "./Player.css";
4 | import Sidebar from "./Sidebar";
5 | import Body from "./Body";
6 |
7 | function Player({ spotify }) {
8 | return (
9 |
14 |
15 |
16 | );
17 | }
18 |
19 | export default Player;
20 |
--------------------------------------------------------------------------------
/src/Sidebar.css:
--------------------------------------------------------------------------------
1 | .sidebar {
2 | display: flex;
3 | flex-direction: column;
4 | flex: 0.2;
5 | padding-left: 10px;
6 | padding-right: 10px;
7 | min-width: 230px;
8 | height: 100vh;
9 | color: white;
10 | background-color: #040404;
11 | }
12 |
13 | .sidebar__title {
14 | margin-left: 10px;
15 | padding: 5px;
16 | font-size: 12px;
17 | }
18 |
19 | .sidebar__logo {
20 | margin-right: auto;
21 | object-fit: contain;
22 | height: 70px;
23 | padding: 10px;
24 | }
25 |
--------------------------------------------------------------------------------
/src/Sidebar.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import "./Sidebar.css";
3 | import SidebarOption from "./SidebarOption";
4 | import HomeIcon from "@material-ui/icons/Home";
5 | import SearchIcon from "@material-ui/icons/Search";
6 | import LibraryMusicIcon from "@material-ui/icons/LibraryMusic";
7 | import { getTokenFromResponse } from "./spotify";
8 | import { useStateValue } from "./StateProvider";
9 |
10 | function Sidebar() {
11 | const [{ playlists }, dispatch] = useStateValue();
12 | console.log(playlists);
13 |
14 | return (
15 |
16 |
21 |
22 |
23 |
24 |
25 |
PLAYLISTS
26 |
27 | {playlists?.items?.map((playlist) => (
28 |
29 | ))}
30 |
31 | );
32 | }
33 |
34 | export default Sidebar;
35 |
--------------------------------------------------------------------------------
/src/SidebarOption.css:
--------------------------------------------------------------------------------
1 | .sidebarOption {
2 | display: flex;
3 | cursor: pointer;
4 | align-items: center;
5 | height: 40px;
6 | color: grey;
7 | transition: 200ms color ease-in;
8 | }
9 |
10 | .sidebarOption:hover {
11 | color: white;
12 | }
13 |
14 | .sidebar > hr {
15 | border: 1px solid #282828;
16 | width: 90%;
17 | margin: 10px auto;
18 | }
19 |
20 | .sidebarOption__icon {
21 | padding-left: 10px;
22 | padding-right: 10px;
23 | }
24 |
25 | .sidebarOption > p {
26 | margin-top: 10px;
27 | margin-left: 20px;
28 | font-size: 14px;
29 | }
30 |
--------------------------------------------------------------------------------
/src/SidebarOption.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import "./SidebarOption.css";
3 |
4 | function SidebarOption({ option = "test", Icon }) {
5 | return (
6 |
7 | {Icon &&
}
8 | {Icon ?
{option} :
{option}
}
9 |
10 | );
11 | }
12 |
13 | export default SidebarOption;
14 |
--------------------------------------------------------------------------------
/src/SongRow.css:
--------------------------------------------------------------------------------
1 | .songRow {
2 | margin-left: 20px;
3 | padding: 20px;
4 | display: flex;
5 | align-items: center;
6 | z-index: 100;
7 | color: white;
8 | }
9 |
10 | .songRow:hover {
11 | cursor: pointer;
12 | background-color: black;
13 | opacity: 0.8;
14 | }
15 |
16 | .songRow__info {
17 | margin-left: 20px;
18 | }
19 |
20 | .songRow__info > h1 {
21 | font-size: 16px;
22 | }
23 |
24 | .songRow__info > p {
25 | font-size: 14px;
26 | margin-top: 3px;
27 | color: gray;
28 | }
29 |
30 | .songRow__album {
31 | height: 40px;
32 | width: 40px;
33 | }
34 |
--------------------------------------------------------------------------------
/src/SongRow.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import "./SongRow.css";
3 |
4 | function SongRow({ track, playSong }) {
5 | console.log(track);
6 | return (
7 | playSong(track.id)}>
8 |
9 |
10 |
{track.name}
11 |
12 | {track.artists.map((artist) => artist.name).join(", ")} -{" "}
13 | {track.album.name}
14 |
15 |
16 |
17 | );
18 | }
19 |
20 | export default SongRow;
21 |
--------------------------------------------------------------------------------
/src/StateProvider.js:
--------------------------------------------------------------------------------
1 | import React, { createContext, useContext, useReducer } from "react";
2 |
3 | export const StateContext = createContext();
4 |
5 | export const StateProvider = ({ reducer, initialState, children }) => (
6 |
7 | {children}
8 |
9 | );
10 |
11 | export const useStateValue = () => useContext(StateContext);
12 |
--------------------------------------------------------------------------------
/src/index.css:
--------------------------------------------------------------------------------
1 | body::-webkit-scrollbar {
2 | display: none;
3 | }
4 |
5 | * {
6 | margin: 0;
7 | }
8 |
9 | body {
10 | margin: 0;
11 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
12 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
13 | sans-serif;
14 | -webkit-font-smoothing: antialiased;
15 | -moz-osx-font-smoothing: grayscale;
16 | }
17 |
18 | code {
19 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
20 | monospace;
21 | }
22 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import ReactDOM from "react-dom";
3 | import "./index.css";
4 | import App from "./App";
5 | import * as serviceWorker from "./serviceWorker";
6 | import { StateProvider } from "./StateProvider";
7 | import reducer, { initialState } from "./reducer";
8 |
9 | ReactDOM.render(
10 |
11 |
12 |
13 |
14 | ,
15 | document.getElementById("root")
16 | );
17 |
18 | // If you want your app to work offline and load faster, you can change
19 | // unregister() to register() below. Note this comes with some pitfalls.
20 | // Learn more about service workers: https://bit.ly/CRA-PWA
21 | serviceWorker.unregister();
22 |
--------------------------------------------------------------------------------
/src/reducer.js:
--------------------------------------------------------------------------------
1 | import { findAllByDisplayValue } from "@testing-library/react";
2 |
3 | export const initialState = {
4 | user: null,
5 | playlists: [],
6 | spotify: null,
7 | discover_weekly: null,
8 | top_artists: null,
9 | playing: false,
10 | item: null,
11 | };
12 |
13 | const reducer = (state, action) => {
14 | console.log(action);
15 | switch (action.type) {
16 | case "SET_USER":
17 | return {
18 | ...state,
19 | user: action.user,
20 | };
21 |
22 | case "SET_PLAYING":
23 | return {
24 | ...state,
25 | playing: action.playing,
26 | };
27 |
28 | case "SET_ITEM":
29 | return {
30 | ...state,
31 | item: action.item,
32 | };
33 |
34 | case "SET_DISCOVER_WEEKLY":
35 | return {
36 | ...state,
37 | discover_weekly: action.discover_weekly,
38 | };
39 |
40 | case "SET_TOP_ARTISTS":
41 | return {
42 | ...state,
43 | top_artists: action.top_artists,
44 | };
45 |
46 | case "SET_TOKEN":
47 | return {
48 | ...state,
49 | token: action.token,
50 | };
51 |
52 | case "SET_SPOTIFY":
53 | return {
54 | ...state,
55 | spotify: action.spotify,
56 | };
57 |
58 | case "SET_PLAYLISTS":
59 | return {
60 | ...state,
61 | playlists: action.playlists,
62 | };
63 | default:
64 | return state;
65 | }
66 | };
67 |
68 | export default reducer;
69 |
--------------------------------------------------------------------------------
/src/serviceWorker.js:
--------------------------------------------------------------------------------
1 | // This optional code is used to register a service worker.
2 | // register() is not called by default.
3 |
4 | // This lets the app load faster on subsequent visits in production, and gives
5 | // it offline capabilities. However, it also means that developers (and users)
6 | // will only see deployed updates on subsequent visits to a page, after all the
7 | // existing tabs open on the page have been closed, since previously cached
8 | // resources are updated in the background.
9 |
10 | // To learn more about the benefits of this model and instructions on how to
11 | // opt-in, read https://bit.ly/CRA-PWA
12 |
13 | const isLocalhost = Boolean(
14 | window.location.hostname === 'localhost' ||
15 | // [::1] is the IPv6 localhost address.
16 | window.location.hostname === '[::1]' ||
17 | // 127.0.0.0/8 are considered localhost for IPv4.
18 | window.location.hostname.match(
19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
20 | )
21 | );
22 |
23 | export function register(config) {
24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
25 | // The URL constructor is available in all browsers that support SW.
26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
27 | if (publicUrl.origin !== window.location.origin) {
28 | // Our service worker won't work if PUBLIC_URL is on a different origin
29 | // from what our page is served on. This might happen if a CDN is used to
30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374
31 | return;
32 | }
33 |
34 | window.addEventListener('load', () => {
35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
36 |
37 | if (isLocalhost) {
38 | // This is running on localhost. Let's check if a service worker still exists or not.
39 | checkValidServiceWorker(swUrl, config);
40 |
41 | // Add some additional logging to localhost, pointing developers to the
42 | // service worker/PWA documentation.
43 | navigator.serviceWorker.ready.then(() => {
44 | console.log(
45 | 'This web app is being served cache-first by a service ' +
46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA'
47 | );
48 | });
49 | } else {
50 | // Is not localhost. Just register service worker
51 | registerValidSW(swUrl, config);
52 | }
53 | });
54 | }
55 | }
56 |
57 | function registerValidSW(swUrl, config) {
58 | navigator.serviceWorker
59 | .register(swUrl)
60 | .then(registration => {
61 | registration.onupdatefound = () => {
62 | const installingWorker = registration.installing;
63 | if (installingWorker == null) {
64 | return;
65 | }
66 | installingWorker.onstatechange = () => {
67 | if (installingWorker.state === 'installed') {
68 | if (navigator.serviceWorker.controller) {
69 | // At this point, the updated precached content has been fetched,
70 | // but the previous service worker will still serve the older
71 | // content until all client tabs are closed.
72 | console.log(
73 | 'New content is available and will be used when all ' +
74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
75 | );
76 |
77 | // Execute callback
78 | if (config && config.onUpdate) {
79 | config.onUpdate(registration);
80 | }
81 | } else {
82 | // At this point, everything has been precached.
83 | // It's the perfect time to display a
84 | // "Content is cached for offline use." message.
85 | console.log('Content is cached for offline use.');
86 |
87 | // Execute callback
88 | if (config && config.onSuccess) {
89 | config.onSuccess(registration);
90 | }
91 | }
92 | }
93 | };
94 | };
95 | })
96 | .catch(error => {
97 | console.error('Error during service worker registration:', error);
98 | });
99 | }
100 |
101 | function checkValidServiceWorker(swUrl, config) {
102 | // Check if the service worker can be found. If it can't reload the page.
103 | fetch(swUrl, {
104 | headers: { 'Service-Worker': 'script' },
105 | })
106 | .then(response => {
107 | // Ensure service worker exists, and that we really are getting a JS file.
108 | const contentType = response.headers.get('content-type');
109 | if (
110 | response.status === 404 ||
111 | (contentType != null && contentType.indexOf('javascript') === -1)
112 | ) {
113 | // No service worker found. Probably a different app. Reload the page.
114 | navigator.serviceWorker.ready.then(registration => {
115 | registration.unregister().then(() => {
116 | window.location.reload();
117 | });
118 | });
119 | } else {
120 | // Service worker found. Proceed as normal.
121 | registerValidSW(swUrl, config);
122 | }
123 | })
124 | .catch(() => {
125 | console.log(
126 | 'No internet connection found. App is running in offline mode.'
127 | );
128 | });
129 | }
130 |
131 | export function unregister() {
132 | if ('serviceWorker' in navigator) {
133 | navigator.serviceWorker.ready
134 | .then(registration => {
135 | registration.unregister();
136 | })
137 | .catch(error => {
138 | console.error(error.message);
139 | });
140 | }
141 | }
142 |
--------------------------------------------------------------------------------
/src/spotify.js:
--------------------------------------------------------------------------------
1 | // https://developer.spotify.com/documentation/web-playback-sdk/quick-start/#
2 | export const authEndpoint = "https://accounts.spotify.com/authorize";
3 | // Replace with your app's client ID, redirect URI and desired scopes
4 | const clientId = "cff76213089a4e228206c8bf120d67cb";
5 | const redirectUri = "http://localhost:3000/";
6 | const scopes = [
7 | "user-read-currently-playing",
8 | "user-read-recently-played",
9 | "user-read-playback-state",
10 | "user-top-read",
11 | "user-modify-playback-state",
12 | ];
13 |
14 | export const getTokenFromResponse = () => {
15 | return window.location.hash
16 | .substring(1)
17 | .split("&")
18 | .reduce((initial, item) => {
19 | var parts = item.split("=");
20 | initial[parts[0]] = decodeURIComponent(parts[1]);
21 |
22 | return initial;
23 | }, {});
24 | };
25 |
26 | export const accessUrl = `${authEndpoint}?client_id=${clientId}&redirect_uri=${redirectUri}&scope=${scopes.join(
27 | "%20"
28 | )}&response_type=token&show_dialog=true`;
29 |
--------------------------------------------------------------------------------