/** * @deprecated This component is deprecated and will be removed in a future release. * Please use the hooks from 'src/features/checkout-v2/hooks' instead. * See README.md in this directory for migration guide. */ import React, { useCallback, useEffect, useState } from 'react' import { forwardRef, useImperativeHandle } from 'react' import { checkoutOrder, fetchCart, fetchCountries, fetchStates, fetchUserProfile, registerNewUser, } from '../../api/ApiClient' import { ICartResponse, ICheckoutBody, ICountriesResponse, IFetchAccessTokenResponse, IRegisterNewUserResponse, IStatesResponse, IUserProfileResponse, } from '../../api/types' import { refreshAccessToken as refreshAccessTokenAsync } from '../../helpers/RefreshAccessToken' import { BillingCoreHandle, IBillingCoreProps, ICheckoutResponse, } from './BillingCoreTypes' import BackgroundTimer from 'react-native-background-timer' /** * @deprecated Use the hooks from 'src/features/checkout-v2/hooks' instead */ const BillingCore = forwardRef((props, ref) => { const [secondsLeft, setSecondsLeft] = useState(420) const [timerOn, setTimerOn] = useState(false) const handleStopTimer = useCallback(() => { BackgroundTimer.stop() BackgroundTimer.stopBackgroundTimer() }, []) const handleStartTimer = useCallback(() => { BackgroundTimer.runBackgroundTimer(() => { setSecondsLeft((secs) => { if (secs > 0) { return secs - 1 } else { return 0 } }) }, 1000) }, []) const handleTimeIsUp = () => { BackgroundTimer.stopBackgroundTimer() props.onCartExpired?.() } useEffect(() => { if (secondsLeft === 0) { handleTimeIsUp() } props.onSecondsLeftChange?.(secondsLeft) }, [secondsLeft]) useEffect(() => { if (timerOn) { handleStartTimer() } else { BackgroundTimer.stopBackgroundTimer() } return () => { BackgroundTimer.stopBackgroundTimer() } }, [handleStartTimer, timerOn]) useImperativeHandle(ref, () => ({ async checkoutOrder(body: ICheckoutBody): Promise { const checkout = await checkoutOrder(body) if (!checkout.error) { BackgroundTimer.stopBackgroundTimer() } return checkout }, async getCart(): Promise { const cart = await fetchCart() if (cart.cartData?.expiresAt) { setSecondsLeft(cart.cartData.expiresAt) setTimerOn(true) } return cart }, async getCountries(): Promise { return await fetchCountries() }, async getStates(countryId: string): Promise { return await fetchStates(countryId) }, async getUserProfile(): Promise { return await fetchUserProfile() }, async registerNewUser(data: FormData): Promise { return await registerNewUser(data) }, async refreshAccessToken(refreshToken?: string): Promise { return await refreshAccessTokenAsync(refreshToken) }, stopCartTimer() { return handleStopTimer() }, })) return <>{props.children} }) export default BillingCore