Skip to content

Commit 2aefc64

Browse files
committed
feat: extract and style product page barcode info
1 parent 1425102 commit 2aefc64

File tree

2 files changed

+99
-10
lines changed

2 files changed

+99
-10
lines changed

src/lib/ui/BarcodeInfo.svelte

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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>

src/routes/products/[barcode]/+page.svelte

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
1414
import Gs1Country from './GS1Country.svelte';
1515
import ProductHeader from './ProductHeader.svelte';
16+
import BarcodeInfo from '$lib/ui/BarcodeInfo.svelte';
1617
1718
import type { PageData } from './$types';
1819
import Prices from './Prices.svelte';
@@ -62,12 +63,6 @@
6263
description: 'Show product barcode',
6364
action: () => (showBarcode = !showBarcode)
6465
});
65-
66-
import JsBarcode from 'jsbarcode';
67-
68-
$effect(() => {
69-
JsBarcode('#barcode', product.code, { format: 'ean13' });
70-
});
7166
</script>
7267

7368
<!-- FIXME: Remove this cast once product.image_front_small_url and product.image_front_url are not nullable in the API -->
@@ -80,10 +75,9 @@
8075
<div class="flex flex-col gap-4">
8176
<ProductHeader {product} taxonomies={data.taxo} />
8277

83-
<div class={['flex flex-col items-center gap-4', showBarcode || 'hidden'].join(' ')}>
84-
<svg id="barcode"></svg>
85-
<p class="text-sm text-gray-500">Barcode: {product.code}</p>
86-
</div>
78+
{#if showBarcode && product.code != null}
79+
<BarcodeInfo code={product.code} />
80+
{/if}
8781

8882
<robotoff-contribution-message product-code={product.code} is-logged-in={$userInfo != null}
8983
></robotoff-contribution-message>

0 commit comments

Comments
 (0)