react-code-dataset
/
Spotify-clone-using-react-redux
/src
/pages
/Dashboard
/Footer
/PlayerControls.jsx
import React from "react"; | |
import styled from "styled-components"; | |
import { | |
BsFillPlayCircleFill, | |
BsFillPauseCircleFill, | |
BsShuffle, | |
} from "react-icons/bs"; | |
import { CgPlayTrackNext, CgPlayTrackPrev } from "react-icons/cg"; | |
import { FiRepeat } from "react-icons/fi"; | |
import axios from "axios"; | |
import { useSelector } from "react-redux"; | |
export default function PlayerControls() { | |
const { token } = useSelector((state) => state.user); | |
const [playerState, setPlayerState] = React.useState(false); | |
const changeState = async () => { | |
const state = playerState ? "pause" : "play"; | |
try { | |
await axios.put( | |
`https://api.spotify.com/v1/me/player/${state}`, | |
{}, | |
{ | |
headers: { | |
"Content-Type": "application/json", | |
Authorization: "Bearer " + token, | |
}, | |
} | |
); | |
} catch (error) {} | |
setPlayerState(!playerState); | |
}; | |
const changeTrack = async (type) => { | |
try { | |
await axios.post( | |
`https://api.spotify.com/v1/me/player/${type}`, | |
{}, | |
{ | |
headers: { | |
"Content-Type": "application/json", | |
Authorization: "Bearer " + token, | |
}, | |
} | |
); | |
const response1 = await axios.get( | |
"https://api.spotify.com/v1/me/player/currently-playing", | |
{ | |
headers: { | |
"Content-Type": "application/json", | |
Authorization: "Bearer " + token, | |
}, | |
} | |
); | |
console.log(response1); | |
} catch (error) {} | |
}; | |
return ( | |
<Container> | |
<div className="shuffle"> | |
<BsShuffle /> | |
</div> | |
<div className="previous"> | |
<CgPlayTrackPrev onClick={() => changeTrack("previous")} /> | |
</div> | |
<div className="state"> | |
{playerState ? ( | |
<BsFillPauseCircleFill onClick={changeState} /> | |
) : ( | |
<BsFillPlayCircleFill onClick={changeState} /> | |
)} | |
</div> | |
<div className="next"> | |
<CgPlayTrackNext onClick={() => changeTrack("next")} /> | |
</div> | |
<div className="repeat"> | |
<FiRepeat /> | |
</div> | |
</Container> | |
); | |
} | |
const Container = styled.div` | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
gap: 2rem; | |
svg { | |
color: #b3b3b3; | |
transition: 0.2s ease-in-out; | |
&:hover { | |
color: white; | |
} | |
} | |
.state { | |
svg { | |
color: white; | |
} | |
} | |
.previous, | |
.next, | |
.state { | |
font-size: 2rem; | |
} | |
`; | |