1
1
import type { JSX , i18n } from '@coveo/atomic' ;
2
2
import React , { useEffect , useRef } from 'react' ;
3
- import { AtomicCommerceInterface } from '../stencil-generated/commerce/index.js ' ;
3
+ import { AtomicCommerceInterface } from './components ' ;
4
4
5
- type ExecuteRequest = HTMLAtomicCommerceInterfaceElement [ 'executeFirstRequest' ] ;
5
+ export type AtomicCommerceInterface = React . ComponentProps <
6
+ typeof AtomicCommerceInterface
7
+ > ;
8
+
9
+ type ExecuteRequest = AtomicCommerceInterface [ 'executeFirstRequest' ] ;
6
10
7
11
/**
8
12
* The properties of the AtomicCommerceInterface component
9
13
*/
10
14
interface WrapperProps
11
- extends Omit < JSX . AtomicCommerceInterface , 'i18n' | 'pipeline' | 'searchHub' > {
15
+ extends Omit < AtomicCommerceInterface , 'i18n' | 'pipeline' | 'searchHub' > {
12
16
/**
13
17
* An optional callback function that can be used to control the execution of the first request.
14
18
*
@@ -26,7 +30,7 @@ interface WrapperProps
26
30
27
31
const DefaultProps : Required < Pick < WrapperProps , 'onReady' | 'localization' > > = {
28
32
onReady : ( executeFirstRequest ) => {
29
- return executeFirstRequest ( ) ;
33
+ return executeFirstRequest ? executeFirstRequest ( ) : Promise . resolve ( ) ;
30
34
} ,
31
35
localization : ( ) => { } ,
32
36
} ;
@@ -45,21 +49,26 @@ export const InterfaceWrapper = (
45
49
//TODO, maybe: provide a default engine
46
50
}
47
51
const { engine, localization, onReady, ...allOtherProps } = mergedProps ;
48
- const interfaceRef = useRef < HTMLAtomicCommerceInterfaceElement > ( null ) ;
52
+ const interfaceRef =
53
+ useRef < React . ElementRef < typeof AtomicCommerceInterface > > ( null ) ;
49
54
let initialization : Promise < void > | null = null ;
50
55
51
56
useEffect ( ( ) => {
52
57
const commerceInterfaceAtomic = interfaceRef . current ! ;
53
58
if ( ! initialization ) {
54
- initialization = commerceInterfaceAtomic . initializeWithEngine ( engine ) ;
55
- initialization . then ( ( ) => {
56
- localization ( commerceInterfaceAtomic . i18n ) ;
57
- onReady (
58
- commerceInterfaceAtomic . executeFirstRequest . bind (
59
- commerceInterfaceAtomic
60
- )
61
- ) ;
62
- } ) ;
59
+ const waitForElement = async ( ) => {
60
+ await customElements . whenDefined ( 'atomic-commerce-interface' ) ;
61
+ initialization = commerceInterfaceAtomic . initializeWithEngine ( engine ) ;
62
+ initialization . then ( ( ) => {
63
+ localization ( commerceInterfaceAtomic . i18n ) ;
64
+ onReady (
65
+ commerceInterfaceAtomic . executeFirstRequest . bind (
66
+ commerceInterfaceAtomic
67
+ )
68
+ ) ;
69
+ } ) ;
70
+ } ;
71
+ waitForElement ( ) ;
63
72
}
64
73
} , [ interfaceRef ] ) ;
65
74
0 commit comments