Skip to content

Commit

Permalink
linting fixes for new files
Browse files Browse the repository at this point in the history
  • Loading branch information
Tharindu Kumarasiri committed Sep 5, 2024
1 parent 23ecf19 commit 3cc928f
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 223 deletions.
44 changes: 23 additions & 21 deletions src/hooks/useBackgroundHandler.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import React, { MutableRefObject, useEffect } from 'react'
import { View, Text, AppState, AppStateStatus } from 'react-native'
import { KomojuProviderIprops, PaymentStatuses, TokenResponseStatuses } from '../util/types';
import sessionShow from '../services/sessionShow';

import { MutableRefObject, useEffect } from "react";
import { AppState, AppStateStatus } from "react-native";
import {
KomojuProviderIprops,
PaymentStatuses,
TokenResponseStatuses,
} from "../util/types";
import sessionShow from "../services/sessionShow";

type Props = {
props: KomojuProviderIprops,
startLoading: () => void
stopLoading: () => void
sessionIdRef: MutableRefObject<string>
onCompleteCallback: MutableRefObject<null>
onPaymentSuccess: () => void
onPaymentAwaiting: () => void
onPaymentCancelled: () => void
onSessionExpired: () => void
onPaymentFailed: () => void
}
props: KomojuProviderIprops;
startLoading: () => void;
stopLoading: () => void;
sessionIdRef: MutableRefObject<string>;
onCompleteCallback: MutableRefObject<null>;
onPaymentSuccess: () => void;
onPaymentAwaiting: () => void;
onPaymentCancelled: () => void;
onSessionExpired: () => void;
onPaymentFailed: () => void;
};

const useBackgroundHandler = ({
props,
Expand All @@ -27,9 +30,8 @@ const useBackgroundHandler = ({
onPaymentCancelled,
onPaymentFailed,
onSessionExpired,
onPaymentSuccess
onPaymentSuccess,
}: Props) => {

useEffect(() => {
// Add event listener for deep links
const windowChangeListener = AppState.addEventListener(
Expand Down Expand Up @@ -78,7 +80,7 @@ const useBackgroundHandler = ({
sessionResponse?.status === PaymentStatuses.ERROR ||
sessionResponse?.payment?.status === PaymentStatuses.ERROR ||
sessionResponse?.secure_token?.verification_status ===
TokenResponseStatuses.ERROR
TokenResponseStatuses.ERROR
) {
onPaymentFailed();
}
Expand All @@ -89,6 +91,6 @@ const useBackgroundHandler = ({
};

return undefined;
}
};

export default useBackgroundHandler
export default useBackgroundHandler;
153 changes: 78 additions & 75 deletions src/hooks/useDeepLinkHandler.tsx
Original file line number Diff line number Diff line change
@@ -1,89 +1,92 @@
import { Linking } from 'react-native'
import { MutableRefObject, useEffect } from 'react'
import { KomojuProviderIprops, PaymentStatuses, TokenResponseStatuses } from '../util/types'
import sessionShow from '../services/sessionShow'
import { Linking } from "react-native";
import { MutableRefObject, useEffect } from "react";
import {
KomojuProviderIprops,
PaymentStatuses,
TokenResponseStatuses,
} from "../util/types";
import sessionShow from "../services/sessionShow";

type Props = {
props: KomojuProviderIprops,
startLoading: () => void
stopLoading: () => void
sessionIdRef: MutableRefObject<string>
onCompleteCallback: MutableRefObject<null>
onPaymentSuccess: () => void
onPaymentAwaiting: () => void
onPaymentCancelled: () => void
onPaymentFailed: () => void
}
props: KomojuProviderIprops;
startLoading: () => void;
stopLoading: () => void;
sessionIdRef: MutableRefObject<string>;
onCompleteCallback: MutableRefObject<null>;
onPaymentSuccess: () => void;
onPaymentAwaiting: () => void;
onPaymentCancelled: () => void;
onPaymentFailed: () => void;
};

const useDeepLinkHandler = ({
props,
startLoading,
stopLoading,
sessionIdRef,
onCompleteCallback,
onPaymentAwaiting,
onPaymentCancelled,
onPaymentFailed,
onPaymentSuccess
props,
startLoading,
stopLoading,
sessionIdRef,
onCompleteCallback,
onPaymentAwaiting,
onPaymentCancelled,
onPaymentFailed,
onPaymentSuccess,
}: Props) => {
useEffect(() => {
// Add event listener for deep links
const subscription = Linking.addEventListener(
"url",
handleDeepLinkStateChange
);

useEffect(() => {
// Add event listener for deep links
const subscription = Linking.addEventListener(
'url',
handleDeepLinkStateChange
);

return () => {
subscription.remove();
};
}, [props]);
return () => {
subscription.remove();
};
}, [props]);

const handleDeepLinkStateChange = async () => {
startLoading();
const handleDeepLinkStateChange = async () => {
startLoading();

// if this is a session flow, check until session response changes from 'pending' to 'completed' or 'error'
const sessionShowPayload = {
publishableKey: props.publishableKey,
sessionId: sessionIdRef.current,
};
// if this is a session flow, check until session response changes from 'pending' to 'completed' or 'error'
const sessionShowPayload = {
publishableKey: props.publishableKey,
sessionId: sessionIdRef.current,
};

// fetch session status to check if the payment is completed
let sessionResponse = await sessionShow(sessionShowPayload);
// fetch session status to check if the payment is completed
let sessionResponse = await sessionShow(sessionShowPayload);

// Polling until session verification status changes
while (
sessionResponse?.status === PaymentStatuses.PENDING &&
sessionResponse?.payment?.status !== PaymentStatuses.CANCELLED &&
sessionResponse?.secure_token?.verification_status !==
TokenResponseStatuses.ERROR
) {
sessionResponse = await sessionShow(sessionShowPayload);
}
// Polling until session verification status changes
while (
sessionResponse?.status === PaymentStatuses.PENDING &&
sessionResponse?.payment?.status !== PaymentStatuses.CANCELLED &&
sessionResponse?.secure_token?.verification_status !==
TokenResponseStatuses.ERROR
) {
sessionResponse = await sessionShow(sessionShowPayload);
}

// if payment success showing success screen or if failed showing error screen
if (sessionResponse?.status === PaymentStatuses.SUCCESS) {
if (sessionResponse?.payment?.status === TokenResponseStatuses.CAPTURED) {
onPaymentSuccess();
} else {
onPaymentAwaiting();
}
// calling user passed onComplete method with session response data
onCompleteCallback.current &&
// TODO: Fix this type error
// @ts-expect-error - Argument of type 'PaymentSessionResponse' is not assignable to parameter of type 'string'.
onCompleteCallback.current(sessionResponse);
} else if (sessionResponse?.payment?.status === PaymentStatuses.CANCELLED) {
onPaymentCancelled();
} else {
onPaymentFailed();
}
// if payment success showing success screen or if failed showing error screen
if (sessionResponse?.status === PaymentStatuses.SUCCESS) {
if (sessionResponse?.payment?.status === TokenResponseStatuses.CAPTURED) {
onPaymentSuccess();
} else {
onPaymentAwaiting();
}
// calling user passed onComplete method with session response data
onCompleteCallback.current &&
// TODO: Fix this type error
// @ts-expect-error - Argument of type 'PaymentSessionResponse' is not assignable to parameter of type 'string'.
onCompleteCallback.current(sessionResponse);
} else if (sessionResponse?.payment?.status === PaymentStatuses.CANCELLED) {
onPaymentCancelled();
} else {
onPaymentFailed();
}

// after all api calls are done stopping the loading indicator
stopLoading();
};
// after all api calls are done stopping the loading indicator
stopLoading();
};

return undefined;
}
return undefined;
};

export default useDeepLinkHandler
export default useDeepLinkHandler;
Loading

0 comments on commit 3cc928f

Please sign in to comment.