import { Loading, scaleUtils } from '@widergy/mobile-ui'; import React, { Fragment, useCallback, useEffect, useState } from 'react'; import { View } from 'react-native'; import { BluetoothState } from '../../../../bluetooth/constants'; import { useBluetoothContext } from '../../../../bluetooth/useBluetoothContext'; import Colors from '../../../Colors'; import GeneralStep from '../../../GeneralStep'; import ProbeReadingToRead from '../../../assets/probeReading/probeReadingToRead.png'; import MeterFamilyTypeBadge from '../MeterFamilyTypeBadge'; import { changeDatetime } from '../../../../bluetooth/writing/changeDatetime'; import styles from './styles'; const InteractionWithProbe = ({ translations, closeProbeReading, meterFamilyType, setReadingResult, onError, mockCode, probeCommand, loadingCurrentDevice, changeDateAndTime, }) => { const SIZE_LOADING = scaleUtils.moderateHorizontalScale(164); const [beginReading, setBeginReading] = useState(false); const bluetoothContext = useBluetoothContext(); const onBeginReading = useCallback(() => { setBeginReading(true); }, [setBeginReading]); const onSuccess = useCallback( arrayResult => { setReadingResult(arrayResult); }, [setReadingResult], ); const disabledStep = !bluetoothContext.isConnected() || bluetoothContext.batteryPercentage == null; useEffect(() => { if (!disabledStep) { const subscription = changeDatetime({ mockCode, bluetoothContext, onBeginReading, onSuccess, onError, probeCommand, }); return () => { subscription?.unsubscribe?.(); }; } return () => {}; // eslint-disable-next-line react-hooks/exhaustive-deps }, [disabledStep]); useEffect(() => { if ( bluetoothContext.connectionError || (bluetoothContext.bluetoothState === BluetoothState.IDLE && !loadingCurrentDevice && !bluetoothContext.targetDevice) ) onError(); }, [ bluetoothContext.bluetoothState, bluetoothContext.connectionError, onError, loadingCurrentDevice, bluetoothContext.targetDevice, ]); const title = changeDateAndTime ? translations.titleWithDate : translations.titleWithoutDate; return ( {!beginReading ? ( { closeProbeReading(); }, key: 'exit', secondary: true, disabled: disabledStep, }, ]} withOpacity={disabledStep} > ) : ( { closeProbeReading(); }, key: 'exit', secondary: true, }, ]} > )} ); }; export default InteractionWithProbe;