Advertisement
Guest User

Untitled

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