Skip to content

Commit

Permalink
feat(home): add page to query api
Browse files Browse the repository at this point in the history
  • Loading branch information
iqbalpa committed Jul 10, 2024
1 parent 2f07063 commit 7f3bb11
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
18 changes: 12 additions & 6 deletions src/api/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ const getAllMovies = async (currentPage: number): Promise<Movie[]> => {
return movies;
};

const getMoviesWithQuery = async (query: string): Promise<Movie[]> => {
const getMoviesWithQuery = async (
query: string,
currentPage: number,
): Promise<Movie[]> => {
const parsedQuery = parseQuery(query);
const res = await axios.get(`${BASE_URL}search/movie?query=${parsedQuery}`, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${API_KEY}`,
const res = await axios.get(
`${BASE_URL}search/movie?query=${parsedQuery}&page=${currentPage}`,
{
headers: {
Accept: 'application/json',
Authorization: `Bearer ${API_KEY}`,
},
},
});
);
const movies: Movie[] = res.data.results;
return movies;
};
Expand Down
7 changes: 5 additions & 2 deletions src/modules/home/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const HomeModule: React.FC = () => {
const [query, setQuery] = useState<string>('');

useEffect(() => {
if (query !== '') {
return;
}
const fetchMovies = async () => {
try {
const res: Movie[] = await api.getAllMovies(currentPage);
Expand All @@ -38,14 +41,14 @@ const HomeModule: React.FC = () => {
}
const fetchMovies = async () => {
try {
const res: Movie[] = await api.getMoviesWithQuery(query);
const res: Movie[] = await api.getMoviesWithQuery(query, currentPage);
setMovies(res);
} catch (e) {
console.log('failed to fetch the movies');
}
};
fetchMovies();
}, [query]);
}, [query, currentPage]);

const handleClickPrev = () => {
setCurrentPage((prevPage) => {
Expand Down

0 comments on commit 7f3bb11

Please sign in to comment.