-
Notifications
You must be signed in to change notification settings - Fork 104
Add SearchBar #19
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
AdysonVieira
wants to merge
3
commits into
felipemotarocha:main
Choose a base branch
from
AdysonVieira:search-bar
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add SearchBar #19
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| 'use client' | ||
| import useSearch from '@/hooks/use-search' | ||
| import { SearchIcon } from 'lucide-react' | ||
| import React, { ReactNode } from 'react' | ||
|
|
||
| const SearchBar = ({children}: {children: ReactNode}) => { | ||
| const { search, onChange } = useSearch() | ||
|
|
||
| return ( | ||
| <div className='p-5 w-full'> | ||
| <div className='bg-white rounded-md flex justify-between items-center px-5'> | ||
| <input | ||
| className='bg-transparent focus:outline-none py-2 w-[95%] text-secondary text-sm' | ||
| type="text" | ||
| id='search-bar' | ||
| name='search-bar' | ||
| value={search} | ||
| onChange={onChange} | ||
| spellCheck={false} | ||
| /> | ||
| <SearchIcon color='#acacac' /> | ||
| </div> | ||
| {children} | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| export default SearchBar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| 'use client' | ||
| import { ProductWithTotalPrice } from '@/helpers/product' | ||
| import React from 'react' | ||
| import Image from 'next/image' | ||
| import { convertCurrencyToReal } from '@/helpers/convert-currency' | ||
| import Link from 'next/link' | ||
| import useSearch from '@/hooks/use-search' | ||
|
|
||
| const SearchCardResult = ({ product }: {product: ProductWithTotalPrice | undefined}) => { | ||
| const { setSearch } = useSearch() | ||
|
|
||
| if (!product) { | ||
| return null | ||
| } | ||
|
|
||
| return ( | ||
| <Link | ||
| href={`/product/${product.slug}`} | ||
| onClick={() => setSearch('')} | ||
| key={product.id} | ||
| className='flex gap-4'> | ||
| <div className='bg-accent w-14 h-14 rounded-md flex items-center justify-center'> | ||
| <Image | ||
| src={product.imageUrls[0]} | ||
| sizes='100vw' | ||
| alt={product.description} | ||
| width={0} | ||
| height={0} | ||
| className='w-10 h-10 aspect-square' | ||
| /> | ||
| </div> | ||
| <div> | ||
| <p className='text-black opacity-70'>{product.name}</p> | ||
| <span className='text-primary font-bold text-md pr-2'>{convertCurrencyToReal(product.totalPrice)}</span> | ||
| <span className='text-black opacity-60 line-through text-xs'>{convertCurrencyToReal(Number(product.basePrice))}</span> | ||
| </div> | ||
| </Link> | ||
| ) | ||
| } | ||
|
|
||
| export default SearchCardResult |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| 'use client' | ||
| import React from 'react' | ||
| import { computeProductTotalPrice } from '@/helpers/product' | ||
| import SearchCardResult from './search-card-result' | ||
| import { Product } from '@prisma/client' | ||
| import useSearch from '@/hooks/use-search' | ||
|
|
||
| interface SearchResultsProps { | ||
| products: Product[] | ||
| } | ||
|
|
||
| const SearchResults = ({ products }: SearchResultsProps ) => { | ||
| const { deferredSearch } = useSearch(); | ||
| const [filteredProducts, setFilteredProducts] = React.useState<Product[]>([]) | ||
|
|
||
|
|
||
| React.useEffect(() => { | ||
| const filtered = products.filter((product) => product.name.toLowerCase().includes(deferredSearch.toLowerCase()) ) | ||
| setFilteredProducts(filtered) | ||
| }, [deferredSearch, products]) | ||
|
|
||
| if (!deferredSearch) return null | ||
|
|
||
| return ( | ||
| <ul className='p-5 bg-slate-50 flex flex-col gap-5 absolute w-[calc(100%-2.5rem)] z-10 max-h-80 overflow-scroll top-16 left-5 box-border rounded-md [&::-webkit-scrollbar]:hidden'> | ||
| { | ||
| deferredSearch && filteredProducts.length !== 0 ? | ||
| filteredProducts?.map((product) => ( | ||
| <li key={product.id}> | ||
| <SearchCardResult product={computeProductTotalPrice(product)} /> | ||
| </li> | ||
| )) | ||
| : <li> | ||
| <p className='text-accent text-sm font-semibold'> | ||
| {`Não encontramos nenhuma sugestão :(`} | ||
| </p> | ||
| </li> | ||
| } | ||
| </ul> | ||
| ) | ||
| } | ||
|
|
||
| export default SearchResults |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import React from 'react' | ||
| import SearchBar from './search-bar' | ||
| import SearchResults from './search-results' | ||
| import { prismaClient } from "@/lib/prisma"; | ||
|
|
||
| const Search = async () => { | ||
| const products = await prismaClient.product.findMany({}) | ||
| console.log(products) | ||
| return ( | ||
| <div className='flex flex-col relative'> | ||
| <SearchBar> | ||
| <SearchResults products={products}/> | ||
| </SearchBar> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| export default Search | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export const convertCurrencyToReal = (value: number) => { | ||
| return value.toLocaleString( | ||
| 'pt-BR', { | ||
| style: 'currency', | ||
| currency: 'BRL' | ||
| } | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import React from "react" | ||
| import { ISearch } from "@/providers/search" | ||
|
|
||
| const useSearch = () => { | ||
| return React.useContext(ISearch) | ||
| } | ||
|
|
||
| export default useSearch |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| 'use client' | ||
| import React, { ReactNode, SetStateAction } from 'react' | ||
|
|
||
| interface ISearchProps { | ||
| search: string | undefined | ||
| onChange: (e: React.ChangeEvent<HTMLInputElement>) => void | ||
| deferredSearch: string | ||
| setSearch: React.Dispatch<SetStateAction<string>> | ||
| } | ||
|
|
||
| export const ISearch = React.createContext({} as ISearchProps) | ||
|
|
||
| const SearchProvider = ({ children }: { children: ReactNode }) => { | ||
| const [search, setSearch] = React.useState('') | ||
| const deferredSearch = React.useDeferredValue(search) | ||
|
|
||
| const onChange = React.useCallback((e: React.ChangeEvent<HTMLInputElement>) => { | ||
| setSearch(e.target.value) | ||
| }, []) | ||
|
|
||
| return ( | ||
| <ISearch.Provider value={{search, onChange, deferredSearch, setSearch}}> | ||
| {children} | ||
| </ISearch.Provider> | ||
| ) | ||
| } | ||
|
|
||
| export default SearchProvider | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remover o console.log