@gem-sdk/core 1.23.0-staging.165 → 1.23.0-staging.172

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,12 +2,15 @@
2
2
 
3
3
  var variant = require('./variant.js');
4
4
 
5
- function getSelectedVariant(variants, choices) {
5
+ function getSelectedVariant(variants, choices, variantId) {
6
6
  /**
7
7
  * Ensure the user has selected all the required options, not just some.
8
8
  */ if (!variants || !variants.length || !choices || variants[0]?.selectedOptions.length !== Object.keys(choices).length) {
9
9
  return;
10
10
  }
11
+ if (variantId) {
12
+ return variants.find((variant)=>variant?.id === variantId);
13
+ }
11
14
  return variants.find((variant)=>{
12
15
  return Object.entries(choices).every(([name, value])=>{
13
16
  return variant?.selectedOptions.some((option)=>option.name === name && option.value === value);
@@ -77,6 +77,9 @@ const useQuantity = ()=>{
77
77
  ]);
78
78
  };
79
79
  const useSelectedOption = ()=>{
80
+ const url = new URL(window.location.href);
81
+ const variantId = url.searchParams.get('variant');
82
+ const selectedVariant = useVariant(variantId ?? '');
80
83
  const setSelectedOption = ProductContext.useProductStore((s)=>s.setSelectedOption);
81
84
  const selectedOptions = ProductContext.useProductStore((s)=>s.selectedOptions);
82
85
  const forceSelectedOption = ProductContext.useProductStore((s)=>s.forceSelectedOption);
@@ -119,13 +122,22 @@ const useSelectedOption = ()=>{
119
122
  isSyncProduct
120
123
  ]);
121
124
  return react.useMemo(()=>({
122
- selectedOptions,
125
+ selectedOptions: selectedVariant ? selectedVariant.selectedOptions.reduce((a, b)=>{
126
+ if (b.name) {
127
+ return {
128
+ ...a,
129
+ [b.name]: b.value
130
+ };
131
+ }
132
+ return a;
133
+ }, {}) ?? undefined : selectedOptions,
123
134
  setSelectedOption: updateOption,
124
135
  forceSelectedOption: forceOption
125
136
  }), [
126
137
  selectedOptions,
127
138
  updateOption,
128
- forceOption
139
+ forceOption,
140
+ selectedVariant
129
141
  ]);
130
142
  };
131
143
  const useVariants = ()=>{
@@ -1,11 +1,14 @@
1
1
  import { checkInStock } from './variant.js';
2
2
 
3
- function getSelectedVariant(variants, choices) {
3
+ function getSelectedVariant(variants, choices, variantId) {
4
4
  /**
5
5
  * Ensure the user has selected all the required options, not just some.
6
6
  */ if (!variants || !variants.length || !choices || variants[0]?.selectedOptions.length !== Object.keys(choices).length) {
7
7
  return;
8
8
  }
9
+ if (variantId) {
10
+ return variants.find((variant)=>variant?.id === variantId);
11
+ }
9
12
  return variants.find((variant)=>{
10
13
  return Object.entries(choices).every(([name, value])=>{
11
14
  return variant?.selectedOptions.some((option)=>option.name === name && option.value === value);
@@ -75,6 +75,9 @@ const useQuantity = ()=>{
75
75
  ]);
76
76
  };
77
77
  const useSelectedOption = ()=>{
78
+ const url = new URL(window.location.href);
79
+ const variantId = url.searchParams.get('variant');
80
+ const selectedVariant = useVariant(variantId ?? '');
78
81
  const setSelectedOption = useProductStore((s)=>s.setSelectedOption);
79
82
  const selectedOptions = useProductStore((s)=>s.selectedOptions);
80
83
  const forceSelectedOption = useProductStore((s)=>s.forceSelectedOption);
@@ -117,13 +120,22 @@ const useSelectedOption = ()=>{
117
120
  isSyncProduct
118
121
  ]);
119
122
  return useMemo(()=>({
120
- selectedOptions,
123
+ selectedOptions: selectedVariant ? selectedVariant.selectedOptions.reduce((a, b)=>{
124
+ if (b.name) {
125
+ return {
126
+ ...a,
127
+ [b.name]: b.value
128
+ };
129
+ }
130
+ return a;
131
+ }, {}) ?? undefined : selectedOptions,
121
132
  setSelectedOption: updateOption,
122
133
  forceSelectedOption: forceOption
123
134
  }), [
124
135
  selectedOptions,
125
136
  updateOption,
126
- forceOption
137
+ forceOption,
138
+ selectedVariant
127
139
  ]);
128
140
  };
129
141
  const useVariants = ()=>{
@@ -9515,7 +9515,7 @@ declare const composeRadiusResponsive: (radiusValue?: ObjectDevices<CornerRadius
9515
9515
  declare const getRadiusStyleActiveState: (radiusValue?: StateProp<CornerRadius>) => React.CSSProperties;
9516
9516
  declare const composeCornerCss: (value?: CornerRadius | undefined, important?: boolean) => string | undefined;
9517
9517
 
9518
- declare function getSelectedVariant(variants?: Maybe<VariantSelectFragment>[], choices?: Record<string, string>): Maybe<VariantSelectFragment>;
9518
+ declare function getSelectedVariant(variants?: Maybe<VariantSelectFragment>[], choices?: Record<string, string>, variantId?: string | null): Maybe<VariantSelectFragment>;
9519
9519
  declare function parseSelectedOption(options: SelectedOption[]): SelectedOption | undefined;
9520
9520
  declare function checkAvailableVariantInStock(variants: Maybe<VariantSelectFragment>[] | undefined, optionId: string, optionValue: string): boolean;
9521
9521
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gem-sdk/core",
3
- "version": "1.23.0-staging.165",
3
+ "version": "1.23.0-staging.172",
4
4
  "license": "MIT",
5
5
  "sideEffects": false,
6
6
  "main": "dist/cjs/index.js",
@@ -25,7 +25,7 @@
25
25
  },
26
26
  "devDependencies": {
27
27
  "@gem-sdk/adapter-shopify": "1.23.0-staging.26",
28
- "@gem-sdk/styles": "1.23.0-staging.157"
28
+ "@gem-sdk/styles": "1.23.0-staging.167"
29
29
  },
30
30
  "dependencies": {
31
31
  "react-error-boundary": "4.0.10",