|
1 | | -import React from 'react'; |
| 1 | +import { useState, useEffect } from 'react'; |
| 2 | +import { axios } from 'axios'; |
| 3 | +import { useSelector } from 'react-redux'; |
2 | 4 |
|
3 | | -const CountryTracks = () => <div>CountryTracks</div>; |
| 5 | +import { Error, Loader, SongCard } from '../components'; |
| 6 | +import {useGetSongsForCountryQuery} from '../redux/services/shazamCore'; |
4 | 7 |
|
5 | | -export default CountryTracks; |
| 8 | +const AroundYou = () => { |
| 9 | + const [country, setcountry] = useState(''); |
| 10 | + const [loading, setLoading] = useState(true); |
| 11 | + const [activeSong, isPlaying] = useSelector((state) => state.player); |
| 12 | +const {data, isFetching, error} = useGetSongsForCountryQuery (country) |
| 13 | + |
| 14 | + useEffect(() => { |
| 15 | + axios |
| 16 | + .get(`https://geo.ipify.org/api/v2/country?apiKey=at_RQv8NLlRvVkI4Ibti3HqadiCwQxJm`) |
| 17 | + .then((res) => setCountry(res?.data?.location?.country)); |
| 18 | + .catch((err) => console.log(err)); |
| 19 | + .finally(() => setLoading(false)) |
| 20 | + }, [country]); |
| 21 | + |
| 22 | + if(isFetching && loading) return <Loader title="Loading songs around you"/>; |
| 23 | + |
| 24 | + if(error && country) return <Error/>; |
| 25 | + |
| 26 | + return <div className="flex flex-col"> |
| 27 | + <h2 className="font-bold text-3xl text-white text-left mt-4 mb-10">Around You <span className="font-black">{country}</span></h2> |
| 28 | + |
| 29 | + <div className="flex flex-wrap sm:justify-start justify-center gap-8"> |
| 30 | + {data?.map((song, i) => ( |
| 31 | + <SongCard |
| 32 | + key={song.key} |
| 33 | + song={song} |
| 34 | + isPlaying={isPlaying} |
| 35 | + activeSong={activeSong} |
| 36 | + data={data} |
| 37 | + i={i} |
| 38 | + /> |
| 39 | + ))} |
| 40 | + </div> |
| 41 | + </div> |
| 42 | + |
| 43 | + |
| 44 | +export default AroundYou; |
0 commit comments