diff --git a/src/components/Screens/RequestScreen.tsx b/src/components/Screens/RequestScreen.tsx index b5f2e0c..6b008fa 100644 --- a/src/components/Screens/RequestScreen.tsx +++ b/src/components/Screens/RequestScreen.tsx @@ -11,6 +11,7 @@ export type RequestScreenProps = { render: ( data: T | undefined, loading: boolean, + lastRefreshDate: Date | undefined, refreshData: (newRequest?: () => Promise) => void, status: REQUEST_STATUS, code?: REQUEST_CODES @@ -37,8 +38,15 @@ const MIN_REFRESH_TIME = 5 * 1000; export default function RequestScreen(props: Props) { const refreshInterval = useRef(); - const [loading, status, code, data, refreshData] = useRequestLogic( - () => props.request(), + const [ + loading, + lastRefreshDate, + status, + code, + data, + refreshData, + ] = useRequestLogic( + props.request, props.cache, props.onCacheUpdate, props.refreshOnFocus, @@ -81,16 +89,6 @@ export default function RequestScreen(props: Props) { }, [props.cache, props.refreshOnFocus]) ); - // useEffect(() => { - // if (status === REQUEST_STATUS.BAD_TOKEN && props.onMajorError) { - // props.onMajorError(status, code); - // } - // // eslint-disable-next-line react-hooks/exhaustive-deps - // }, [status, code]); - - // if (status === REQUEST_STATUS.BAD_TOKEN && props.onMajorError) { - // return ; - // } else if (data === undefined && loading && props.showLoading !== false) { return ; } else if ( @@ -110,6 +108,13 @@ export default function RequestScreen(props: Props) { /> ); } else { - return props.render(data, loading, refreshData, status, code); + return props.render( + data, + loading, + lastRefreshDate, + refreshData, + status, + code + ); } } diff --git a/src/components/Screens/WebSectionList.tsx b/src/components/Screens/WebSectionList.tsx index b36e835..5fe7717 100644 --- a/src/components/Screens/WebSectionList.tsx +++ b/src/components/Screens/WebSectionList.tsx @@ -62,6 +62,7 @@ type Props = Omit< createDataset: ( data: RawData | undefined, loading: boolean, + lastRefreshDate: Date | undefined, refreshData: (newRequest?: () => Promise) => void, status: REQUEST_STATUS, code?: REQUEST_CODES @@ -69,6 +70,7 @@ type Props = Omit< renderListHeaderComponent?: ( data: RawData | undefined, loading: boolean, + lastRefreshDate: Date | undefined, refreshData: (newRequest?: () => Promise) => void, status: REQUEST_STATUS, code?: REQUEST_CODES @@ -108,6 +110,7 @@ function WebSectionList(props: Props) { const render = ( data: RawData | undefined, loading: boolean, + lastRefreshDate: Date | undefined, refreshData: (newRequest?: () => Promise) => void, status: REQUEST_STATUS, code?: REQUEST_CODES @@ -116,6 +119,7 @@ function WebSectionList(props: Props) { const dataset = props.createDataset( data, loading, + lastRefreshDate, refreshData, status, code @@ -143,6 +147,7 @@ function WebSectionList(props: Props) { ? props.renderListHeaderComponent( data, loading, + lastRefreshDate, refreshData, status, code diff --git a/src/screens/Proxiwash/ProxiwashScreen.tsx b/src/screens/Proxiwash/ProxiwashScreen.tsx index abe8794..2934688 100644 --- a/src/screens/Proxiwash/ProxiwashScreen.tsx +++ b/src/screens/Proxiwash/ProxiwashScreen.tsx @@ -52,7 +52,6 @@ import GENERAL_STYLES from '../../constants/Styles'; import { readData } from '../../utils/WebData'; import { useFocusEffect, useNavigation } from '@react-navigation/core'; import { setupMachineNotification } from '../../utils/Notifications'; -import { REQUEST_STATUS } from '../../utils/Requests'; import ProximoListHeader from '../../components/Lists/Proximo/ProximoListHeader'; const REFRESH_TIME = 1000 * 10; // Refresh every 10 seconds @@ -110,8 +109,6 @@ function ProxiwashScreen() { ) as 'tripodeB' | 'washinsa' ); - const lastrefreshDate = useRef(undefined); - const modalStateStrings: { [key in MachineStates]: string } = { [MachineStates.AVAILABLE]: i18n.t('screens.proxiwash.modal.ready'), [MachineStates.RUNNING]: i18n.t('screens.proxiwash.modal.running'), @@ -418,21 +415,17 @@ function ProxiwashScreen() { }; const renderListHeaderComponent = ( - _data: FetchedDataType | undefined, - loading: boolean, - _refreshData: (newRequest?: () => Promise) => void, - status: REQUEST_STATUS + data: FetchedDataType | undefined, + _loading: boolean, + lastRefreshDate: Date | undefined ) => { - const success = status === REQUEST_STATUS.SUCCESS; - if (success && !loading) { - lastrefreshDate.current = new Date(); + if (data) { + return ( + + ); + } else { + return null; } - return ( - - ); }; let data: LaundromatType; diff --git a/src/utils/customHooks.tsx b/src/utils/customHooks.tsx index 30be168..67d5b51 100644 --- a/src/utils/customHooks.tsx +++ b/src/utils/customHooks.tsx @@ -55,24 +55,24 @@ export function useRequestLogic( ) { const [response, setResponse] = useState<{ loading: boolean; + lastRefreshDate?: Date; status: REQUEST_STATUS; code?: number; data: T | undefined; }>({ loading: startLoading !== false && cache === undefined, + lastRefreshDate: undefined, status: REQUEST_STATUS.SUCCESS, code: undefined, data: undefined, }); - const [lastRefreshDate, setLastRefreshDate] = useState( - undefined - ); const refreshData = (newRequest?: () => Promise) => { let canRefresh; - if (lastRefreshDate && minRefreshTime) { - const last = lastRefreshDate; - canRefresh = new Date().getTime() - last.getTime() > minRefreshTime; + if (response.lastRefreshDate && minRefreshTime) { + canRefresh = + new Date().getTime() - response.lastRefreshDate.getTime() > + minRefreshTime; } else { canRefresh = true; } @@ -83,12 +83,12 @@ export function useRequestLogic( loading: true, })); } - setLastRefreshDate(new Date()); const r = newRequest ? newRequest : request; r() .then((requestResponse: T) => { setResponse({ loading: false, + lastRefreshDate: new Date(), status: REQUEST_STATUS.SUCCESS, code: undefined, data: requestResponse, @@ -100,23 +100,25 @@ export function useRequestLogic( .catch(() => { setResponse((prevState) => ({ loading: false, + lastRefreshDate: prevState.lastRefreshDate, status: REQUEST_STATUS.CONNECTION_ERROR, code: 0, data: prevState.data, })); - setLastRefreshDate(undefined); }); } }; const value: [ boolean, + Date | undefined, REQUEST_STATUS, number | undefined, T | undefined, (newRequest?: () => Promise) => void ] = [ response.loading, + response.lastRefreshDate, response.status, response.code, cache ? cache : response.data,