-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pcomparator): add reverse search price (#77)
* feat(pcomparator): add reverse search price * improve pending state
- Loading branch information
1 parent
37577a4
commit b7ad0ab
Showing
15 changed files
with
318 additions
and
70 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
24 changes: 24 additions & 0 deletions
24
pcomparator/src/applications/Searchbar/Api/searchByBarcode.ts
This file contains 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,24 @@ | ||
"use server"; | ||
import { z } from "zod"; | ||
|
||
const ParamsSchema = z.object({ | ||
barcode: z.string() | ||
}); | ||
|
||
export const searchByBarcode = async (params: z.infer<typeof ParamsSchema>): Promise<{ name: string }> => { | ||
try { | ||
const payload = ParamsSchema.parse(params); | ||
|
||
const { | ||
product: { product_name } | ||
} = await ( | ||
await fetch(`https://world.openfoodfacts.net/api/v2/product/${payload.barcode}`, { method: "get" }) | ||
).json(); | ||
|
||
return { name: product_name }; | ||
// return { success: true, prices: res, search: payload.data.search }; | ||
} catch (error) { | ||
console.error(error); | ||
throw error; | ||
} | ||
}; |
47 changes: 47 additions & 0 deletions
47
pcomparator/src/applications/Searchbar/Ui/SearchBarcode/BarcodeScannerModal.tsx
This file contains 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,47 @@ | ||
import { Modal, ModalContent } from "@nextui-org/react"; | ||
import { BarcodeScanner } from "react-barcode-scanner"; | ||
import type { Barcode } from "~/applications/Prices/Domain/ValueObjects/Barcode"; | ||
|
||
interface BarcodeScannerModalProps { | ||
isOpen: boolean; | ||
onOpenChange: (isOpen: boolean) => void; | ||
onBarcodeDetected: (barcode: Barcode) => void; | ||
} | ||
|
||
export const BarcodeScannerModal = ({ | ||
isOpen, | ||
onOpenChange, | ||
onBarcodeDetected | ||
}: BarcodeScannerModalProps) => { | ||
return ( | ||
<Modal | ||
isOpen={isOpen} | ||
onOpenChange={onOpenChange} | ||
size="4xl" | ||
data-testid="modal-barcode-scanner" | ||
className="h-[80dvh]" | ||
> | ||
<ModalContent> | ||
<BarcodeScanner | ||
onCapture={(barcode) => onBarcodeDetected({ barcode: barcode.rawValue, format: barcode.format })} | ||
options={{ | ||
formats: [ | ||
"codabar", | ||
"upc_a", | ||
"code_128", | ||
"code_39", | ||
"code_93", | ||
"data_matrix", | ||
"ean_13", | ||
"ean_8", | ||
"itf", | ||
"pdf417", | ||
"qr_code", | ||
"upc_e" | ||
] | ||
}} | ||
/> | ||
</ModalContent> | ||
</Modal> | ||
); | ||
}; |
56 changes: 56 additions & 0 deletions
56
pcomparator/src/applications/Searchbar/Ui/SearchBarcode/SearchBarcode.tsx
This file contains 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,56 @@ | ||
"use client"; | ||
|
||
import { Button, useDisclosure } from "@nextui-org/react"; | ||
import { ScanBarcode } from "lucide-react"; | ||
import { useState, useTransition } from "react"; | ||
import { search } from "~/applications/Searchbar/Api/search"; | ||
import { searchByBarcode } from "~/applications/Searchbar/Api/searchByBarcode"; | ||
import { BarcodeScannerModal } from "~/applications/Searchbar/Ui/SearchBarcode/BarcodeScannerModal"; | ||
import { SearchResultModal } from "~/applications/Searchbar/Ui/SearchResult/SearchResultModal"; | ||
|
||
export const SearchBarcode = () => { | ||
const [pending, startTransition] = useTransition(); | ||
const { onOpen, onClose, isOpen, onOpenChange } = useDisclosure(); | ||
const [searchResult, setSearchResult] = useState<Awaited<ReturnType<typeof search>> | null>(null); | ||
|
||
return ( | ||
<> | ||
{!searchResult ? ( | ||
<BarcodeScannerModal | ||
isOpen={isOpen} | ||
onOpenChange={onOpenChange} | ||
onBarcodeDetected={(barcode) => { | ||
!pending && | ||
startTransition(async () => { | ||
const { name } = await searchByBarcode({ barcode: barcode.barcode }); | ||
const formData = new FormData(); | ||
|
||
formData.append("search", name); | ||
setSearchResult(await search(null, formData)); | ||
}); | ||
}} | ||
/> | ||
) : null} | ||
<Button | ||
startContent={<ScanBarcode />} | ||
onPress={onOpen} | ||
radius="full" | ||
variant="faded" | ||
className="p-7 w-18 h-18 -mt-8 border-none shadow-medium" | ||
isIconOnly | ||
/> | ||
{searchResult && ( | ||
<SearchResultModal | ||
isOpen={isOpen} | ||
onClose={() => { | ||
onClose(); | ||
setSearchResult(null); | ||
}} | ||
onOpenChange={onOpenChange} | ||
prices={searchResult?.prices as any} | ||
search={searchResult?.search} | ||
/> | ||
)} | ||
</> | ||
); | ||
}; |
59 changes: 59 additions & 0 deletions
59
pcomparator/src/applications/Searchbar/Ui/SearchResult/SearchResultModal.tsx
This file contains 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,59 @@ | ||
import { Trans } from "@lingui/macro"; | ||
import { | ||
Button, | ||
Card, | ||
CardBody, | ||
Image, | ||
Modal, | ||
ModalBody, | ||
ModalContent, | ||
ModalFooter, | ||
ModalHeader | ||
} from "@nextui-org/react"; | ||
import type { Price } from "~/applications/Prices/Domain/Entities/Price"; | ||
import type { Product } from "~/applications/Prices/Domain/Entities/Product"; | ||
import type { Store } from "~/applications/Prices/Domain/Entities/Store"; | ||
import { getCurrencySymbol } from "~/applications/Prices/Domain/ValueObjects/Currency"; | ||
|
||
interface SearchResultProps { | ||
isOpen: boolean; | ||
onOpenChange: () => void; | ||
onClose: () => void; | ||
search: string; | ||
prices: (Price & { product: Product; store: Store })[]; | ||
} | ||
|
||
export const SearchResultModal = ({ isOpen, onClose, onOpenChange, prices, search }: SearchResultProps) => ( | ||
<Modal isOpen={isOpen} onClose={onClose} onOpenChange={onOpenChange}> | ||
<ModalContent> | ||
<ModalHeader> | ||
<p> | ||
<Trans>Prices for {search}</Trans> | ||
</p> | ||
</ModalHeader> | ||
<ModalBody> | ||
{prices.map((price, i) => ( | ||
<Card key={price.product.name + i}> | ||
<CardBody className="flex !flex-row gap-4"> | ||
<Image src={price.priceProofImage!} width={100} height={100} className="object-cover" /> | ||
<div className="flex flex-col justify-between"> | ||
<div> | ||
<p className="font-bold text-lg">{price.product.name}</p> | ||
<p className="text-small">{price.store.name} – 6km</p> | ||
</div> | ||
<p> | ||
{getCurrencySymbol(price.currency)} {price.amount} | ||
</p> | ||
</div> | ||
</CardBody> | ||
</Card> | ||
))} | ||
</ModalBody> | ||
<ModalFooter> | ||
<Button size="sm" onPress={onClose}> | ||
<Trans>Close</Trans> | ||
</Button> | ||
</ModalFooter> | ||
</ModalContent> | ||
</Modal> | ||
); |
This file contains 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
Oops, something went wrong.