Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add loading state to travel #47809

Merged
merged 4 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading