@okendo/shopify-hydrogen 1.2.1 → 1.2.2

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.
@@ -2,15 +2,16 @@ import React from 'react';
2
2
  import { OkendoWidgetClient } from './OkendoWidget.client';
3
3
  import { getOkendoProductId } from '../shared/productUtils';
4
4
  import { widgetConfigurationError, widgetMetafieldError } from '../shared/errorUtils';
5
+ import { logger } from '../shared/logger';
5
6
  export const OkendoClientStarRating = (props) => {
6
7
  const { productId, okendoStarRatingSnippet } = props;
7
8
  const okendoProductId = getOkendoProductId(productId);
8
9
  if (!okendoProductId) {
9
- console.error(widgetConfigurationError('OkendoClientStarRating', 'productId was not provided'));
10
+ logger.error(widgetConfigurationError('OkendoClientStarRating', 'productId was not provided'));
10
11
  return null;
11
12
  }
12
13
  if (!okendoStarRatingSnippet?.value) {
13
- console.warn(widgetMetafieldError('OkendoStarRating', 'StarRatingSnippet'));
14
+ logger.warn(widgetMetafieldError('OkendoStarRating', 'StarRatingSnippet'));
14
15
  }
15
16
  const dataAttributes = {
16
17
  'data-oke-star-rating': '',
@@ -2,21 +2,23 @@ import React, { useEffect, useRef } from 'react';
2
2
  export const OkendoWidgetClient = (props) => {
3
3
  const { dataAttributes, metafieldContent = '' } = props;
4
4
  const widgetContainer = useRef(null);
5
- const initializeReviewsWidget = () => {
5
+ const initializeWidget = () => {
6
6
  if (widgetContainer.current) {
7
- window.okeWidgetApi.initWidget(widgetContainer.current);
7
+ // `forceReinitialisation` must be true because we can expect `data-oke-rendered`
8
+ // to be present on already-initialised widget snippets.
9
+ window.okeWidgetApi.initWidget(widgetContainer.current, true);
8
10
  }
9
11
  };
10
12
  useEffect(() => {
11
13
  if (window.okeWidgetApi && widgetContainer.current) {
12
- initializeReviewsWidget();
14
+ initializeWidget();
13
15
  }
14
16
  else {
15
- document.addEventListener('oke-script-loaded', initializeReviewsWidget);
17
+ document.addEventListener('oke-script-loaded', initializeWidget);
16
18
  }
17
19
  return () => {
18
- document.removeEventListener('oke-script-loaded', initializeReviewsWidget);
20
+ document.removeEventListener('oke-script-loaded', initializeWidget);
19
21
  };
20
- }, []);
22
+ }, [dataAttributes]);
21
23
  return (React.createElement("div", { ref: widgetContainer, ...dataAttributes, dangerouslySetInnerHTML: { __html: metafieldContent } }));
22
24
  };
@@ -3,6 +3,7 @@ import { fetchSync, useShopQuery, gql, CacheShort } from '@shopify/hydrogen';
3
3
  import { Head } from '@shopify/hydrogen';
4
4
  import { okendoError } from '../shared/errorUtils';
5
5
  import { setInOkendoRequestContext } from '../shared/server/requestUtils';
6
+ import { logger } from '../shared/logger';
6
7
  const kDefaultOkendoApiDomain = 'api.okendo.io/v1';
7
8
  const kDefaultOkendoCdnDomain = 'cdn-static.okendo.io';
8
9
  export const OkendoProvider = (props) => {
@@ -11,7 +12,7 @@ export const OkendoProvider = (props) => {
11
12
  const url = `https://${apiDomain ?? kDefaultOkendoApiDomain}/stores/${subscriberId}/widget_plus_settings`;
12
13
  const settingsResponse = fetchSync(url, { preload: true });
13
14
  if (!settingsResponse.ok) {
14
- console.error(okendoError('Failed to retrieve subscriber settings. Please check your environment variables.'));
15
+ logger.error(okendoError('Failed to retrieve subscriber settings. Please check your environment variables.'));
15
16
  setInOkendoRequestContext('setupFailed', true);
16
17
  return null;
17
18
  }
@@ -24,7 +25,7 @@ export const OkendoProvider = (props) => {
24
25
  preload: true
25
26
  });
26
27
  if (!initScriptResponse.ok) {
27
- console.error(okendoError('Failed to retrieve widget initialization script.'));
28
+ logger.error(okendoError('Failed to retrieve widget initialization script.'));
28
29
  setInOkendoRequestContext('setupFailed', true);
29
30
  return null;
30
31
  }
@@ -36,14 +37,14 @@ export const OkendoProvider = (props) => {
36
37
  ? `/products/${product.productHandle}/${product.variantId ? '?variantId=' + product.variantId : ''}`
37
38
  : undefined;
38
39
  // Get pre-rendered style settings.
39
- const query = gql `
40
- query metafields {
41
- shop {
42
- widgetPreRenderStyleTags: metafield(namespace: "okendo", key: "WidgetPreRenderStyleTags") {
43
- value
44
- }
45
- }
46
- }
40
+ const query = gql `
41
+ query metafields {
42
+ shop {
43
+ widgetPreRenderStyleTags: metafield(namespace: "okendo", key: "WidgetPreRenderStyleTags") {
44
+ value
45
+ }
46
+ }
47
+ }
47
48
  `;
48
49
  const { data: { shop: { widgetPreRenderStyleTags } } } = useShopQuery({
49
50
  query: query,
@@ -51,7 +52,7 @@ export const OkendoProvider = (props) => {
51
52
  });
52
53
  const preRenderStyleTags = widgetPreRenderStyleTags?.value ?? '';
53
54
  if (!preRenderStyleTags) {
54
- console.warn(okendoError('Failed to retrieve pre-rendered widget style settings.'));
55
+ logger.warn(okendoError('Failed to retrieve pre-rendered widget style settings.'));
55
56
  }
56
57
  return (React.createElement(React.Fragment, null,
57
58
  React.createElement(Head, null,
@@ -4,6 +4,7 @@ import { OkendoWidgetClient } from '../client-components/OkendoWidget.client';
4
4
  import { widgetMetafieldError } from '../shared/errorUtils';
5
5
  import { getOkendoProductId } from '../shared/productUtils';
6
6
  import { useOkendoRequestContext } from '../shared/server/requestUtils';
7
+ import { logger } from '../shared/logger';
7
8
  export const OkendoReviewsWidget = (props) => {
8
9
  const { setupFailed } = useOkendoRequestContext();
9
10
  if (setupFailed) {
@@ -11,23 +12,23 @@ export const OkendoReviewsWidget = (props) => {
11
12
  }
12
13
  const { productId } = props;
13
14
  const productQuery = productId
14
- ? `
15
- product(id: $productId) {
16
- reviewsWidgetSnippet: metafield(namespace: "okendo", key: "ReviewsWidgetSnippet") {
17
- value
18
- }
19
- }
15
+ ? `
16
+ product(id: $productId) {
17
+ reviewsWidgetSnippet: metafield(namespace: "okendo", key: "ReviewsWidgetSnippet") {
18
+ value
19
+ }
20
+ }
20
21
  `
21
22
  : '';
22
- const query = gql `
23
- query metafields${productId ? '($productId: ID!)' : ''} {
24
- ${productQuery}
25
- shop {
26
- widgetPreRenderBodyStyleTags: metafield(namespace: "okendo", key: "WidgetPreRenderBodyStyleTags") {
27
- value
28
- }
29
- }
30
- }
23
+ const query = gql `
24
+ query metafields${productId ? '($productId: ID!)' : ''} {
25
+ ${productQuery}
26
+ shop {
27
+ widgetPreRenderBodyStyleTags: metafield(namespace: "okendo", key: "WidgetPreRenderBodyStyleTags") {
28
+ value
29
+ }
30
+ }
31
+ }
31
32
  `;
32
33
  const { data: { product, shop } } = useShopQuery({
33
34
  query: query,
@@ -37,10 +38,10 @@ export const OkendoReviewsWidget = (props) => {
37
38
  }
38
39
  });
39
40
  if (productId && !product?.reviewsWidgetSnippet?.value) {
40
- console.warn(widgetMetafieldError('OkendoReviewsWidget', 'ReviewsWidgetSnippet'));
41
+ logger.warn(widgetMetafieldError('OkendoReviewsWidget', 'ReviewsWidgetSnippet'));
41
42
  }
42
43
  if (!shop?.widgetPreRenderBodyStyleTags?.value) {
43
- console.warn(widgetMetafieldError('OkendoReviewsWidget', 'WidgetPreRenderBodyStyleTags'));
44
+ logger.warn(widgetMetafieldError('OkendoReviewsWidget', 'WidgetPreRenderBodyStyleTags'));
44
45
  }
45
46
  const dataAttributes = {
46
47
  'data-oke-widget': ''
@@ -4,6 +4,7 @@ import { OkendoWidgetClient } from '../client-components/OkendoWidget.client';
4
4
  import { widgetConfigurationError, widgetMetafieldError } from '../shared/errorUtils';
5
5
  import { getOkendoProductId } from '../shared/productUtils';
6
6
  import { useOkendoRequestContext } from '../shared/server/requestUtils';
7
+ import { logger } from '../shared/logger';
7
8
  export const OkendoStarRating = (props) => {
8
9
  const { setupFailed } = useOkendoRequestContext();
9
10
  if (setupFailed) {
@@ -12,17 +13,17 @@ export const OkendoStarRating = (props) => {
12
13
  const { productId } = props;
13
14
  const okendoProductId = getOkendoProductId(productId);
14
15
  if (!okendoProductId) {
15
- console.error(widgetConfigurationError('OkendoStarRating', 'productId was not provided'));
16
+ logger.error(widgetConfigurationError('OkendoStarRating', 'productId was not provided'));
16
17
  return null;
17
18
  }
18
- const query = gql `
19
- query metafields($productId: ID!) {
20
- product(id: $productId) {
21
- starRatingSnippet: metafield(namespace: "okendo", key: "StarRatingSnippet") {
22
- value
23
- }
24
- }
25
- }
19
+ const query = gql `
20
+ query metafields($productId: ID!) {
21
+ product(id: $productId) {
22
+ starRatingSnippet: metafield(namespace: "okendo", key: "StarRatingSnippet") {
23
+ value
24
+ }
25
+ }
26
+ }
26
27
  `;
27
28
  const { data: { product: { starRatingSnippet } } } = useShopQuery({
28
29
  query: query,
@@ -32,7 +33,7 @@ export const OkendoStarRating = (props) => {
32
33
  }
33
34
  });
34
35
  if (!starRatingSnippet?.value) {
35
- console.warn(widgetMetafieldError('OkendoStarRating', 'StarRatingSnippet'));
36
+ logger.warn(widgetMetafieldError('OkendoStarRating', 'StarRatingSnippet'));
36
37
  }
37
38
  const dataAttributes = {
38
39
  'data-oke-star-rating': '',
@@ -1,11 +1,11 @@
1
1
  import { gql } from '@shopify/hydrogen';
2
- export const OKENDO_PRODUCT_STAR_RATING_FRAGMENT = gql `
3
- fragment OkendoStarRatingSnippet on Product {
4
- okendoStarRatingSnippet: metafield(
5
- namespace: "okendo"
6
- key: "StarRatingSnippet"
7
- ) {
8
- value
9
- }
10
- }
2
+ export const OKENDO_PRODUCT_STAR_RATING_FRAGMENT = gql `
3
+ fragment OkendoStarRatingSnippet on Product {
4
+ okendoStarRatingSnippet: metafield(
5
+ namespace: "okendo"
6
+ key: "StarRatingSnippet"
7
+ ) {
8
+ value
9
+ }
10
+ }
11
11
  `;
@@ -0,0 +1,14 @@
1
+ export declare const logger: {
2
+ log: {
3
+ (...data: any[]): void;
4
+ (message?: any, ...optionalParams: any[]): void;
5
+ };
6
+ warn: {
7
+ (...data: any[]): void;
8
+ (message?: any, ...optionalParams: any[]): void;
9
+ };
10
+ error: {
11
+ (...data: any[]): void;
12
+ (message?: any, ...optionalParams: any[]): void;
13
+ };
14
+ };
@@ -0,0 +1,7 @@
1
+ const isProduction = process.env.NODE_ENV === 'production';
2
+ const noop = () => undefined;
3
+ export const logger = {
4
+ log: isProduction ? noop : console.log,
5
+ warn: isProduction ? noop : console.warn,
6
+ error: console.error
7
+ };
package/package.json CHANGED
@@ -1,58 +1,58 @@
1
- {
2
- "name": "@okendo/shopify-hydrogen",
3
- "version": "1.2.1",
4
- "description": "A component library containing Okendo Reviews React components.",
5
- "main": "dist/esnext/index.js",
6
- "engines": {
7
- "node": ">=14"
8
- },
9
- "exports": {
10
- ".": "./dist/esnext/index.js",
11
- "./plugin": {
12
- "import": "./dist/esnext/framework/plugins/plugin.js",
13
- "require": "./dist/node/framework/plugins/plugin.js"
14
- },
15
- "./client": {
16
- "import": {
17
- "types": "./dist/esnext/client.d.ts",
18
- "default": "./dist/esnext/client.js"
19
- },
20
- "node": {
21
- "types": "./dist/esnext/client.d.ts",
22
- "default": "./dist/esnext/client.js"
23
- }
24
- },
25
- "./plugin.cjs": "./plugin.cjs",
26
- "./package.json": "./package.json",
27
- "./*": "./dist/esnext/*.js"
28
- },
29
- "types": "dist/esnext/index.d.ts",
30
- "files": [
31
- "dist",
32
- "plugin.cjs"
33
- ],
34
- "scripts": {
35
- "build": "rimraf dist/ && npm run build:esm && npm run build:plugin:cjs",
36
- "build:plugin:cjs": "tsc --p tsconfig.plugin.cjs.json",
37
- "build:esm": "tsc --p tsconfig.esm.json",
38
- "prepublishOnly": "npm run build"
39
- },
40
- "author": "Okendo",
41
- "license": "SEE LICENSE IN LICENSE.txt",
42
- "devDependencies": {
43
- "@okendo/reviews-widget-plus": "^0.49.0",
44
- "@okendo/tsconfig": "0.0.2",
45
- "@shopify/hydrogen": "^1.2.0",
46
- "@types/node": "^17.0.35",
47
- "@types/react": "^18.0.14",
48
- "@types/react-dom": "^18.0.5",
49
- "dotenv": "^16.0.1",
50
- "ncp": "^2.0.0",
51
- "rimraf": "^3.0.2",
52
- "typescript": "^4.7.4",
53
- "vite": "^2.9.0"
54
- },
55
- "peerDependencies": {
56
- "@shopify/hydrogen": "1.2.x"
57
- }
58
- }
1
+ {
2
+ "name": "@okendo/shopify-hydrogen",
3
+ "version": "1.2.2",
4
+ "description": "A component library containing Okendo Reviews React components.",
5
+ "main": "dist/esnext/index.js",
6
+ "engines": {
7
+ "node": ">=14"
8
+ },
9
+ "exports": {
10
+ ".": "./dist/esnext/index.js",
11
+ "./plugin": {
12
+ "import": "./dist/esnext/framework/plugins/plugin.js",
13
+ "require": "./dist/node/framework/plugins/plugin.js"
14
+ },
15
+ "./client": {
16
+ "import": {
17
+ "types": "./dist/esnext/client.d.ts",
18
+ "default": "./dist/esnext/client.js"
19
+ },
20
+ "node": {
21
+ "types": "./dist/esnext/client.d.ts",
22
+ "default": "./dist/esnext/client.js"
23
+ }
24
+ },
25
+ "./plugin.cjs": "./plugin.cjs",
26
+ "./package.json": "./package.json",
27
+ "./*": "./dist/esnext/*.js"
28
+ },
29
+ "types": "dist/esnext/index.d.ts",
30
+ "files": [
31
+ "dist",
32
+ "plugin.cjs"
33
+ ],
34
+ "scripts": {
35
+ "build": "rimraf dist/ && npm run build:esm && npm run build:plugin:cjs",
36
+ "build:plugin:cjs": "tsc --p tsconfig.plugin.cjs.json",
37
+ "build:esm": "tsc --p tsconfig.esm.json",
38
+ "prepublishOnly": "npm run build"
39
+ },
40
+ "author": "Okendo",
41
+ "license": "SEE LICENSE IN LICENSE.txt",
42
+ "devDependencies": {
43
+ "@okendo/reviews-widget-plus": "^0.53.1",
44
+ "@okendo/tsconfig": "0.0.2",
45
+ "@shopify/hydrogen": "^1.2.0",
46
+ "@types/node": "^17.0.35",
47
+ "@types/react": "^18.0.14",
48
+ "@types/react-dom": "^18.0.5",
49
+ "dotenv": "^16.0.1",
50
+ "ncp": "^2.0.0",
51
+ "rimraf": "^3.0.2",
52
+ "typescript": "^4.7.4",
53
+ "vite": "^2.9.0"
54
+ },
55
+ "peerDependencies": {
56
+ "@shopify/hydrogen": "1.2.x"
57
+ }
58
+ }
package/plugin.cjs CHANGED
@@ -1 +1 @@
1
- module.exports = require('./dist/node/framework/plugin');
1
+ module.exports = require('./dist/node/framework/plugin');