Skip to content

Commit

Permalink
Merge pull request #43966 from rushatgabhane/traveldot
Browse files Browse the repository at this point in the history
[VIP][Travel] Open travelDot after accepting terms and conditions
  • Loading branch information
danieldoglas authored Jun 25, 2024
2 parents 9e510d3 + beb9744 commit 1b87b13
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 18 deletions.
4 changes: 4 additions & 0 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ const ONYXKEYS = {
/** The NVP with the last action taken (for the Quick Action Button) */
NVP_QUICK_ACTION_GLOBAL_CREATE: 'nvp_quickActionGlobalCreate',

/** The NVP containing all information necessary to connect with Spontana */
NVP_TRAVEL_SETTINGS: 'nvp_travelSettings',

/** The start date (yyyy-MM-dd HH:mm:ss) of the workspace owner’s free trial period. */
NVP_FIRST_DAY_FREE_TRIAL: 'nvp_private_firstDayFreeTrial',

Expand Down Expand Up @@ -722,6 +725,7 @@ type OnyxValuesMapping = {
[ONYXKEYS.CACHED_PDF_PATHS]: Record<string, string>;
[ONYXKEYS.POLICY_OWNERSHIP_CHANGE_CHECKS]: Record<string, OnyxTypes.PolicyOwnershipChangeChecks>;
[ONYXKEYS.NVP_QUICK_ACTION_GLOBAL_CREATE]: OnyxTypes.QuickAction;
[ONYXKEYS.NVP_TRAVEL_SETTINGS]: OnyxTypes.TravelSettings;
[ONYXKEYS.REVIEW_DUPLICATES]: OnyxTypes.ReviewDuplicates;
[ONYXKEYS.NVP_FIRST_DAY_FREE_TRIAL]: string;
[ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL]: string;
Expand Down
2 changes: 2 additions & 0 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ const SIDE_EFFECT_REQUEST_COMMANDS = {
JOIN_POLICY_VIA_INVITE_LINK: 'JoinWorkspaceViaInviteLink',
RECONNECT_APP: 'ReconnectApp',
GENERATE_SPOTNANA_TOKEN: 'GenerateSpotnanaToken',
ACCEPT_SPOTNANA_TERMS: 'AcceptSpotnanaTerms',
} as const;

type SideEffectRequestCommand = ValueOf<typeof SIDE_EFFECT_REQUEST_COMMANDS>;
Expand All @@ -577,6 +578,7 @@ type SideEffectRequestCommandParameters = {
[SIDE_EFFECT_REQUEST_COMMANDS.JOIN_POLICY_VIA_INVITE_LINK]: Parameters.JoinPolicyInviteLinkParams;
[SIDE_EFFECT_REQUEST_COMMANDS.RECONNECT_APP]: Parameters.ReconnectAppParams;
[SIDE_EFFECT_REQUEST_COMMANDS.GENERATE_SPOTNANA_TOKEN]: Parameters.GenerateSpotnanaTokenParams;
[SIDE_EFFECT_REQUEST_COMMANDS.ACCEPT_SPOTNANA_TERMS]: EmptyObject;
};

type ApiRequestCommandParameters = WriteCommandParameters & ReadCommandParameters & SideEffectRequestCommandParameters;
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/Link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,4 @@ function openLink(href: string, environmentURL: string, isAttachment = false) {
openExternalLink(href);
}

export {buildOldDotURL, openOldDotLink, openExternalLink, openLink, getInternalNewExpensifyPath, getInternalExpensifyPath, openTravelDotLink};
export {buildOldDotURL, openOldDotLink, openExternalLink, openLink, getInternalNewExpensifyPath, getInternalExpensifyPath, openTravelDotLink, buildTravelDotURL};
20 changes: 13 additions & 7 deletions src/libs/actions/Travel.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import type {OnyxUpdate} from 'react-native-onyx';
import * as API from '@libs/API';
import {WRITE_COMMANDS} from '@libs/API/types';
import {SIDE_EFFECT_REQUEST_COMMANDS} from '@libs/API/types';
import asyncOpenURL from '@libs/asyncOpenURL';
import ONYXKEYS from '@src/ONYXKEYS';
import {buildTravelDotURL} from './Link';

/**
* Accept Spotnana terms and conditions to receive a proper token used for authenticating further actions
*/
function acceptSpotnanaTerms() {
const successData: OnyxUpdate[] = [
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: 'merge',
key: ONYXKEYS.ACCOUNT,
key: ONYXKEYS.NVP_TRAVEL_SETTINGS,
value: {
travelSettings: {
hasAcceptedTerms: true,
},
hasAcceptedTerms: true,
},
},
];

API.write(WRITE_COMMANDS.ACCEPT_SPOTNANA_TERMS, {}, {successData});
asyncOpenURL(
// eslint-disable-next-line rulesdir/no-api-side-effects-method
API.makeRequestWithSideEffects(SIDE_EFFECT_REQUEST_COMMANDS.ACCEPT_SPOTNANA_TERMS, {}, {optimisticData})
.then((response) => (response?.spotnanaToken ? buildTravelDotURL(response.spotnanaToken) : buildTravelDotURL()))
.catch(() => buildTravelDotURL()),
(travelDotURL) => travelDotURL,
);
}

// eslint-disable-next-line import/prefer-default-export
Expand Down
13 changes: 12 additions & 1 deletion src/pages/Travel/ManageTrips.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import {Linking, View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import type {FeatureListItem} from '@components/FeatureList';
import FeatureList from '@components/FeatureList';
import * as Illustrations from '@components/Icon/Illustrations';
Expand All @@ -9,7 +10,9 @@ import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import Navigation from '@libs/Navigation/Navigation';
import colors from '@styles/theme/colors';
import * as Link from '@userActions/Link';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';

const tripsFeatures: FeatureListItem[] = [
Expand All @@ -27,6 +30,10 @@ function ManageTrips() {
const styles = useThemeStyles();
const {isSmallScreenWidth} = useWindowDimensions();
const {translate} = useLocalize();
const [travelSettings] = useOnyx(ONYXKEYS.NVP_TRAVEL_SETTINGS);
const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID);

const hasAcceptedTravelTerms = travelSettings?.hasAcceptedTerms;

const navigateToBookTravelDemo = () => {
Linking.openURL(CONST.BOOK_TRAVEL_DEMO_URL);
Expand All @@ -42,7 +49,11 @@ function ManageTrips() {
ctaText={translate('travel.bookTravel')}
ctaAccessibilityLabel={translate('travel.bookTravel')}
onCtaPress={() => {
Navigation.navigate(ROUTES.TRAVEL_TCS);
if (!hasAcceptedTravelTerms) {
Navigation.navigate(ROUTES.TRAVEL_TCS);
return;
}
Link.openTravelDotLink(activePolicyID);
}}
illustration={Illustrations.EmptyStateTravel}
illustrationStyle={[styles.mv4, styles.tripIllustrationSize]}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Travel/TravelTerms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function TravelTerms() {

Travel.acceptSpotnanaTerms();
setError(false);
Navigation.resetToHome();
Navigation.goBack();
}}
message={errorMessage}
isAlertVisible={error || !!errorMessage}
Expand Down
4 changes: 0 additions & 4 deletions src/types/onyx/Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type {ValueOf} from 'type-fest';
import type CONST from '@src/CONST';
import type DismissedReferralBanners from './DismissedReferralBanners';
import type * as OnyxCommon from './OnyxCommon';
import type {TravelSettings} from './TravelSettings';

/** Two factor authentication steps */
type TwoFactorAuthStep = ValueOf<typeof CONST.TWO_FACTOR_AUTH_STEPS> | '';
Expand Down Expand Up @@ -72,9 +71,6 @@ type Account = {
/** Referral banners that the user dismissed */
dismissedReferralBanners?: DismissedReferralBanners;

/** Object containing all account information necessary to connect with Spontana */
travelSettings?: TravelSettings;

/** Indicates whether the user is an approved accountant */
isApprovedAccountant?: boolean;

Expand Down
2 changes: 2 additions & 0 deletions src/types/onyx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import type Task from './Task';
import type Transaction from './Transaction';
import type {TransactionViolation, ViolationName} from './TransactionViolation';
import type TransactionViolations from './TransactionViolation';
import type {TravelSettings} from './TravelSettings';
import type User from './User';
import type UserLocation from './UserLocation';
import type UserMetadata from './UserMetadata';
Expand Down Expand Up @@ -162,6 +163,7 @@ export type {
Transaction,
TransactionViolation,
TransactionViolations,
TravelSettings,
User,
UserLocation,
UserMetadata,
Expand Down
4 changes: 0 additions & 4 deletions tests/utils/collections/userAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ function getValidAccount(credentialLogin = ''): Account {
primaryLogin: credentialLogin,
isLoading: false,
requiresTwoFactorAuth: false,
travelSettings: {
accountIDs: {},
hasAcceptedTerms: false,
},
};
}

Expand Down

0 comments on commit 1b87b13

Please sign in to comment.