Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SO

import React, {createContext, useContext, useRef, useCallback} from 'react';
import {UIManager, findNodeHandle} from 'react-native';
import type {CheckoutEventResponsePayload} from './events.d';

interface CheckoutEventContextType {
registerWebView: (webViewRef: React.RefObject<any>) => void;
unregisterWebView: () => void;
respondToEvent: (eventId: string, response: any) => Promise<boolean>;
respondToEvent: (
eventId: string,
response: CheckoutEventResponsePayload,
) => Promise<boolean>;
}

const CheckoutEventContext = createContext<CheckoutEventContextType | null>(
Expand Down Expand Up @@ -52,7 +56,10 @@ export const ShopifyCheckoutEventProvider = ({
}, []);

const respondToEvent = useCallback(
async (eventId: string, response: any): Promise<boolean> => {
async (
eventId: string,
response: CheckoutEventResponsePayload,
): Promise<boolean> => {
Comment on lines +59 to +62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this function need to be async? The contents look synchronous to me, unless the UIManager calls are async but we're not awaiting them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point - no longer needs async (previously did)
I'm used to eslint catching that I'm setting up another PR with typescript-eslint so I can add require-await to avoid this mistake in future

Image

if (!webViewRef.current?.current) {
return false;
}
Expand Down Expand Up @@ -112,7 +119,7 @@ export function useShopifyEvent(eventId: string) {
return {
id: eventId,
respondWith: useCallback(
async (response: any) => {
async (response: CheckoutEventResponsePayload) => {
if (!eventContext) {
return false;
}
Expand Down
9 changes: 9 additions & 0 deletions modules/@shopify/checkout-sheet-kit/src/events.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,3 +596,12 @@ export interface CheckoutSubmitStartResponsePayload {
*/
errors?: CheckoutResponseError[];
}

/**
* Union type of all checkout event response payloads.
* Use with ShopifyCheckoutEventProvider.respondToEvent() or useShopifyEvent().respondWith()
*/
export type CheckoutEventResponsePayload =
| CheckoutAddressChangeStartResponsePayload
| CheckoutPaymentMethodChangeStartResponsePayload
| CheckoutSubmitStartResponsePayload;
1 change: 1 addition & 0 deletions modules/@shopify/checkout-sheet-kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ export type {
CheckoutAddressChangeStartEvent,
CheckoutAddressChangeStartResponsePayload,
CheckoutCompleteEvent,
CheckoutEventResponsePayload,
CheckoutPaymentMethodChangeStartEvent,
CheckoutPaymentMethodChangeStartResponsePayload,
CheckoutResponseError,
Expand Down
Loading