Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
2,859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const DashboardContainer = () => {
  2.   const { state } = useContext(SpotifyDataContext);
  3.   const windowWidth = useWindowSize();
  4.  
  5.   return (
  6.     <Dashboard>
  7.       <Header />
  8.       <div className="body" id="body">
  9.         {windowWidth.width > 900 ? <Sidebar /> : null}
  10.  
  11.         <ContentWrapper>
  12.           <TimerangeBar></TimerangeBar>
  13.           {windowWidth.width < 900 ? <MobileSidebar /> : null}
  14.  
  15.           {/* This KEY seems like a hack. However i didn't manage to find another way.*/}
  16.           <Content type={state.type} timeperiod={state.timeperiod} key={state.type} />
  17.         </ContentWrapper>
  18.       </div>
  19.     </Dashboard>
  20.   );
  21. };
  22.  
  23.  
  24.  
  25. const Content = ({ type, timeperiod }) => {
  26.   const { spotifyData } = useTopTracksAndArtists(type, timeperiod);
  27.  
  28.   console.log(spotifyData);
  29.  
  30.   const renderContent = () => {
  31.     if (spotifyData !== null) {
  32.       return spotifyData.items.map((item, index) => {
  33.         return (
  34.           <ContentItem key={item.id}>
  35.             <ItemImg imgUrl={type === "tracks" ? item.album.images : item.images} index={index + 1}></ItemImg>
  36.  
  37.             <ItemDetails
  38.               redirect={item.external_urls.spotify}
  39.               artist={type === "tracks" ? item.artists[0].name : ""}
  40.               title={item.name}
  41.               popularity={item.popularity}
  42.               index={index + 1}
  43.             />
  44.           </ContentItem>
  45.         );
  46.       });
  47.     } else {
  48.       return <Loader />;
  49.     }
  50.   };
  51.  
  52.   return (
  53.     <>
  54.       <div className="content__title">TOP {type === "tracks" ? "TRACKS" : "ARTISTS"}</div>
  55.       <ContentList>{<Loader />}</ContentList>
  56.     </>
  57.   );
  58. };
  59.  
  60.  
  61.  
  62. const useTopArtistsAndTracks = (type, time_range) => {
  63.   const [data, setData] = useState({
  64.     spotifyData: null,
  65.     loading: true,
  66.   });
  67.  
  68.   const TokenContext = useContext(context);
  69.  
  70.   useEffect(() => {
  71.     const getData = async () => {
  72.       try {
  73.         setData({ spotifyData: null, loading: true });
  74.         const response = await Axios.get(
  75.           `https://api.spotify.com/v1/me/top/${type}?limit=50&offset=0&time_range=${time_range}`,
  76.           {
  77.             headers: {
  78.               Authorization: `Bearer ${TokenContext.token}`,
  79.             },
  80.           }
  81.         );
  82.  
  83.         setData({ spotifyData: response.data, loading: false });
  84.       } catch (err) {
  85.         console.error(err);
  86.         Cookies.remove("token");
  87.       }
  88.     };
  89.  
  90.     getData();
  91.     // eslint-disable-next-line react-hooks/exhaustive-deps
  92.   }, [type, time_range]);
  93.  
  94.   return data;
  95. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement