Skip to content

Commit

Permalink
Add Search Page component
Browse files Browse the repository at this point in the history
  • Loading branch information
Stardust committed Nov 24, 2022
1 parent b41486f commit 1ff58e1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/pages/Search.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
const Search = () => (
<div>Search</div>
);
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 <Loader title="Loading top charts" />;
}

if (error) {
return <Error />;
}

return (
<div className="flex flex-col">
<h2 className="font-bold text-3xl text-white text-lfet mt-4 mb-10">
Showing result for <span className="font-black">{searchTerm}</span>
</h2>

<div className="flex flex-wrap sm:justify-start justify-center gap-8">
{songs?.map((song, i) => (
<SongCard
key={song.key}
song={song}
isPlaying={isPlaying}
activeSong={activeSong}
data={songs}
i={i}
/>
))}
</div>
</div>
);
};

export default Search;
5 changes: 5 additions & 0 deletions src/redux/services/shazamCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
}),
}),
});

Expand All @@ -40,4 +44,5 @@ export const {
useGetArtistDetailsQuery,
useGetSongsByCountryQuery,
useGetSongsByGenreQuery,
useGetSongsBySearchQuery,
} = shazamCoreApi;

0 comments on commit 1ff58e1

Please sign in to comment.