Skip to content

Commit

Permalink
Merge pull request #5 from DevShruti/dev-shruti
Browse files Browse the repository at this point in the history
context
  • Loading branch information
shrutimalik123 committed Jan 10, 2022
2 parents fb1dd9f + ad7cee3 commit 588e6b5
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 7 deletions.
22 changes: 16 additions & 6 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import './App.css';
import Login from "./Login";
import { getTokenFromResponse } from "./spotify";
import SpotifyWebApi from "spotify-web-api-js"

import Player from "./Player";
import { useStateValue } from "./StateProvider";
const s = new SpotifyWebApi();

function App() {
const [ token , setToken] = useState(null);
const [{ user }, dispatch] = useStateValue();
useEffect(() => {
const hash = getTokenFromResponse();

Expand All @@ -17,21 +19,29 @@ function App() {
if (_token) {
setToken(_token)



s.setAccessToken(_token);
}


s.getMe().then((user) => {
console.log(user)
dispatch({
type: "SET_USER",
user: user,
});

});
}

console.log("i have a token", hash)
console.log("i have a token", hash)

}, []);


console.log("that is the user", user)
console.log("token" , token)

return (
<div className="app">
<Player/>
{ token ? (<h1>I am logged in</h1>):
(<Login/>)}

Expand Down
11 changes: 11 additions & 0 deletions src/Player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react'

function Player() {
return (
<div>
<h1>Welcome to Spotify</h1>
</div>
)
}

export default Player
11 changes: 11 additions & 0 deletions src/StateProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React, { createContext, useContext, useReducer } from "react";

export const StateContext = createContext();

export const StateProvider = ({ reducer, initialState, children }) => (
<StateContext.Provider value={useReducer(reducer, initialState)}>
{children}
</StateContext.Provider>
);

export const useStateValue = () => useContext(StateContext);
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { StateProvider } from "./StateProvider";
import reducer, { initialState } from "./reducer";

ReactDOM.render(
<React.StrictMode>
<App />
<StateProvider initialState={initialState} reducer={reducer}>
<App />
</StateProvider>
</React.StrictMode>,
document.getElementById('root')
);
Expand Down
28 changes: 28 additions & 0 deletions src/reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const initialState = {
user: null,
playlists: [],
spotify: null,
playing: false,
item: null,
token: " BQD88suew-8mIxZQYSMxkdTCvNo34_qXqzCjjIkly4yxjqehFRqAd3gaamcZ_gru-Cr2cRjBcvAhFVbB1Yiw5q20pFAB-Y4ILbXbi57erKdFgD-BQqzdxRwkbowwrAVuTGLE_MvUFDytfuAE_czu8e9LEPlzPiq6-pUHNYGRjHzR",
};

const reducer = (state, action) => {
console.log(action);
switch (action.type) {
case "SET_USER":
return {
...state,
user: action.user,
};

case "SET_TOKEN":
return {
...state,
token: action.token,
};

}
}

export default reducer;

0 comments on commit 588e6b5

Please sign in to comment.