Skip to content

Commit

Permalink
feat(home): create search bar
Browse files Browse the repository at this point in the history
  • Loading branch information
iqbalpa committed Jul 10, 2024
1 parent 76ba3f4 commit 7769a4e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
28 changes: 28 additions & 0 deletions src/components/searchBar/searchBar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, { ChangeEvent } from 'react';
import { Search } from 'lucide-react';

interface ISearchBar {
query: string;
handleQueryChange: (e: ChangeEvent<HTMLInputElement>) => void;
}

const SearchBar: React.FC<ISearchBar> = ({ query, handleQueryChange }) => {
return (
<div className="relative flex w-full justify-center bg-slate-800 p-4">
<div className="relative w-full max-w-md">
<Search
size={24}
className="absolute left-4 top-1/2 -translate-y-1/2 transform text-slate-500"
/>
<input
placeholder="Search"
className="w-full rounded-full border-2 border-slate-300 py-2 pl-12 pr-4 focus:border-slate-500"
value={query}
onChange={handleQueryChange}
/>
</div>
</div>
);
};

export default SearchBar;
13 changes: 9 additions & 4 deletions src/modules/home/home.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
'use client';

import React, { useEffect, useState } from 'react';
import React, { ChangeEvent, useEffect, useState } from 'react';
import api from '@/api/api';
import { Movie } from '@/constant/movie';
import Header from '@/components/header/header';
import Backdrop from '@/components/backdrop/backdrop';
import ListMovies from '@/components/listMovies/listMovies';
import MyPagination from '@/components/myPagination/myPagination';
import SearchBar from '@/components/searchBar/searchBar';

const HomeModule: React.FC = () => {
const [movies, setMovies] = useState<Movie[]>([]);
const [backdrop, setBackdrop] = useState<string>('');
const [currentPage, setCurrentPage] = useState<number>(1);
const [query, setQuery] = useState<string>('');

useEffect(() => {
const fetchMovies = async () => {
Expand All @@ -30,7 +32,6 @@ const HomeModule: React.FC = () => {
fetchMovies();
}, [currentPage]);

const maxPage = 500;
const handleClickPrev = () => {
setCurrentPage((prevPage) => {
if (prevPage === 1) {
Expand All @@ -41,17 +42,21 @@ const HomeModule: React.FC = () => {
};
const handleClickNext = () => {
setCurrentPage((prevPage) => {
if (prevPage === maxPage) {
return maxPage;
if (prevPage === 500) {
return 500;
}
return prevPage + 1;
});
};
const handleQueryChange = (e: ChangeEvent<HTMLInputElement>) => {
setQuery(e.target.value);
};

return (
<div className="flex min-h-screen w-full flex-col pb-20">
<Header />
<Backdrop movie={movies[2]} backdrop={backdrop} />
<SearchBar query={query} handleQueryChange={handleQueryChange} />
<ListMovies movies={movies} />
<MyPagination
currentPage={currentPage}
Expand Down

0 comments on commit 7769a4e

Please sign in to comment.