Skip to content
Closed
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 @@ -49,7 +49,7 @@
}

override func supportedEvents() -> [String]! {
return ["close", "completed", "error", "pixel"]
return ["close", "completed", "error", "pixel", "linkClick"]
}

override func startObserving() {
Expand All @@ -66,6 +66,13 @@
}
}

func checkoutDidClickLink(url: URL) {

Check failure on line 69 in modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift

View workflow job for this annotation

GitHub Actions / Lint Swift code

Indentation Width Violation: Code should be unindented by multiples of one tab or multiples of 4 spaces (indentation_width)
if hasListeners {

Check failure on line 70 in modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift

View workflow job for this annotation

GitHub Actions / Lint Swift code

Indentation Width Violation: Code should be indented using one tab or 4 spaces (indentation_width)
let event: [String: String] = ["url": url.absoluteString]

Check failure on line 71 in modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift

View workflow job for this annotation

GitHub Actions / Lint Swift code

Indentation Width Violation: Code should be indented using one tab or 4 spaces (indentation_width)
self.sendEvent(withName: "linkClick", body: encodeToJSON(from: event))
}
}

func shouldRecoverFromError(error: CheckoutError) -> Bool {
return error.isRecoverable
}
Expand Down
4 changes: 4 additions & 0 deletions modules/@shopify/checkout-sheet-kit/src/events.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,7 @@ namespace CheckoutCompletedEvent {
export interface CheckoutCompletedEvent {
orderDetails: CheckoutCompletedEvent.OrderDetails;
}

export interface CheckoutLinkClickEvent {
url: string;
}
12 changes: 11 additions & 1 deletion modules/@shopify/checkout-sheet-kit/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SO

import type {EmitterSubscription} from 'react-native';
import type {PixelEvent} from './pixels';
import type {CheckoutCompletedEvent} from './events';
import type {CheckoutCompletedEvent, CheckoutLinkClickEvent} from './events';
import type {CheckoutException} from './errors';

export type Maybe<T> = T | undefined;
Expand Down Expand Up @@ -144,6 +144,7 @@ export type CheckoutEvent =
| 'completed'
| 'error'
| 'geolocationRequest'
| 'linkClick'
| 'pixel';

export interface GeolocationRequestEvent {
Expand All @@ -159,11 +160,15 @@ export type CheckoutExceptionCallback = (error: CheckoutException) => void;
export type CheckoutCompletedEventCallback = (
event: CheckoutCompletedEvent,
) => void;
export type CheckoutLinkClickEventCallback = (
event: CheckoutLinkClickEvent,
) => void;

export type CheckoutEventCallback =
| CloseEventCallback
| CheckoutExceptionCallback
| CheckoutCompletedEventCallback
| CheckoutLinkClickEventCallback
| GeolocationRequestEventCallback
| PixelEventCallback;

Expand All @@ -177,6 +182,11 @@ function addEventListener(
callback: CheckoutCompletedEventCallback,
): Maybe<EmitterSubscription>;

function addEventListener(
event: 'linkClick',
callback: CheckoutLinkClickEventCallback,
): Maybe<EmitterSubscription>;

function addEventListener(
event: 'error',
callback: CheckoutExceptionCallback,
Expand Down
23 changes: 22 additions & 1 deletion modules/@shopify/checkout-sheet-kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
NativeEventEmitter,
PermissionsAndroid,
Platform,
Linking,
} from 'react-native';
import type {
EmitterSubscription,
Expand Down Expand Up @@ -54,7 +55,7 @@ import {
GenericError,
} from './errors.d';
import {CheckoutErrorCode} from './errors.d';
import type {CheckoutCompletedEvent} from './events.d';
import type {CheckoutCompletedEvent, CheckoutLinkClickEvent} from './events.d';
import type {CustomEvent, PixelEvent, StandardEvent} from './pixels.d';

const RNShopifyCheckoutSheetKit = NativeModules.ShopifyCheckoutSheetKit;
Expand All @@ -76,6 +77,7 @@ class ShopifyCheckoutSheet implements ShopifyCheckoutSheetKit {
);

private features: Features;
private linkClickCallback: Maybe<EventSubscription>;
private geolocationCallback: Maybe<EventSubscription>;

/**
Expand All @@ -99,6 +101,8 @@ class ShopifyCheckoutSheet implements ShopifyCheckoutSheetKit {
) {
this.subscribeToGeolocationRequestPrompts();
}

this.subscribeToLinkClicks();
}

public readonly version: string = RNShopifyCheckoutSheetKit.version;
Expand Down Expand Up @@ -172,6 +176,9 @@ class ShopifyCheckoutSheet implements ShopifyCheckoutSheetKit {
case 'completed':
eventCallback = this.interceptEventEmission('completed', callback);
break;
case 'linkClick':
eventCallback = this.interceptEventEmission('linkClick', callback);
break;
case 'error':
eventCallback = this.interceptEventEmission(
'error',
Expand Down Expand Up @@ -243,6 +250,20 @@ class ShopifyCheckoutSheet implements ShopifyCheckoutSheetKit {
);
}

private subscribeToLinkClicks() {
this.linkClickCallback = this.addEventListener(
'linkClick',
async (event: CheckoutLinkClickEvent) => {
const {url} = event;
const canOpenUrl = await Linking.canOpenURL(url);

if (canOpenUrl) {
await Linking.openURL(url);
}
},
);
}

/**
* Requests geolocation permissions on Android
* @returns Promise<boolean> indicating if permission was granted
Expand Down
Loading