@constructor-io/constructorio-ui-plp 1.6.5 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,8 +6,9 @@ const useCioClient_1 = tslib_1.__importDefault(require("./useCioClient"));
6
6
  const defaultGetters = tslib_1.__importStar(require("../utils/itemFieldGetters"));
7
7
  const defaultFormatters = tslib_1.__importStar(require("../utils/formatters"));
8
8
  const defaultUrlHelpers = tslib_1.__importStar(require("../utils/urlHelpers"));
9
+ const shopifyDefaults_1 = require("../utils/shopifyDefaults");
9
10
  function useCioPlpProvider(props) {
10
- const { apiKey, formatters, callbacks, itemFieldGetters, urlHelpers, staticRequestConfigs = {}, renderOverrides = {}, cioClient: customCioClient, cioClientOptions: customCioClientOptions = {}, } = props;
11
+ const { apiKey, formatters, callbacks, itemFieldGetters, urlHelpers, staticRequestConfigs = {}, renderOverrides = {}, cioClient: customCioClient, cioClientOptions: customCioClientOptions = {}, useShopifyDefaults = false, } = props;
11
12
  const [cioClientOptions, setCioClientOptions] = (0, react_1.useState)(customCioClientOptions);
12
13
  const cioClient = (0, useCioClient_1.default)({ apiKey, cioClient: customCioClient, options: cioClientOptions });
13
14
  const contextValue = (0, react_1.useMemo)(() => ({
@@ -17,8 +18,8 @@ function useCioPlpProvider(props) {
17
18
  staticRequestConfigs,
18
19
  itemFieldGetters: Object.assign(Object.assign({}, defaultGetters), itemFieldGetters),
19
20
  formatters: Object.assign(Object.assign({}, defaultFormatters), formatters),
20
- callbacks: Object.assign({}, callbacks),
21
- urlHelpers: Object.assign(Object.assign({}, defaultUrlHelpers), urlHelpers),
21
+ callbacks: Object.assign(Object.assign({}, (useShopifyDefaults && shopifyDefaults_1.shopifyDefaults.callbacks)), callbacks),
22
+ urlHelpers: Object.assign(Object.assign(Object.assign({}, defaultUrlHelpers), (useShopifyDefaults && shopifyDefaults_1.shopifyDefaults.urlHelpers)), urlHelpers),
22
23
  renderOverrides: Object.assign({}, renderOverrides),
23
24
  }), [
24
25
  cioClient,
@@ -29,6 +30,7 @@ function useCioPlpProvider(props) {
29
30
  urlHelpers,
30
31
  renderOverrides,
31
32
  staticRequestConfigs,
33
+ useShopifyDefaults,
32
34
  ]);
33
35
  return contextValue;
34
36
  }
@@ -6,7 +6,7 @@ const useCioPlpContext_1 = require("./useCioPlpContext");
6
6
  const utils_1 = require("../utils");
7
7
  const itemFieldGetters_1 = require("../utils/itemFieldGetters");
8
8
  const useProductInfo = ({ item }) => {
9
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
9
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
10
10
  const state = (0, useCioPlpContext_1.useCioPlpContext)();
11
11
  const productSwatch = (0, useProductSwatch_1.default)({ item });
12
12
  if (!item.data || !item.itemId || !item.itemName) {
@@ -15,18 +15,22 @@ const useProductInfo = ({ item }) => {
15
15
  const getPrice = (0, utils_1.tryCatchify)(((_a = state === null || state === void 0 ? void 0 : state.itemFieldGetters) === null || _a === void 0 ? void 0 : _a.getPrice) || itemFieldGetters_1.getPrice);
16
16
  const getSalePrice = (0, utils_1.tryCatchify)(((_b = state === null || state === void 0 ? void 0 : state.itemFieldGetters) === null || _b === void 0 ? void 0 : _b.getSalePrice) || itemFieldGetters_1.getSalePrice);
17
17
  const getRolloverImage = (0, utils_1.tryCatchify)(((_c = state === null || state === void 0 ? void 0 : state.itemFieldGetters) === null || _c === void 0 ? void 0 : _c.getRolloverImage) || itemFieldGetters_1.getRolloverImage);
18
- const itemName = ((_d = productSwatch === null || productSwatch === void 0 ? void 0 : productSwatch.selectedVariation) === null || _d === void 0 ? void 0 : _d.itemName) || item.itemName;
19
- const itemPrice = ((_e = productSwatch === null || productSwatch === void 0 ? void 0 : productSwatch.selectedVariation) === null || _e === void 0 ? void 0 : _e.price) || getPrice(item);
20
- const itemImageUrl = ((_f = productSwatch === null || productSwatch === void 0 ? void 0 : productSwatch.selectedVariation) === null || _f === void 0 ? void 0 : _f.imageUrl) || item.imageUrl;
21
- const itemUrl = ((_g = productSwatch === null || productSwatch === void 0 ? void 0 : productSwatch.selectedVariation) === null || _g === void 0 ? void 0 : _g.url) || item.url;
22
- const variationId = (_h = productSwatch === null || productSwatch === void 0 ? void 0 : productSwatch.selectedVariation) === null || _h === void 0 ? void 0 : _h.variationId;
23
- let rolloverImage = (_j = productSwatch === null || productSwatch === void 0 ? void 0 : productSwatch.selectedVariation) === null || _j === void 0 ? void 0 : _j.rolloverImage;
18
+ const getItemUrl = (0, utils_1.tryCatchify)(((_d = state === null || state === void 0 ? void 0 : state.itemFieldGetters) === null || _d === void 0 ? void 0 : _d.getItemUrl) || itemFieldGetters_1.getItemUrl);
19
+ const itemName = ((_e = productSwatch === null || productSwatch === void 0 ? void 0 : productSwatch.selectedVariation) === null || _e === void 0 ? void 0 : _e.itemName) || item.itemName;
20
+ const itemPrice = ((_f = productSwatch === null || productSwatch === void 0 ? void 0 : productSwatch.selectedVariation) === null || _f === void 0 ? void 0 : _f.price) || getPrice(item);
21
+ const itemImageUrl = ((_g = productSwatch === null || productSwatch === void 0 ? void 0 : productSwatch.selectedVariation) === null || _g === void 0 ? void 0 : _g.imageUrl) || item.imageUrl;
22
+ // Get href - merge variation URL into item if variation is selected
23
+ const itemWithVariationUrl = (productSwatch === null || productSwatch === void 0 ? void 0 : productSwatch.selectedVariation)
24
+ ? Object.assign(Object.assign({}, item), { url: productSwatch.selectedVariation.url }) : item;
25
+ const itemUrl = getItemUrl(itemWithVariationUrl) || ((_h = productSwatch === null || productSwatch === void 0 ? void 0 : productSwatch.selectedVariation) === null || _h === void 0 ? void 0 : _h.url);
26
+ const variationId = (_j = productSwatch === null || productSwatch === void 0 ? void 0 : productSwatch.selectedVariation) === null || _j === void 0 ? void 0 : _j.variationId;
27
+ let rolloverImage = (_k = productSwatch === null || productSwatch === void 0 ? void 0 : productSwatch.selectedVariation) === null || _k === void 0 ? void 0 : _k.rolloverImage;
24
28
  // Fallback to item's rollover image if all variations don't have a rollover image
25
- if (!rolloverImage && ((_k = productSwatch === null || productSwatch === void 0 ? void 0 : productSwatch.swatchList) === null || _k === void 0 ? void 0 : _k.every((swatch) => !swatch.rolloverImage))) {
29
+ if (!rolloverImage && ((_l = productSwatch === null || productSwatch === void 0 ? void 0 : productSwatch.swatchList) === null || _l === void 0 ? void 0 : _l.every((swatch) => !swatch.rolloverImage))) {
26
30
  rolloverImage = getRolloverImage(item);
27
31
  }
28
32
  const { itemId } = item;
29
- let salePrice = ((_l = productSwatch === null || productSwatch === void 0 ? void 0 : productSwatch.selectedVariation) === null || _l === void 0 ? void 0 : _l.salePrice) || getSalePrice(item);
33
+ let salePrice = ((_m = productSwatch === null || productSwatch === void 0 ? void 0 : productSwatch.selectedVariation) === null || _m === void 0 ? void 0 : _m.salePrice) || getSalePrice(item);
30
34
  let hasSalePrice = true;
31
35
  if (!(0, utils_1.isValidSalePrice)(salePrice, itemPrice)) {
32
36
  salePrice = undefined;
@@ -10,3 +10,4 @@ tslib_1.__exportStar(require("./dataAttributeHelpers"), exports);
10
10
  tslib_1.__exportStar(require("./urlHelpers"), exports);
11
11
  tslib_1.__exportStar(require("./requestConfigsHelpers"), exports);
12
12
  tslib_1.__exportStar(require("./common"), exports);
13
+ tslib_1.__exportStar(require("./shopifyDefaults"), exports);
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getIsHiddenGroupField = exports.getSwatchPreview = exports.getSwatches = exports.getRolloverImage = exports.getSalePrice = exports.getPrice = void 0;
3
+ exports.getItemUrl = exports.getIsHiddenGroupField = exports.getSwatchPreview = exports.getSwatches = exports.getRolloverImage = exports.getSalePrice = exports.getPrice = void 0;
4
4
  // eslint-disable-next-line import/prefer-default-export
5
5
  function getPrice(item) {
6
6
  return item.data.price;
@@ -45,3 +45,7 @@ function getIsHiddenGroupField(group) {
45
45
  return (_a = group === null || group === void 0 ? void 0 : group.data) === null || _a === void 0 ? void 0 : _a.cio_plp_hidden;
46
46
  }
47
47
  exports.getIsHiddenGroupField = getIsHiddenGroupField;
48
+ function getItemUrl(item) {
49
+ return item === null || item === void 0 ? void 0 : item.url;
50
+ }
51
+ exports.getItemUrl = getItemUrl;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.shopifyDefaults = void 0;
4
+ // eslint-disable-next-line import/prefer-default-export
5
+ exports.shopifyDefaults = {
6
+ selector: '#cio-plp-ui-container',
7
+ urlHelpers: {
8
+ setUrl(url) {
9
+ const modifiedUrl = url.replace('/group_id', '/collections');
10
+ if (typeof window !== 'undefined') {
11
+ window.location.href = modifiedUrl;
12
+ }
13
+ },
14
+ },
15
+ callbacks: {
16
+ onAddToCart(_event, item, selectedVariation) {
17
+ var _a;
18
+ // eslint-disable-next-line no-underscore-dangle
19
+ const shopifyId = ((_a = item.data) === null || _a === void 0 ? void 0 : _a.__shopify_id) || (selectedVariation === null || selectedVariation === void 0 ? void 0 : selectedVariation.variationId) || item.itemId;
20
+ if (typeof window !== 'undefined') {
21
+ fetch('/cart/add.js', {
22
+ method: 'POST',
23
+ headers: { 'Content-Type': 'application/json' },
24
+ body: JSON.stringify({
25
+ id: shopifyId,
26
+ quantity: 1,
27
+ }),
28
+ }).catch((error) => {
29
+ // eslint-disable-next-line no-console
30
+ console.error('Failed to add item to cart:', error);
31
+ });
32
+ }
33
+ },
34
+ onProductCardClick(_event, item) {
35
+ if (typeof window !== 'undefined') {
36
+ const url = new URL(`/products/${item.itemId}`, window.location.origin);
37
+ window.location.href = url.href;
38
+ }
39
+ },
40
+ },
41
+ };
@@ -1,3 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = '1.6.5';
3
+ exports.default = '1.7.1';
@@ -3,8 +3,9 @@ import useCioClient from './useCioClient';
3
3
  import * as defaultGetters from '../utils/itemFieldGetters';
4
4
  import * as defaultFormatters from '../utils/formatters';
5
5
  import * as defaultUrlHelpers from '../utils/urlHelpers';
6
+ import { shopifyDefaults } from '../utils/shopifyDefaults';
6
7
  export default function useCioPlpProvider(props) {
7
- const { apiKey, formatters, callbacks, itemFieldGetters, urlHelpers, staticRequestConfigs = {}, renderOverrides = {}, cioClient: customCioClient, cioClientOptions: customCioClientOptions = {}, } = props;
8
+ const { apiKey, formatters, callbacks, itemFieldGetters, urlHelpers, staticRequestConfigs = {}, renderOverrides = {}, cioClient: customCioClient, cioClientOptions: customCioClientOptions = {}, useShopifyDefaults = false, } = props;
8
9
  const [cioClientOptions, setCioClientOptions] = useState(customCioClientOptions);
9
10
  const cioClient = useCioClient({ apiKey, cioClient: customCioClient, options: cioClientOptions });
10
11
  const contextValue = useMemo(() => ({
@@ -14,8 +15,12 @@ export default function useCioPlpProvider(props) {
14
15
  staticRequestConfigs,
15
16
  itemFieldGetters: { ...defaultGetters, ...itemFieldGetters },
16
17
  formatters: { ...defaultFormatters, ...formatters },
17
- callbacks: { ...callbacks },
18
- urlHelpers: { ...defaultUrlHelpers, ...urlHelpers },
18
+ callbacks: { ...(useShopifyDefaults && shopifyDefaults.callbacks), ...callbacks },
19
+ urlHelpers: {
20
+ ...defaultUrlHelpers,
21
+ ...(useShopifyDefaults && shopifyDefaults.urlHelpers),
22
+ ...urlHelpers,
23
+ },
19
24
  renderOverrides: { ...renderOverrides },
20
25
  }), [
21
26
  cioClient,
@@ -26,6 +31,7 @@ export default function useCioPlpProvider(props) {
26
31
  urlHelpers,
27
32
  renderOverrides,
28
33
  staticRequestConfigs,
34
+ useShopifyDefaults,
29
35
  ]);
30
36
  return contextValue;
31
37
  }
@@ -1,7 +1,7 @@
1
1
  import useProductSwatch from './useProductSwatch';
2
2
  import { useCioPlpContext } from './useCioPlpContext';
3
3
  import { tryCatchify, isValidSalePrice } from '../utils';
4
- import { getPrice as defaultGetPrice, getSalePrice as defaultGetSalePrice, getRolloverImage as defaultGetRolloverImage, } from '../utils/itemFieldGetters';
4
+ import { getPrice as defaultGetPrice, getSalePrice as defaultGetSalePrice, getRolloverImage as defaultGetRolloverImage, getItemUrl as defaultGetItemUrl, } from '../utils/itemFieldGetters';
5
5
  const useProductInfo = ({ item }) => {
6
6
  const state = useCioPlpContext();
7
7
  const productSwatch = useProductSwatch({ item });
@@ -11,10 +11,15 @@ const useProductInfo = ({ item }) => {
11
11
  const getPrice = tryCatchify(state?.itemFieldGetters?.getPrice || defaultGetPrice);
12
12
  const getSalePrice = tryCatchify(state?.itemFieldGetters?.getSalePrice || defaultGetSalePrice);
13
13
  const getRolloverImage = tryCatchify(state?.itemFieldGetters?.getRolloverImage || defaultGetRolloverImage);
14
+ const getItemUrl = tryCatchify(state?.itemFieldGetters?.getItemUrl || defaultGetItemUrl);
14
15
  const itemName = productSwatch?.selectedVariation?.itemName || item.itemName;
15
16
  const itemPrice = productSwatch?.selectedVariation?.price || getPrice(item);
16
17
  const itemImageUrl = productSwatch?.selectedVariation?.imageUrl || item.imageUrl;
17
- const itemUrl = productSwatch?.selectedVariation?.url || item.url;
18
+ // Get href - merge variation URL into item if variation is selected
19
+ const itemWithVariationUrl = productSwatch?.selectedVariation
20
+ ? { ...item, url: productSwatch.selectedVariation.url }
21
+ : item;
22
+ const itemUrl = getItemUrl(itemWithVariationUrl) || productSwatch?.selectedVariation?.url;
18
23
  const variationId = productSwatch?.selectedVariation?.variationId;
19
24
  let rolloverImage = productSwatch?.selectedVariation?.rolloverImage;
20
25
  // Fallback to item's rollover image if all variations don't have a rollover image
@@ -7,3 +7,4 @@ export * from './dataAttributeHelpers';
7
7
  export * from './urlHelpers';
8
8
  export * from './requestConfigsHelpers';
9
9
  export * from './common';
10
+ export * from './shopifyDefaults';
@@ -33,3 +33,6 @@ export function getSwatchPreview(variation) {
33
33
  export function getIsHiddenGroupField(group) {
34
34
  return group?.data?.cio_plp_hidden;
35
35
  }
36
+ export function getItemUrl(item) {
37
+ return item?.url;
38
+ }
@@ -0,0 +1,37 @@
1
+ // eslint-disable-next-line import/prefer-default-export
2
+ export const shopifyDefaults = {
3
+ selector: '#cio-plp-ui-container',
4
+ urlHelpers: {
5
+ setUrl(url) {
6
+ const modifiedUrl = url.replace('/group_id', '/collections');
7
+ if (typeof window !== 'undefined') {
8
+ window.location.href = modifiedUrl;
9
+ }
10
+ },
11
+ },
12
+ callbacks: {
13
+ onAddToCart(_event, item, selectedVariation) {
14
+ // eslint-disable-next-line no-underscore-dangle
15
+ const shopifyId = item.data?.__shopify_id || selectedVariation?.variationId || item.itemId;
16
+ if (typeof window !== 'undefined') {
17
+ fetch('/cart/add.js', {
18
+ method: 'POST',
19
+ headers: { 'Content-Type': 'application/json' },
20
+ body: JSON.stringify({
21
+ id: shopifyId,
22
+ quantity: 1,
23
+ }),
24
+ }).catch((error) => {
25
+ // eslint-disable-next-line no-console
26
+ console.error('Failed to add item to cart:', error);
27
+ });
28
+ }
29
+ },
30
+ onProductCardClick(_event, item) {
31
+ if (typeof window !== 'undefined') {
32
+ const url = new URL(`/products/${item.itemId}`, window.location.origin);
33
+ window.location.href = url.href;
34
+ }
35
+ },
36
+ },
37
+ };
@@ -1 +1 @@
1
- export default '1.6.5';
1
+ export default '1.7.1';
@@ -17,6 +17,7 @@ export interface ItemFieldGetters {
17
17
  getSwatchPreview: (variation: Variation) => string;
18
18
  getSwatches: (item: Item, retrievePrice: ItemFieldGetters['getPrice'], retrieveSwatchPreview: ItemFieldGetters['getSwatchPreview'], retrieveSalePrice: ItemFieldGetters['getSalePrice'], retrieveRolloverImage: ItemFieldGetters['getRolloverImage']) => SwatchItem[] | undefined;
19
19
  getIsHiddenGroupField: (group: PlpItemGroup) => boolean | undefined;
20
+ getItemUrl: (item: Item) => string | undefined;
20
21
  }
21
22
  export interface Formatters {
22
23
  formatPrice: (price?: number) => string;
@@ -215,6 +216,7 @@ export interface CioPlpProviderProps {
215
216
  initialSearchResponse?: SearchResponse;
216
217
  initialBrowseResponse?: GetBrowseResultsResponse;
217
218
  staticRequestConfigs?: Partial<RequestConfigs>;
219
+ useShopifyDefaults?: boolean;
218
220
  }
219
221
  export type UseSortReturn = {
220
222
  sortOptions: PlpSortOption[];
@@ -7,3 +7,4 @@ export * from './dataAttributeHelpers';
7
7
  export * from './urlHelpers';
8
8
  export * from './requestConfigsHelpers';
9
9
  export * from './common';
10
+ export * from './shopifyDefaults';
@@ -5,3 +5,4 @@ export declare function getRolloverImage(item: Item | Variation): string | undef
5
5
  export declare function getSwatches(item: Item, retrievePrice: ItemFieldGetters['getPrice'], retrieveSwatchPreview: ItemFieldGetters['getSwatchPreview'], retrieveSalePrice: ItemFieldGetters['getSalePrice'], retrieveRolloverImage: ItemFieldGetters['getRolloverImage']): SwatchItem[] | undefined;
6
6
  export declare function getSwatchPreview(variation: Variation): string;
7
7
  export declare function getIsHiddenGroupField(group: PlpItemGroup): any;
8
+ export declare function getItemUrl(item: Item): string | undefined;
@@ -0,0 +1,7 @@
1
+ import type { Callbacks, UrlHelpers } from '../types';
2
+ export interface ShopifyDefaults {
3
+ selector: string;
4
+ urlHelpers: Pick<UrlHelpers, 'setUrl'>;
5
+ callbacks: Pick<Callbacks, 'onAddToCart' | 'onProductCardClick'>;
6
+ }
7
+ export declare const shopifyDefaults: ShopifyDefaults;
@@ -1,2 +1,2 @@
1
- declare const _default: "1.6.5";
1
+ declare const _default: "1.7.1";
2
2
  export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructor-io/constructorio-ui-plp",
3
- "version": "1.6.5",
3
+ "version": "1.7.1",
4
4
  "description": "Constructor PLP UI library for web applications",
5
5
  "author": "Constructor.io Corporation",
6
6
  "license": "MIT",
@@ -70,20 +70,20 @@
70
70
  "@babel/preset-typescript": "^7.27.1",
71
71
  "@chromatic-com/storybook": "^3.2.6",
72
72
  "@cspell/eslint-plugin": "^6.18.1",
73
- "@storybook/addon-a11y": "^8.6.14",
74
- "@storybook/addon-actions": "^8.6.14",
73
+ "@storybook/addon-a11y": "^8.6.15",
74
+ "@storybook/addon-actions": "^8.6.15",
75
75
  "@storybook/addon-essentials": "^8.6.14",
76
76
  "@storybook/addon-interactions": "^8.6.14",
77
- "@storybook/addon-links": "^8.6.14",
77
+ "@storybook/addon-links": "^8.6.15",
78
78
  "@storybook/addon-mdx-gfm": "^8.6.14",
79
79
  "@storybook/blocks": "^8.6.14",
80
- "@storybook/manager-api": "^8.6.14",
81
- "@storybook/react": "^8.6.14",
82
- "@storybook/react-vite": "^8.6.14",
83
- "@storybook/react-webpack5": "^8.6.14",
84
- "@storybook/test": "^8.6.14",
80
+ "@storybook/manager-api": "^8.6.15",
81
+ "@storybook/react": "^8.6.15",
82
+ "@storybook/react-vite": "^8.6.15",
83
+ "@storybook/react-webpack5": "^8.6.15",
84
+ "@storybook/test": "^8.6.15",
85
85
  "@storybook/test-runner": "^0.22.0",
86
- "@storybook/theming": "^8.6.14",
86
+ "@storybook/theming": "^8.6.15",
87
87
  "@testing-library/jest-dom": "^6.1.5",
88
88
  "@testing-library/react": "^14.0.0",
89
89
  "@types/jest": "^29.5.8",
@@ -110,7 +110,7 @@
110
110
  "react": "^18.2.0",
111
111
  "react-dom": "^18.2.0",
112
112
  "start-server-and-test": "^2.0.3",
113
- "storybook": "^8.6.14",
113
+ "storybook": "^8.6.15",
114
114
  "typescript": "^4.9.4",
115
115
  "vite": "^6.3.5",
116
116
  "vite-plugin-css-injected-by-js": "^3.1.0",