Skip to content

Commit 55f7d42

Browse files
committed
Add new fields from widget
1 parent 2c9069e commit 55f7d42

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ function ExampleProductPage() {
1717
// Get the product variants and colors
1818
const colors = [{ id: 'red', name: 'Magnificent Red'},{ id: 'blue', name: 'Dashing Blue'}];
1919
const variants = [
20-
{variantId: 'variant-1', sizeLabel: 'S', inStock: true, sku: 'sku-1', colorId: 'red' },
21-
{variantId: 'variant-2', sizeLabel: 'S', inStock: true, sku: 'sku-2', colorId: 'blue' },
22-
{variantId: 'variant-3', sizeLabel: 'M', inStock: true, sku: 'sku-3', colorId: 'red' },
23-
{variantId: 'variant-4', sizeLabel: 'M', inStock: false, sku: 'sku-4', colorId: 'blue' },
24-
{variantId: 'variant-5', sizeLabel: 'L', inStock: false, sku: 'sku-5', colorId: 'red' },
25-
{variantId: 'variant-6', sizeLabel: 'L', inStock: false, sku: 'sku-6', colorId: 'blue' }
20+
{variantId: 'variant-1', sizeLabel: 'S', inStock: true, sku: 'sku-1', colorId: 'red', price: 11.99, imageUrl: 'https://placekitten.com/300/500' },
21+
{variantId: 'variant-2', sizeLabel: 'S', inStock: true, sku: 'sku-2', colorId: 'blue', price: 11.99, imageUrl: 'https://placekitten.com/300/500' },
22+
{variantId: 'variant-3', sizeLabel: 'M', inStock: true, sku: 'sku-3', colorId: 'red', price: 11.99, imageUrl: 'https://placekitten.com/300/500' },
23+
{variantId: 'variant-4', sizeLabel: 'M', inStock: false, sku: 'sku-4', colorId: 'blue', price: 11.99, imageUrl: 'https://placekitten.com/300/500' },
24+
{variantId: 'variant-5', sizeLabel: 'L', inStock: false, sku: 'sku-5', colorId: 'red', price: 11.99, imageUrl: 'https://placekitten.com/300/500' },
25+
{variantId: 'variant-6', sizeLabel: 'L', inStock: false, sku: 'sku-6', colorId: 'blue', price: 11.99, imageUrl: 'https://placekitten.com/300/500' }
2626
];
2727

2828
// Render the widget
@@ -38,6 +38,7 @@ function ExampleProductPage() {
3838
colors={colors}
3939
variants={variants}
4040
onAddToCart={addToCart}
41+
onResult={handleResult}
4142
/>
4243
</>
4344
);

examples/src/ProductPage.jsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ export function ProductPage() {
66
window.alert(`Add ${id} to cart`);
77
};
88

9+
const onResult = ({ label }, resultType) => {
10+
console.log(`Got Result ${label} from resultType ${resultType}`);
11+
}
12+
913
const colors = [{ id: 'red', name: 'Magnificent Red'},{ id: 'blue', name: 'Dashing Blue'}];
1014
const variants = [
11-
{variantId: 'variant-1', sizeLabel: 'S', inStock: true, sku: 'sku-1', colorId: 'red' },
12-
{variantId: 'variant-2', sizeLabel: 'S', inStock: true, sku: 'sku-2', colorId: 'blue' },
13-
{variantId: 'variant-3', sizeLabel: 'M', inStock: true, sku: 'sku-3', colorId: 'red' },
14-
{variantId: 'variant-4', sizeLabel: 'M', inStock: false, sku: 'sku-4', colorId: 'blue' },
15-
{variantId: 'variant-5', sizeLabel: 'L', inStock: false, sku: 'sku-5', colorId: 'red' },
16-
{variantId: 'variant-6', sizeLabel: 'L', inStock: false, sku: 'sku-6', colorId: 'blue' }
15+
{variantId: 'variant-1', sizeLabel: 'S', inStock: true, sku: 'sku-1', colorId: 'red', price: 11.99, imageUrl: 'https://placekitten.com/300/500' },
16+
{variantId: 'variant-2', sizeLabel: 'S', inStock: true, sku: 'sku-2', colorId: 'blue', price: 11.99, imageUrl: 'https://placekitten.com/300/500' },
17+
{variantId: 'variant-3', sizeLabel: 'M', inStock: true, sku: 'sku-3', colorId: 'red', price: 11.99, imageUrl: 'https://placekitten.com/300/500' },
18+
{variantId: 'variant-4', sizeLabel: 'M', inStock: false, sku: 'sku-4', colorId: 'blue', price: 11.99, imageUrl: 'https://placekitten.com/300/500' },
19+
{variantId: 'variant-5', sizeLabel: 'L', inStock: false, sku: 'sku-5', colorId: 'red', price: 11.99, imageUrl: 'https://placekitten.com/300/500' },
20+
{variantId: 'variant-6', sizeLabel: 'L', inStock: false, sku: 'sku-6', colorId: 'blue', price: 11.99, imageUrl: 'https://placekitten.com/300/500' }
1721
];
1822

1923
return <>
@@ -70,6 +74,7 @@ export function ProductPage() {
7074
colors={colors}
7175
variants={variants}
7276
onAddToCart={addToCart}
77+
onResult={onResult}
7378
/>
7479
</div>
7580
</div>

src/faslet-widget.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export interface Variant {
1111
inStock: boolean;
1212
sku: string;
1313
colorId: string;
14+
price?: number;
15+
imageUrl?: string;
1416
}
1517

1618
export interface FasletWidgetProps {
@@ -25,6 +27,10 @@ export interface FasletWidgetProps {
2527
colors?: Color[];
2628
shopPageUrl?: string;
2729
onAddToCart?: (id: string) => Promise<unknown>;
30+
onResult?: (
31+
{ label: string },
32+
resultType: 'auto' | 'result-screen'
33+
) => unknown;
2834
}
2935

3036
export function FasletWidget({
@@ -38,7 +44,8 @@ export function FasletWidget({
3844
variants,
3945
colors,
4046
onAddToCart,
41-
shopPageUrl
47+
shopPageUrl,
48+
onResult
4249
}: FasletWidgetProps) {
4350
// Add script tag to head
4451
useEffect(() => {
@@ -70,11 +77,14 @@ export function FasletWidget({
7077
sku: variant.sku,
7178
id: variant.variantId,
7279
available: variant.inStock,
73-
color: variant.colorId
80+
color: variant.colorId,
81+
price: variant.price,
82+
imageUrl: variant.imageUrl
7483
})),
7584
colors,
7685
shopUrl: shopPageUrl,
77-
addToCart: onAddToCart
86+
addToCart: onAddToCart,
87+
onResult
7888
};
7989

8090
return (

src/global.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@ interface Window {
66
addToCart?: (variantId: string) => Promise<unknown>;
77
onButtonShow?: () => unknown;
88
onButtonHidden?: () => unknown;
9+
openWidget?: () => unknown;
10+
onResult?: (
11+
{ label: string },
12+
resultType: 'auto' | 'result-screen'
13+
) => unknown;
14+
onColorSelected?: (colorId: string) => unknown;
15+
selectColor?: (colorId: string) => unknown;
916
variants: {
1017
size: string;
1118
color?: string;
1219
id: string;
1320
available: boolean;
1421
sku: string;
22+
price?: number;
23+
imageUrl?: string;
1524
}[];
1625
colors?: {
1726
id: string;

0 commit comments

Comments
 (0)