1 | import { useMemo, useState } from 'react';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | export function useProductVariants(product, { initialVariant } = {}) {
|
7 | var _a, _b;
|
8 | const variantOptions = (_a = product === null || product === void 0 ? void 0 : product.variants) === null || _a === void 0 ? void 0 : _a.options;
|
9 | const variantValues = (_b = product === null || product === void 0 ? void 0 : product.variants) === null || _b === void 0 ? void 0 : _b.values;
|
10 |
|
11 |
|
12 | const normalisedValues = useMemo(() => {
|
13 | if (!variantValues)
|
14 | return;
|
15 | return variantValues.reduce((acc, curr) => {
|
16 | const valueIdentifier = curr.values.join(':');
|
17 | acc[valueIdentifier] = curr;
|
18 | return acc;
|
19 | }, {});
|
20 | }, [variantValues]);
|
21 | const [selectedValues, selectValues] = useVariantState({
|
22 | variantOptions,
|
23 | initialVariant
|
24 | });
|
25 | const selectedVariant = useMemo(() => {
|
26 | return getSelectedVariant(normalisedValues, selectedValues);
|
27 | }, [normalisedValues, selectedValues]);
|
28 | const validation = useMemo(() => {
|
29 | if (!variantOptions || variantOptions.length === 0)
|
30 | return 'valid';
|
31 | const selectedValueLength = selectedValues.filter(Boolean).length;
|
32 | const allValuesSelected = variantOptions.length === selectedValueLength;
|
33 | const invalidValueCombination = allValuesSelected && !selectedVariant;
|
34 | if (invalidValueCombination) {
|
35 | return 'invalid';
|
36 | }
|
37 | if (!allValuesSelected)
|
38 | return 'incomplete';
|
39 | const variantIsInStock = selectedVariant.stockStatus.buyable;
|
40 | return variantIsInStock ? 'valid' : 'outOfStock';
|
41 | }, [selectedValues, selectedVariant, variantOptions]);
|
42 | function getSelectedValue(option) {
|
43 | const optionIndex = variantOptions.indexOf(option);
|
44 | return selectedValues[optionIndex];
|
45 | }
|
46 | function selectValue(value, option) {
|
47 | const selectedIndex = variantOptions.indexOf(option);
|
48 | const currentValueForOption = selectedValues[selectedIndex];
|
49 | const valueIsValid = valueIsValidForOption(variantOptions, option, value);
|
50 | if (!valueIsValid) {
|
51 | throw new Error(`${value} is not a valid value for ${option.name}`);
|
52 | }
|
53 | if (currentValueForOption === value)
|
54 | return;
|
55 | selectValues(state => {
|
56 | const newState = [...state];
|
57 | newState[selectedIndex] = value;
|
58 | return newState;
|
59 | });
|
60 | }
|
61 | |
62 |
|
63 |
|
64 | function validateSelection(value, option) {
|
65 | const totalOptionsLength = variantOptions.length;
|
66 | const selectedValuesLength = selectedValues.filter(Boolean).length;
|
67 | const optionLevel = variantOptions.indexOf(option);
|
68 | const selectedValue = selectedValues[optionLevel];
|
69 | if (!valueIsValidForOption(variantOptions, option, value)) {
|
70 | return 'invalid';
|
71 | }
|
72 | if (selectedValuesLength < totalOptionsLength - 1) {
|
73 | return 'incomplete';
|
74 | }
|
75 | if (selectedValue && selectedValuesLength !== totalOptionsLength) {
|
76 | return 'incomplete';
|
77 | }
|
78 | const variant = getProposedVariant({
|
79 | currentState: selectedValues,
|
80 | variantTable: normalisedValues,
|
81 | optionLevel,
|
82 | value
|
83 | });
|
84 | if (!variant)
|
85 | return 'invalid';
|
86 | const variantIsBuyable = variant.stockStatus.buyable;
|
87 | if (!variantIsBuyable)
|
88 | return 'outOfStock';
|
89 | return 'valid';
|
90 | }
|
91 | const missingOptions = useMemo(() => {
|
92 | if (!variantOptions)
|
93 | return null;
|
94 | const noneMissing = selectedValues.filter(Boolean).length === variantOptions.length;
|
95 | const allMissing = selectedValues.length === 0;
|
96 | if (noneMissing)
|
97 | return null;
|
98 | if (allMissing)
|
99 | return variantOptions;
|
100 | const unselectedOptions = variantOptions.filter((_, index) => {
|
101 | const optionIsNotSelected = typeof selectedValues[index] === 'undefined';
|
102 | return optionIsNotSelected;
|
103 | });
|
104 | return unselectedOptions;
|
105 | }, [selectedValues, variantOptions]);
|
106 | function getVariantForSelection(value, option) {
|
107 | return getProposedVariant({
|
108 | currentState: selectedValues,
|
109 | variantTable: normalisedValues,
|
110 | optionLevel: variantOptions.indexOf(option),
|
111 | value
|
112 | });
|
113 | }
|
114 | function getMissingOptions() {
|
115 | return missingOptions;
|
116 | }
|
117 | return {
|
118 | selectedVariant,
|
119 | validation,
|
120 | getSelectedValue,
|
121 | selectValue,
|
122 | validateSelection,
|
123 | getVariantForSelection,
|
124 | missingOptions,
|
125 | getMissingOptions
|
126 | };
|
127 | }
|
128 | function useVariantState({ variantOptions, initialVariant }) {
|
129 | const defaultValues = useMemo(() => getDefaultValues(variantOptions, initialVariant), [initialVariant, variantOptions]);
|
130 | const [selectedValues, selectValues] = useState(defaultValues);
|
131 | return [selectedValues, selectValues];
|
132 | }
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | function getDefaultValues(options, initialVariant) {
|
140 | if (initialVariant === null || initialVariant === void 0 ? void 0 : initialVariant.values)
|
141 | return initialVariant.values;
|
142 | if (!options)
|
143 | return [];
|
144 | const optionsWithSingleValue = options
|
145 | .map((option, index) => {
|
146 | return option.values.length === 1
|
147 | ? { value: option.values[0], index }
|
148 | : null;
|
149 | })
|
150 | .filter(Boolean);
|
151 | if (optionsWithSingleValue.length === 0)
|
152 | return [];
|
153 | const defaultValues = [];
|
154 | for (const { value, index } of optionsWithSingleValue) {
|
155 | defaultValues[index] = value;
|
156 | }
|
157 | return defaultValues;
|
158 | }
|
159 | function getSelectedVariant(normalisedValues, selectedValues) {
|
160 | return normalisedValues === null || normalisedValues === void 0 ? void 0 : normalisedValues[selectedValues.join(':')];
|
161 | }
|
162 | function valueIsValidForOption(variantOptions, option, value) {
|
163 | const optionLevel = variantOptions.indexOf(option);
|
164 | const possibleValues = variantOptions[optionLevel].values;
|
165 | const isValid = possibleValues.includes(value);
|
166 | return isValid;
|
167 | }
|
168 | function getProposedVariant({ currentState, variantTable, optionLevel, value }) {
|
169 | const proposedState = [...currentState];
|
170 | proposedState[optionLevel] = value;
|
171 | const variant = getSelectedVariant(variantTable, proposedState);
|
172 | return variant;
|
173 | }
|
174 |
|
\ | No newline at end of file |