@akinon/pz-virtual-try-on 1.119.0-rc.3 → 2.0.0-beta.13

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.
@@ -1,232 +0,0 @@
1
- 'use client';
2
-
3
- import React, { useState, useCallback, useMemo, useEffect } from 'react';
4
- import { Product } from '@akinon/next/types';
5
- import { BarcodeScannerButton } from './barcode-scanner-button';
6
- import { BarcodeScannerModal } from './barcode-scanner-modal';
7
- import { VirtualTryOnProductSelector } from './virtual-try-on-product-selector';
8
- import { VirtualTryOnUploadModal } from './virtual-try-on-upload-modal';
9
- import { BasketAsyncModal } from './basket-async-modal';
10
- import { useVirtualTryOnAsync } from '../hooks/use-virtual-try-on-async';
11
- import { getVirtualTryOnEnabled } from '../utils';
12
- import type {
13
- BarcodeScannerSettings,
14
- VirtualTryOnPluginSettings,
15
- BasketProduct
16
- } from '../types';
17
-
18
- export interface BarcodeScannerPluginProps {
19
- className?: string;
20
- barcodeScannerSettings?: BarcodeScannerSettings;
21
- vtoSettings?: VirtualTryOnPluginSettings;
22
-
23
- settings?: {
24
- barcodeScanner?: BarcodeScannerSettings;
25
- vto?: VirtualTryOnPluginSettings;
26
- };
27
-
28
- onProductsScanned?: (products: Product[]) => void;
29
- onVTOComplete?: (result: any) => void;
30
-
31
- showVTOContinue?: boolean;
32
- }
33
-
34
- export function BarcodeScannerPlugin({
35
- className,
36
- barcodeScannerSettings,
37
- vtoSettings,
38
- settings,
39
- onProductsScanned,
40
- onVTOComplete,
41
- showVTOContinue = true
42
- }: BarcodeScannerPluginProps) {
43
- // Check if virtual try-on is enabled
44
- const [isEnabled, setIsEnabled] = useState(false);
45
-
46
- // Merge settings from props and PluginModule settings
47
- const finalBarcodeScannerSettings =
48
- barcodeScannerSettings || settings?.barcodeScanner;
49
- const finalVtoSettings = vtoSettings || settings?.vto;
50
-
51
- const [isScannerModalOpen, setIsScannerModalOpen] = useState(false);
52
- const [isSelectorOpen, setIsSelectorOpen] = useState(false);
53
- const [showUploadModal, setShowUploadModal] = useState(false);
54
- const [showAsyncModal, setShowAsyncModal] = useState(false);
55
- const [scannedProducts, setScannedProducts] = useState<BasketProduct[]>([]);
56
- const [selectedProducts, setSelectedProducts] = useState<BasketProduct[]>([]);
57
- const [categoryMapping, setCategoryMapping] = useState<
58
- Record<string, number[]>
59
- >({});
60
-
61
- useEffect(() => {
62
- setIsEnabled(getVirtualTryOnEnabled());
63
- }, []);
64
-
65
- // Fetch categories when products are selected
66
- useEffect(() => {
67
- if (selectedProducts.length === 0) {
68
- setCategoryMapping({});
69
- return;
70
- }
71
-
72
- const fetchCategories = async () => {
73
- try {
74
- const pks = selectedProducts.map((item) => item.pk).join(',');
75
- const response = await fetch(`/api/product-categories?pks=${pks}`);
76
-
77
- if (response.ok) {
78
- const mapping = await response.json();
79
- setCategoryMapping(mapping);
80
- }
81
- } catch (error) {}
82
- };
83
-
84
- fetchCategories();
85
- }, [selectedProducts]);
86
-
87
- const hookData = useVirtualTryOnAsync(
88
- selectedProducts.length > 0
89
- ? selectedProducts
90
- : [{ pk: 0, sku: '', name: '', productimage_set: [], attributes: {} }],
91
- categoryMapping
92
- );
93
-
94
- const handleOpenScanner = useCallback(() => {
95
- setIsScannerModalOpen(true);
96
- }, []);
97
-
98
- const handleCloseScannerModal = useCallback(() => {
99
- setIsScannerModalOpen(false);
100
- setScannedProducts([]);
101
- }, []);
102
-
103
- const handleProductsFound = useCallback(
104
- (products: Product[]) => {
105
- onProductsScanned?.(products);
106
- },
107
- [onProductsScanned]
108
- );
109
-
110
- const handleContinueToVTO = useCallback((products: Product[]) => {
111
- if (products.length === 0) {
112
- return;
113
- }
114
-
115
- const basketProducts: BasketProduct[] = products.map((product) => ({
116
- pk: product.pk,
117
- sku: product.sku || '',
118
- name: product.name,
119
- productimage_set: product.productimage_set,
120
- attributes: product.attributes,
121
- category: (product as any).category
122
- }));
123
-
124
- setIsScannerModalOpen(false);
125
- setScannedProducts(basketProducts);
126
-
127
- if (basketProducts.length === 1) {
128
- setSelectedProducts(basketProducts);
129
- setShowUploadModal(true);
130
- } else {
131
- setIsSelectorOpen(true);
132
- }
133
- }, []);
134
-
135
- const handleProductsSelected = useCallback((products: BasketProduct[]) => {
136
- setSelectedProducts(products);
137
- setIsSelectorOpen(false);
138
- setShowUploadModal(true);
139
- }, []);
140
-
141
- const handleSelectorClose = useCallback(() => {
142
- setIsSelectorOpen(false);
143
- setScannedProducts([]);
144
- }, []);
145
-
146
- const handleUploadModalClose = useCallback(() => {
147
- hookData.cancelTryOn?.();
148
- setShowUploadModal(false);
149
- setSelectedProducts([]);
150
- setScannedProducts([]);
151
- setCategoryMapping({});
152
- }, [hookData]);
153
-
154
- const handleAsyncModalClose = useCallback(() => {
155
- hookData.cancelTryOn?.();
156
- setShowAsyncModal(false);
157
- setSelectedProducts([]);
158
- setScannedProducts([]);
159
- setCategoryMapping({});
160
- hookData.reset();
161
- }, [hookData]);
162
-
163
- const handleProcessingStart = useCallback(() => {
164
- setShowUploadModal(false);
165
- setShowAsyncModal(true);
166
- }, []);
167
-
168
- const mergedBarcodeSettings = useMemo(
169
- (): BarcodeScannerSettings => ({
170
- ...finalBarcodeScannerSettings,
171
- showContinueToVTO: showVTOContinue
172
- }),
173
- [finalBarcodeScannerSettings, showVTOContinue]
174
- );
175
-
176
- if (!isEnabled) {
177
- return null;
178
- }
179
-
180
- return (
181
- <>
182
- <BarcodeScannerButton
183
- onClick={handleOpenScanner}
184
- className={className}
185
- settings={mergedBarcodeSettings}
186
- />
187
-
188
- <BarcodeScannerModal
189
- isOpen={isScannerModalOpen}
190
- onClose={handleCloseScannerModal}
191
- onProductFound={handleProductsFound}
192
- onContinueToVTO={handleContinueToVTO}
193
- settings={mergedBarcodeSettings}
194
- />
195
-
196
- {isSelectorOpen && scannedProducts.length > 0 && (
197
- <VirtualTryOnProductSelector
198
- isOpen={true}
199
- onClose={handleSelectorClose}
200
- products={scannedProducts}
201
- maxSelection={3}
202
- onConfirm={handleProductsSelected}
203
- settings={finalVtoSettings}
204
- />
205
- )}
206
-
207
- {showUploadModal && selectedProducts.length > 0 && (
208
- <VirtualTryOnUploadModal
209
- isOpen={true}
210
- onClose={handleUploadModalClose}
211
- hookData={hookData}
212
- onProcessingStart={handleProcessingStart}
213
- settings={finalVtoSettings}
214
- />
215
- )}
216
-
217
- {showAsyncModal && selectedProducts.length > 0 && (
218
- <BasketAsyncModal
219
- isOpen={true}
220
- onClose={handleAsyncModalClose}
221
- hookData={hookData}
222
- products={selectedProducts}
223
- onComplete={onVTOComplete}
224
- settings={finalVtoSettings}
225
- source="barcode"
226
- />
227
- )}
228
- </>
229
- );
230
- }
231
-
232
- export default BarcodeScannerPlugin;