Skip to content

search engin #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions src/components/products/Products.js
Original file line number Diff line number Diff line change
@@ -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 (
<div className='card flex flex-col justify-between'>
Expand Down
38 changes: 10 additions & 28 deletions src/pages/Search.js
Original file line number Diff line number Diff line change
@@ -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) {
<div>
{products.map((product) => {
return (
<div key={product.id}>
<img src={product.image} alt={product.title} />
<h1>{product.title}</h1>
<Link href={`/item/${product.id}`}>View Product</Link>
</div>
);
})}
</div>;
products.filter((product)=>{
if (searchInput == product.title) {
return <SearchPage product={product} key={product.id} />;
} else {
return <h1>Not Found</h1>;
}
}
})
}

return (
Expand All @@ -46,7 +40,7 @@ function Search({ products }) {
</div>
<input
type="text"
placeholder="Search....."
placeholder="Search...."
className="block w-full p-4 pl-10 text-sm text-gray-900 border border-gray-300 rounded-lg bg-slate-700 focus:border-red-400"
value={searchInput}
onChange={handleInput}
Expand All @@ -60,15 +54,3 @@ function Search({ products }) {
</div>
);
}

export default Search;

export const getStaticProps = async () => {
const res = await fetch("https://fakestoreapi.com/products");
const data = await res.json();
return {
props: {
products: data,
},
};
};
27 changes: 27 additions & 0 deletions src/pages/SearchEngPage/SearchPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react'

function SearchPage({ product }) {
return (
<div className='card flex flex-col justify-between'>
<Link href={`/item/${product.id}`}>
<img
src={product.image}
alt={product.title}
width={720}
height={720}
className="rounded shadow-lg h-[50vw] lg:h-[15vw] md:h-[15vw] sm:h-[50vw] w-[100%]"
/>
</Link>
<div className='py-2'>
<Link href={`/item/${product.id}`}>
<h2 className='text-lg'>{product.title}</h2>
</Link>
<p>{product.category}</p>
<p> ${product.price}</p>
</div>
<button className='primary-button' type='button'>Add To Cart</button>
</div>
)
}

export default SearchPage
2 changes: 2 additions & 0 deletions src/pages/index.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -14,6 +15,7 @@ export default function Home({ products }) {
{products.map(product => (
<Products product={product} key={product.id} ></Products>
))}
<Search products={products}/>
</div>
</Layout>
)
Expand Down