-
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.
chore: Improved the logic using a common hook to avoid boilerplate an…
…d code duplication
- Loading branch information
Showing
4 changed files
with
62 additions
and
57 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
ts/features/payments/common/hooks/usePaymentsBackoffRetry.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,45 @@ | ||
import { useIOToast } from "@pagopa/io-app-design-system"; | ||
import { useIODispatch, useIOSelector } from "../../../../store/hooks"; | ||
import { paymentsBackoffRetrySelector } from "../store/selectors"; | ||
import { PaymentsBackoffRetry } from "../types/PaymentsBackoffRetry"; | ||
import I18n from "../../../../i18n"; | ||
import { | ||
getTimeRemainingText, | ||
isBackoffRetryTimeElapsed | ||
} from "../utils/backoffRetry"; | ||
import { increasePaymentsBackoffRetry } from "../store/actions"; | ||
|
||
export const usePaymentsBackoffRetry = (id: PaymentsBackoffRetry) => { | ||
const toast = useIOToast(); | ||
const dispatch = useIODispatch(); | ||
const backoff = useIOSelector(paymentsBackoffRetrySelector(id)); | ||
|
||
/** | ||
* This function check if the user can retry the request meaning that the exponential backoff time is elapsed | ||
* If the time is not elapsed, by default a toast error will be shown | ||
* @param showToast If true it shows automatically a toast with the time remaining to retry | ||
* @returns true if the request can be retried, otherwise false | ||
*/ | ||
const canRetryRequest = (showToast: boolean = true) => { | ||
if ( | ||
backoff?.allowedRetryTimestamp && | ||
!isBackoffRetryTimeElapsed(backoff?.allowedRetryTimestamp) | ||
) { | ||
if (showToast) { | ||
toast.error( | ||
I18n.t("features.payments.backoff.retryCountDown", { | ||
time: getTimeRemainingText(backoff?.allowedRetryTimestamp) | ||
}) | ||
); | ||
} | ||
return false; | ||
} | ||
dispatch(increasePaymentsBackoffRetry(id)); | ||
return true; | ||
}; | ||
|
||
return { | ||
backoff, | ||
canRetryRequest | ||
}; | ||
}; |
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