From 2a087905a7ab881853050de32af7ab291e1b4d25 Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Mon, 12 Oct 2020 11:10:20 -0300 Subject: [PATCH] Search functionality --- components/core/Searchbar/Searchbar.tsx | 11 ++++++++--- pages/search.tsx | 4 +++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/components/core/Searchbar/Searchbar.tsx b/components/core/Searchbar/Searchbar.tsx index 5f9d52ed6..c29bbe581 100644 --- a/components/core/Searchbar/Searchbar.tsx +++ b/components/core/Searchbar/Searchbar.tsx @@ -19,10 +19,15 @@ const Searchbar: FC = ({ className }) => { { + onKeyUp={(e) => { e.preventDefault() - router.push('/search') - console.log('changing') + + if (e.key === 'Enter') { + router.push({ + pathname: `/search`, + query: { q: e.currentTarget.value }, + }) + } }} />
diff --git a/pages/search.tsx b/pages/search.tsx index 72135d88a..bc560d6f8 100644 --- a/pages/search.tsx +++ b/pages/search.tsx @@ -4,6 +4,7 @@ import { Layout } from '@components/core' import { Grid, Marquee, Hero } from '@components/ui' import { ProductCard } from '@components/product' import getSiteInfo from '@lib/bigcommerce/api/operations/get-site-info' +import { useRouter } from 'next/router' export async function getStaticProps({ preview }: GetStaticPropsContext) { const { products } = await getAllProducts() @@ -19,6 +20,7 @@ export default function Home({ categories, brands, }: InferGetStaticPropsType) { + const router = useRouter() return (
@@ -45,7 +47,7 @@ export default function Home({
- Showing 8 results for "Jacket" + Showing 8 results for "{router.query.q}"