|
| 1 | +<script lang="ts"> |
| 2 | + import JsBarcode from 'jsbarcode'; |
| 3 | + import Card from './Card.svelte'; |
| 4 | +
|
| 5 | + let { code }: { code: string } = $props(); |
| 6 | +
|
| 7 | + let openPricesStatus: number | null = $state(null); |
| 8 | + let proOffStatus: number | null = $state(null); |
| 9 | +
|
| 10 | + $effect(() => { |
| 11 | + JsBarcode('#barcode', code, { format: 'ean13' }); |
| 12 | + fetch(`https://prices.openfoodfacts.org/product/${code}`) |
| 13 | + .then((r) => (openPricesStatus = r.status)) |
| 14 | + .catch(() => (openPricesStatus = 0)); |
| 15 | + fetch(`https://pro.openfoodfacts.dev/products/${code}`) |
| 16 | + .then((r) => (proOffStatus = r.status)) |
| 17 | + .catch(() => (proOffStatus = 0)); |
| 18 | + }); |
| 19 | +</script> |
| 20 | + |
| 21 | +<Card> |
| 22 | + <h1 class="card-title my-4 grow text-2xl font-bold md:text-4xl">Barcode Information</h1> |
| 23 | + <div class="card-body flex flex-row-reverse"> |
| 24 | + <svg id="barcode" class="mb-2 block"></svg> |
| 25 | + |
| 26 | + <div class="grow"> |
| 27 | + <label class="input w-full"> |
| 28 | + <span class="label">Barcode</span> |
| 29 | + <input type="text" readonly value={code} /> |
| 30 | + </label> |
| 31 | + |
| 32 | + <ul class="list mt-4 list-inside list-disc"> |
| 33 | + <li> |
| 34 | + <a |
| 35 | + class="text-xs text-blue-500 hover:underline" |
| 36 | + href={`https://www.google.com/search?q=${code}`} |
| 37 | + target="_blank" |
| 38 | + rel="noopener" |
| 39 | + title="Search on Google" |
| 40 | + > |
| 41 | + Google |
| 42 | + </a> |
| 43 | + </li> |
| 44 | + <li> |
| 45 | + <a |
| 46 | + class="text-xs text-blue-500 hover:underline" |
| 47 | + href={`https://duckduckgo.com/?q=${code}`} |
| 48 | + target="_blank" |
| 49 | + rel="noopener" |
| 50 | + title="Search on DuckDuckGo" |
| 51 | + > |
| 52 | + DuckDuckGo |
| 53 | + </a> |
| 54 | + </li> |
| 55 | + <li> |
| 56 | + <a |
| 57 | + class="text-xs text-green-600 hover:underline" |
| 58 | + href={`https://prices.openfoodfacts.org/product/${code}`} |
| 59 | + target="_blank" |
| 60 | + rel="noopener" |
| 61 | + title="Open Prices" |
| 62 | + > |
| 63 | + Open Prices {openPricesStatus === null |
| 64 | + ? '…' |
| 65 | + : openPricesStatus === 200 |
| 66 | + ? '' |
| 67 | + : ' (Not found)'} |
| 68 | + </a> |
| 69 | + </li> |
| 70 | + <li> |
| 71 | + <a |
| 72 | + class="text-xs text-green-600 hover:underline" |
| 73 | + href={`https://pro.openfoodfacts.dev/products/${code}`} |
| 74 | + target="_blank" |
| 75 | + rel="noopener" |
| 76 | + title="Pro OFF" |
| 77 | + > |
| 78 | + Pro OFF{proOffStatus === null ? '…' : proOffStatus === 200 ? '' : ' (Not found)'} |
| 79 | + </a> |
| 80 | + </li> |
| 81 | + {#if code && code.length >= 3} |
| 82 | + <li> |
| 83 | + <a |
| 84 | + class="text-xs text-purple-500 hover:underline" |
| 85 | + href={`/search?barcode_prefix=${code.slice(0, 3)}`} |
| 86 | + title="Find similar products" |
| 87 | + > |
| 88 | + Similar prefix |
| 89 | + </a> |
| 90 | + </li> |
| 91 | + {/if} |
| 92 | + </ul> |
| 93 | + </div> |
| 94 | + </div> |
| 95 | +</Card> |
0 commit comments