diff --git a/src/components/products/Products.js b/src/components/products/Products.js index 7e65d2b..5f95439 100644 --- a/src/components/products/Products.js +++ b/src/components/products/Products.js @@ -1,12 +1,5 @@ import Link from 'next/link'; - -export const getStaticProps = async () => { - const res = await fetch("https://fakestoreapi.com/products"); - const data = await res.json(); - return { props: { products: data } }; -}; - export default function Products({ product }) { return (
diff --git a/src/pages/Search.js b/src/pages/Search.js index 5bfea57..24f523c 100644 --- a/src/pages/Search.js +++ b/src/pages/Search.js @@ -1,28 +1,22 @@ import React, { useState } from "react"; +import SearchPage from "./SearchEngPage/SearchPage"; -function Search({ products }) { +export default function Search({ products }) { const [searchInput, setSearchInput] = useState(""); + console.log(products) function handleInput(event) { setSearchInput(event.target.value); } function handleSearch() { - for (const element of products) { - if (element.title == searchInput) { -
- {products.map((product) => { - return ( -
- {product.title} -

{product.title}

- View Product -
- ); - })} -
; + products.filter((product)=>{ + if (searchInput == product.title) { + return ; + } else { + return

Not Found

; } - } + }) } return ( @@ -46,7 +40,7 @@ function Search({ products }) {
); } - -export default Search; - -export const getStaticProps = async () => { - const res = await fetch("https://fakestoreapi.com/products"); - const data = await res.json(); - return { - props: { - products: data, - }, - }; -}; diff --git a/src/pages/SearchEngPage/SearchPage.js b/src/pages/SearchEngPage/SearchPage.js new file mode 100644 index 0000000..467f60b --- /dev/null +++ b/src/pages/SearchEngPage/SearchPage.js @@ -0,0 +1,27 @@ +import React from 'react' + +function SearchPage({ product }) { + return ( +
+ + {product.title} + +
+ +

{product.title}

+ +

{product.category}

+

${product.price}

+
+ +
+ ) +} + +export default SearchPage \ No newline at end of file diff --git a/src/pages/index.js b/src/pages/index.js index 5cb4279..c5819b4 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -1,5 +1,6 @@ import Layout from '@/components/Layout' import Products from '@/components/products/Products'; +import Search from './Search'; export const getStaticProps = async () => { const res = await fetch("https://fakestoreapi.com/products"); @@ -14,6 +15,7 @@ export default function Home({ products }) { {products.map(product => ( ))} + )