Skip to content

Commit

Permalink
Merge pull request #47809 from rushatgabhane/travel-loading
Browse files Browse the repository at this point in the history
Add loading state to travel
  • Loading branch information
stitesExpensify authored Aug 27, 2024
2 parents e5b6cef + 751f9d9 commit 690338d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/libs/actions/Travel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,27 @@ function acceptSpotnanaTerms() {
key: ONYXKEYS.NVP_TRAVEL_SETTINGS,
value: {
hasAcceptedTerms: true,
isLoading: true,
},
},
];

const finallyData: OnyxUpdate[] = [
{
onyxMethod: 'merge',
key: ONYXKEYS.NVP_TRAVEL_SETTINGS,
value: {
isLoading: false,
},
},
];

const error = new Error('Failed to generate spotnana token.');

return new Promise((_, reject) => {
return new Promise((resolve, reject) => {
asyncOpenURL(
// eslint-disable-next-line rulesdir/no-api-side-effects-method
API.makeRequestWithSideEffects(SIDE_EFFECT_REQUEST_COMMANDS.ACCEPT_SPOTNANA_TERMS, null, {optimisticData})
API.makeRequestWithSideEffects(SIDE_EFFECT_REQUEST_COMMANDS.ACCEPT_SPOTNANA_TERMS, null, {optimisticData, finallyData})
.then((response) => {
if (!response?.spotnanaToken) {
reject(error);
Expand All @@ -35,7 +47,10 @@ function acceptSpotnanaTerms() {
reject(error);
throw error;
}),
(travelDotURL) => travelDotURL ?? '',
(travelDotURL) => {
resolve(travelDotURL);
return travelDotURL ?? '';
},
);
});
}
Expand Down
5 changes: 5 additions & 0 deletions src/pages/Travel/TravelTerms.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {useCallback, useEffect, useState} from 'react';
import {View} from 'react-native';
import {ScrollView} from 'react-native-gesture-handler';
import {useOnyx} from 'react-native-onyx';
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView';
import CheckboxWithLabel from '@components/CheckboxWithLabel';
import FormAlertWithSubmitButton from '@components/FormAlertWithSubmitButton';
Expand All @@ -15,13 +16,16 @@ import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import * as Travel from '@userActions/Travel';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';

function TravelTerms() {
const styles = useThemeStyles();
const {translate} = useLocalize();
const {canUseSpotnanaTravel} = usePermissions();
const [hasAcceptedTravelTerms, setHasAcceptedTravelTerms] = useState(false);
const [errorMessage, setErrorMessage] = useState('');
const [travelSettings] = useOnyx(ONYXKEYS.NVP_TRAVEL_SETTINGS);
const isLoading = travelSettings?.isLoading;

const toggleTravelTerms = () => {
setHasAcceptedTravelTerms(!hasAcceptedTravelTerms);
Expand Down Expand Up @@ -98,6 +102,7 @@ function TravelTerms() {
message={errorMessage}
isAlertVisible={!!errorMessage}
containerStyles={[styles.mh0, styles.mt5]}
isLoading={isLoading}
/>
</ScrollView>
)}
Expand Down
3 changes: 3 additions & 0 deletions src/types/onyx/TravelSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ type TravelSettings = {

/** Whether the user is setup for staging travelDot */
testAccount?: boolean;

/** Whether the user is waiting for the API response after accepting terms */
isLoading?: boolean;
};

/** Model of workspace travel information to connect with Spotnana */
Expand Down

0 comments on commit 690338d

Please sign in to comment.