-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into IOCOM-868-message-header
- Loading branch information
Showing
10 changed files
with
205 additions
and
69 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
ts/features/walletV3/payment/hooks/useOnTransactionActivationEffect.tsx
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,65 @@ | ||
import React from "react"; | ||
import { TransactionInfo } from "../../../../../definitions/pagopa/ecommerce/TransactionInfo"; | ||
import { TransactionStatusEnum } from "../../../../../definitions/pagopa/ecommerce/TransactionStatus"; | ||
import { useIODispatch, useIOSelector } from "../../../../store/hooks"; | ||
import { getGenericError } from "../../../../utils/errors"; | ||
import { walletPaymentGetTransactionInfo } from "../store/actions/networking"; | ||
import { walletPaymentTransactionSelector } from "../store/selectors"; | ||
|
||
const INITIAL_DELAY = 250; | ||
const MAX_TRIES = 3; | ||
|
||
type EffectCallback = ( | ||
transaction: TransactionInfo | ||
) => void | (() => void | undefined); | ||
|
||
/** | ||
* This custom hook manages the transition of a transaction's status from ACTIVATION_REQUESTED to ACTIVATED. | ||
* It employs a polling mechanism to continuously check the status, and once the status becomes ACTIVATED, | ||
* the specified effect is triggered. | ||
* @param effect Function to be executed upon transaction activation | ||
*/ | ||
const useOnTransactionActivationEffect = (effect: EffectCallback) => { | ||
const dispatch = useIODispatch(); | ||
const transactionPot = useIOSelector(walletPaymentTransactionSelector); | ||
|
||
const delayRef = React.useRef(INITIAL_DELAY); | ||
const countRef = React.useRef(0); | ||
|
||
/* eslint-disable functional/immutable-data */ | ||
React.useEffect(() => { | ||
if (transactionPot.kind === "PotSome") { | ||
const { transactionId, status } = transactionPot.value; | ||
|
||
if (status === TransactionStatusEnum.ACTIVATED) { | ||
// Execute the effect function when the transaction is activated | ||
delayRef.current = INITIAL_DELAY; | ||
countRef.current = 0; | ||
return effect(transactionPot.value); | ||
} else if (countRef.current > MAX_TRIES) { | ||
// The transaction is not yet ACTIVATED, and we exceeded the max retries | ||
dispatch( | ||
walletPaymentGetTransactionInfo.failure( | ||
getGenericError(new Error("Max try reached")) | ||
) | ||
); | ||
return; | ||
} else { | ||
// The transaction is not yet ACTIVATED, continue polling for transaction status with a timeout | ||
const timeout = setTimeout(() => { | ||
delayRef.current *= 2; | ||
countRef.current += 1; | ||
dispatch(walletPaymentGetTransactionInfo.request({ transactionId })); | ||
}, delayRef.current); | ||
// Clean up the timeout to avoid memory leaks | ||
return () => { | ||
clearTimeout(timeout); | ||
}; | ||
} | ||
} | ||
|
||
return undefined; | ||
}, [dispatch, transactionPot, effect]); | ||
}; | ||
|
||
export { useOnTransactionActivationEffect }; |
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
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
65 changes: 65 additions & 0 deletions
65
ts/features/walletV3/payment/saga/networking/handleWalletPaymentGetTransactionInfo.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,65 @@ | ||
import * as E from "fp-ts/lib/Either"; | ||
import { pipe } from "fp-ts/lib/function"; | ||
import { call, put } from "typed-redux-saga/macro"; | ||
import { ActionType } from "typesafe-actions"; | ||
import { SagaCallReturnType } from "../../../../../types/utils"; | ||
import { getGenericError, getNetworkError } from "../../../../../utils/errors"; | ||
import { readablePrivacyReport } from "../../../../../utils/reporters"; | ||
import { withRefreshApiCall } from "../../../../fastLogin/saga/utils"; | ||
import { PaymentClient } from "../../api/client"; | ||
import { walletPaymentGetTransactionInfo } from "../../store/actions/networking"; | ||
import { getOrFetchWalletSessionToken } from "./handleWalletPaymentNewSessionToken"; | ||
|
||
export function* handleWalletPaymentGetTransactionInfo( | ||
getTransactionInfo: PaymentClient["getTransactionInfo"], | ||
action: ActionType<(typeof walletPaymentGetTransactionInfo)["request"]> | ||
) { | ||
const sessionToken = yield* getOrFetchWalletSessionToken(); | ||
|
||
if (sessionToken === undefined) { | ||
yield* put( | ||
walletPaymentGetTransactionInfo.failure({ | ||
...getGenericError(new Error(`Missing session token`)) | ||
}) | ||
); | ||
return; | ||
} | ||
|
||
const getTransactionInfoRequest = getTransactionInfo({ | ||
eCommerceSessionToken: sessionToken, | ||
transactionId: action.payload.transactionId | ||
}); | ||
|
||
try { | ||
const getTransactionInfoResult = (yield* call( | ||
withRefreshApiCall, | ||
getTransactionInfoRequest, | ||
action | ||
)) as SagaCallReturnType<typeof getTransactionInfo>; | ||
|
||
yield* put( | ||
pipe( | ||
getTransactionInfoResult, | ||
E.fold( | ||
error => | ||
walletPaymentGetTransactionInfo.failure({ | ||
...getGenericError(new Error(readablePrivacyReport(error))) | ||
}), | ||
({ status, value }) => { | ||
if (status === 200) { | ||
return walletPaymentGetTransactionInfo.success(value); | ||
} else { | ||
return walletPaymentGetTransactionInfo.failure({ | ||
...getGenericError(new Error(JSON.stringify(value))) | ||
}); | ||
} | ||
} | ||
) | ||
) | ||
); | ||
} catch (e) { | ||
yield* put( | ||
walletPaymentGetTransactionInfo.failure({ ...getNetworkError(e) }) | ||
); | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.