From 1ff58e1c7791db4cb98e04f6026a311a5a673caa Mon Sep 17 00:00:00 2001 From: Stardust Date: Thu, 24 Nov 2022 08:59:42 -0600 Subject: [PATCH] Add Search Page component --- src/pages/Search.jsx | 45 +++++++++++++++++++++++++++++--- src/redux/services/shazamCore.js | 5 ++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/pages/Search.jsx b/src/pages/Search.jsx index 3223815..7288e42 100644 --- a/src/pages/Search.jsx +++ b/src/pages/Search.jsx @@ -1,5 +1,44 @@ -const Search = () => ( -
Search
-); +import { useSelector } from 'react-redux'; +import { useParams } from 'react-router-dom'; + +import { Error, Loader, SongCard } from '../components'; +import { useGetSongsBySearchQuery } from '../redux/services/shazamCore'; + +const Search = () => { + const { searchTerm } = useParams(); + const { activeSong, isPlaying } = useSelector((state) => state.player); + const { data, isFetching, error } = useGetSongsBySearchQuery(searchTerm); + + const songs = data?.tracks?.hits?.map((song) => song.track); + + if (isFetching) { + return ; + } + + if (error) { + return ; + } + + return ( +
+

+ Showing result for {searchTerm} +

+ +
+ {songs?.map((song, i) => ( + + ))} +
+
+ ); +}; export default Search; diff --git a/src/redux/services/shazamCore.js b/src/redux/services/shazamCore.js index d883d79..918ba84 100644 --- a/src/redux/services/shazamCore.js +++ b/src/redux/services/shazamCore.js @@ -30,6 +30,10 @@ export const shazamCoreApi = createApi({ getSongsByGenre: builder.query({ query: (genre) => `/charts/genre-world?genre_code=${genre}`, }), + getSongsBySearch: builder.query({ + query: (searchTerm) => + `/search/multi?search_type=SONGS_ARTISTS&query=${searchTerm}`, + }), }), }); @@ -40,4 +44,5 @@ export const { useGetArtistDetailsQuery, useGetSongsByCountryQuery, useGetSongsByGenreQuery, + useGetSongsBySearchQuery, } = shazamCoreApi;