import React, {FC, useState, useEffect} from 'react'; import {Modal} from 'react-native'; import NetInfo from '@react-native-community/netinfo'; import SuccessIcon from '@assets/svg/Success'; import ErrorIcon from '@assets/svg/Error'; import { Overlay, Wrapper, Description, ButtonWrapper, SubTitle, CloseButton, RetryButton, } from '@styles/components/error-modal'; interface IProps { type?: 'success' | 'error'; description: string | undefined; hideModal: () => void; visible?: boolean; retry: () => void; } const ErrorModal: FC = ({ type, description, visible, hideModal, retry, }) => { const [tryAgain, setTryAgain] = useState(); const [count, setCount] = useState(0); const [isOffline, setOfflineStatus] = useState(false); useEffect(() => { const removeNetInfoSubscription = NetInfo.addEventListener(state => { const offline = !(state.isConnected && state.isInternetReachable); setOfflineStatus(offline); setTryAgain(undefined); setCount(0); }); return () => removeNetInfoSubscription(); }, [isOffline]); const handleRetry = () => { if (count < 2) { setCount(count + 1); } else { setTryAgain('Try again after sometime'); setCount(0); } retry(); }; return ( {type === 'success' ? : } {description} {type !== 'success' && ( <> {description?.includes('Network') && ( <> {tryAgain} {!tryAgain && ( { handleRetry(); hideModal(); }} /> )} )} )} ); }; export default ErrorModal;