Skip to content

Commit ec8478b

Browse files
committed
fix: add useEffect to scorll to bottom when having error
1 parent fa4dfbe commit ec8478b

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

src/components/BookTravelButton.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import HybridAppModule from '@expensify/react-native-hybrid-app';
22
import {Str} from 'expensify-common';
33
import type {ReactElement} from 'react';
4-
import React, {useCallback, useContext, useState} from 'react';
4+
import React, {useCallback, useContext, useEffect, useState} from 'react';
55
import {useOnyx} from 'react-native-onyx';
66
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
77
import useLocalize from '@hooks/useLocalize';
@@ -33,6 +33,9 @@ type BookTravelButtonProps = {
3333

3434
/** Whether to render the error message below the button */
3535
shouldRenderErrorMessageBelowButton?: boolean;
36+
37+
/** Function to set the shouldScrollToBottom state */
38+
setShouldScrollToBottom?: (shouldScrollToBottom: boolean) => void;
3639
};
3740

3841
const navigateToAcceptTerms = (domain: string, isUserValidated?: boolean) => {
@@ -45,7 +48,7 @@ const navigateToAcceptTerms = (domain: string, isUserValidated?: boolean) => {
4548
Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHOD_VERIFY_ACCOUNT.getRoute(Navigation.getActiveRoute(), ROUTES.TRAVEL_TCS.getRoute(domain)));
4649
};
4750

48-
function BookTravelButton({text, shouldRenderErrorMessageBelowButton = false}: BookTravelButtonProps) {
51+
function BookTravelButton({text, shouldRenderErrorMessageBelowButton = false, setShouldScrollToBottom}: BookTravelButtonProps) {
4952
const styles = useThemeStyles();
5053
const StyleUtils = useStyleUtils();
5154
const {translate} = useLocalize();
@@ -74,6 +77,13 @@ function BookTravelButton({text, shouldRenderErrorMessageBelowButton = false}: B
7477
const hidePreventionModal = () => setPreventionModalVisibility(false);
7578
const hideVerificationModal = () => setVerificationModalVisibility(false);
7679

80+
useEffect(() => {
81+
if (!errorMessage) {
82+
return;
83+
}
84+
setShouldScrollToBottom?.(true);
85+
}, [errorMessage, setShouldScrollToBottom]);
86+
7787
const bookATrip = useCallback(() => {
7888
setErrorMessage('');
7989

src/pages/Travel/ManageTrips.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useEffect, useRef} from 'react';
1+
import React, {useEffect, useRef, useState} from 'react';
22
// eslint-disable-next-line no-restricted-imports
33
import type {ScrollView as RNScrollView} from 'react-native';
44
import {InteractionManager, Linking, View} from 'react-native';
@@ -30,6 +30,7 @@ function ManageTrips() {
3030
const styles = useThemeStyles();
3131
const {shouldUseNarrowLayout} = useResponsiveLayout();
3232
const {translate} = useLocalize();
33+
const [shouldScrollToBottom, setShouldScrollToBottom] = useState(false);
3334

3435
const navigateToBookTravelDemo = () => {
3536
Linking.openURL(CONST.BOOK_TRAVEL_DEMO_URL);
@@ -44,8 +45,12 @@ function ManageTrips() {
4445
};
4546

4647
useEffect(() => {
48+
if (!shouldScrollToBottom) {
49+
return;
50+
}
4751
scrollToBottom();
48-
}, []);
52+
setShouldScrollToBottom(false);
53+
}, [shouldScrollToBottom]);
4954

5055
return (
5156
<ScrollView
@@ -74,6 +79,7 @@ function ManageTrips() {
7479
<BookTravelButton
7580
text={translate('travel.bookTravel')}
7681
shouldRenderErrorMessageBelowButton
82+
setShouldScrollToBottom={setShouldScrollToBottom}
7783
/>
7884
</>
7985
}

0 commit comments

Comments
 (0)