-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NO CHANGELOG][Checkout Widget] Handle widget event (#2109)
Co-authored-by: Jhonatan Gonzalez <[email protected]>
- Loading branch information
1 parent
19566cb
commit bcdd87e
Showing
3 changed files
with
126 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/checkout/widgets-lib/src/widgets/checkout/hooks/useCheckoutEventsRelayer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { useContext, useEffect, useRef } from 'react'; | ||
import { | ||
CheckoutConnectSuccessEvent, | ||
CheckoutEventType, | ||
CheckoutFlowType, | ||
CheckoutSuccessEvent, | ||
IMTBLWidgetEvents, | ||
PostMessageHandlerEventType, | ||
WidgetEventData, | ||
WidgetType, | ||
} from '@imtbl/checkout-sdk'; | ||
import { useCheckoutContext } from '../context/CheckoutContextProvider'; | ||
import { sendCheckoutEvent } from '../CheckoutWidgetEvents'; | ||
import { EventTargetContext } from '../../../context/event-target-context/EventTargetContext'; | ||
import { CheckoutActions } from '../context/CheckoutContext'; | ||
|
||
function isWidgetEvent(payload: any): payload is { | ||
type: IMTBLWidgetEvents.IMTBL_CHECKOUT_WIDGET_EVENT; | ||
detail: { | ||
type: CheckoutEventType; | ||
data: WidgetEventData[WidgetType.CHECKOUT][keyof WidgetEventData[WidgetType.CHECKOUT]]; | ||
}; | ||
} { | ||
return payload.type === IMTBLWidgetEvents.IMTBL_CHECKOUT_WIDGET_EVENT; | ||
} | ||
|
||
export function useCheckoutEventsRelayer() { | ||
const [{ postMessageHandler, provider }, checkoutDispatch] = useCheckoutContext(); | ||
const { eventTargetState: { eventTarget } } = useContext(EventTargetContext); | ||
const unsubscribePostMessageHandler = useRef<(() => void) | undefined>(); | ||
|
||
useEffect(() => { | ||
if (!postMessageHandler) return undefined; | ||
unsubscribePostMessageHandler.current?.(); | ||
|
||
unsubscribePostMessageHandler.current = postMessageHandler.subscribe(({ type, payload }) => { | ||
if (type !== PostMessageHandlerEventType.WIDGET_EVENT || !isWidgetEvent(payload)) return; | ||
|
||
if (payload.detail.type === CheckoutEventType.SUCCESS | ||
&& (payload.detail.data as CheckoutSuccessEvent).flow === CheckoutFlowType.CONNECT) { | ||
const checkoutConnectSuccessEvent = payload.detail as unknown as CheckoutConnectSuccessEvent; | ||
if (!provider) { | ||
throw new Error('Provider not found, unable to send checkout connect success event'); | ||
} | ||
checkoutConnectSuccessEvent.data.provider = provider; | ||
sendCheckoutEvent(eventTarget, { type: payload.detail.type, data: checkoutConnectSuccessEvent }); | ||
return; | ||
} | ||
|
||
if (payload.detail.type === CheckoutEventType.DISCONNECTED) { | ||
checkoutDispatch({ | ||
payload: { | ||
type: CheckoutActions.SET_PROVIDER, | ||
provider: undefined, | ||
}, | ||
}); | ||
} | ||
|
||
sendCheckoutEvent(eventTarget, payload.detail); | ||
|
||
if (payload.detail.type === CheckoutEventType.INITIALISED) { | ||
checkoutDispatch({ | ||
payload: { | ||
type: CheckoutActions.SET_INITIALISED, | ||
initialised: true, | ||
}, | ||
}); | ||
} | ||
}); | ||
|
||
return () => { | ||
unsubscribePostMessageHandler.current?.(); | ||
}; | ||
}, [postMessageHandler, checkoutDispatch, eventTarget, provider]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters