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

[TS migration] Migrate IOURequestStartPage, IOURequestRedirectToStartPage, IOURequestStepDistance and IOURequestStepAmount pages to TypeScript #39910

Merged
Show file tree
Hide file tree
Changes from 12 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
2 changes: 1 addition & 1 deletion src/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ const ROUTES = {
},
MONEY_REQUEST_STEP_WAYPOINT: {
route: ':action/:iouType/waypoint/:transactionID/:reportID/:pageIndex',
getRoute: (action: ValueOf<typeof CONST.IOU.ACTION>, iouType: ValueOf<typeof CONST.IOU.TYPE>, transactionID: string, reportID: string, pageIndex = '', backTo = '') =>
getRoute: (action: ValueOf<typeof CONST.IOU.ACTION>, iouType: ValueOf<typeof CONST.IOU.TYPE>, transactionID: string, reportID?: string, pageIndex = '', backTo = '') =>
getUrlWithBackToParam(`${action}/${iouType}/waypoint/${transactionID}/${reportID}/${pageIndex}`, backTo),
},
// This URL is used as a redirect to one of the create tabs below. This is so that we can message users with a link
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type DistanceRequestProps = {
onSecondaryInteraction?: () => void;

/** Function to get the index of the item */
getIndex?: () => number;
getIndex?: () => number | undefined;
kosmydel marked this conversation as resolved.
Show resolved Hide resolved

/** Whether the item is active */
isActive?: boolean;
Expand Down
5 changes: 3 additions & 2 deletions src/libs/Navigation/OnyxTabNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React from 'react';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import type {TabSelectorProps} from '@components/TabSelector/TabSelector';
import type {IOURequestType} from '@libs/actions/IOU';
import Tab from '@userActions/Tab';
import ONYXKEYS from '@src/ONYXKEYS';
import type {SelectedTabRequest} from '@src/types/onyx';
Expand All @@ -24,7 +25,7 @@ type OnyxTabNavigatorProps = OnyxTabNavigatorOnyxProps &
selectedTab?: SelectedTabRequest;

/** A function triggered when a tab has been selected */
onTabSelected?: (newIouType: string) => void;
onTabSelected?: (newIouType: IOURequestType) => void;

tabBar: (props: TabSelectorProps) => React.ReactNode;

Expand Down Expand Up @@ -52,7 +53,7 @@ function OnyxTabNavigator({id, selectedTab, children, onTabSelected = () => {},
const index = state.index;
const routeNames = state.routeNames;
Tab.setSelectedTab(id, routeNames[index] as SelectedTabRequest);
onTabSelected(routeNames[index]);
onTabSelected(routeNames[index] as IOURequestType);
},
...(screenListeners ?? {}),
}}
Expand Down
33 changes: 25 additions & 8 deletions src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
Route,
} from '@react-navigation/native';
import type {ValueOf} from 'type-fest';
import type {IOURequestType} from '@libs/actions/IOU';
import type CONST from '@src/CONST';
import type {Country} from '@src/CONST';
import type NAVIGATORS from '@src/NAVIGATORS';
Expand Down Expand Up @@ -331,13 +332,6 @@ type RoomInviteNavigatorParamList = {
};

type MoneyRequestNavigatorParamList = {
[SCREENS.MONEY_REQUEST.STEP_AMOUNT]: {
action: ValueOf<typeof CONST.IOU.ACTION>;
iouType: ValueOf<typeof CONST.IOU.TYPE>;
transactionID: string;
reportID: string;
backTo: string;
};
[SCREENS.MONEY_REQUEST.PARTICIPANTS]: {
iouType: string;
reportID: string;
Expand Down Expand Up @@ -422,7 +416,7 @@ type MoneyRequestNavigatorParamList = {
threadReportID: number;
};
[SCREENS.MONEY_REQUEST.STEP_DISTANCE]: {
action: string;
action: ValueOf<typeof CONST.IOU.ACTION>;
iouType: ValueOf<typeof CONST.IOU.TYPE>;
transactionID: string;
reportID: string;
Expand All @@ -432,6 +426,29 @@ type MoneyRequestNavigatorParamList = {
iouType: string;
reportID: string;
};
[SCREENS.MONEY_REQUEST.CREATE]: {
iouType: ValueOf<typeof CONST.IOU.TYPE>;
reportID: string;
transactionID: string;

// These are not used in the screen, but are needed for the navigation
// for IOURequestStepDistance and IOURequestStepAmount components
backTo: never;
action: never;
kosmydel marked this conversation as resolved.
Show resolved Hide resolved
};
[SCREENS.MONEY_REQUEST.START]: {
iouType: ValueOf<typeof CONST.IOU.TYPE>;
reportID: string;
transactionID: string;
iouRequestType: IOURequestType;
};
[SCREENS.MONEY_REQUEST.STEP_AMOUNT]: {
iouType: ValueOf<typeof CONST.IOU.TYPE>;
reportID: string;
transactionID: string;
backTo: Routes;
action: ValueOf<typeof CONST.IOU.ACTION>;
};
};

type NewTaskNavigatorParamList = {
Expand Down
7 changes: 4 additions & 3 deletions src/libs/TransactionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ONYXKEYS from '@src/ONYXKEYS';
import type {RecentWaypoint, Report, TaxRate, TaxRates, TaxRatesWithDefault, Transaction, TransactionViolation} from '@src/types/onyx';
import type {Comment, Receipt, TransactionChanges, TransactionPendingFieldsKey, Waypoint, WaypointCollection} from '@src/types/onyx/Transaction';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import type {IOURequestType} from './actions/IOU';
import {isCorporateCard, isExpensifyCard} from './CardUtils';
import DateUtils from './DateUtils';
import * as Localize from './Localize';
Expand Down Expand Up @@ -45,16 +46,16 @@ function isDistanceRequest(transaction: OnyxEntry<Transaction>): boolean {
return type === CONST.TRANSACTION.TYPE.CUSTOM_UNIT && customUnitName === CONST.CUSTOM_UNITS.NAME_DISTANCE;
}

function isScanRequest(transaction: Transaction): boolean {
function isScanRequest(transaction: OnyxEntry<Transaction>): boolean {
// This is used during the request creation flow before the transaction has been saved to the server
if (lodashHas(transaction, 'iouRequestType')) {
return transaction.iouRequestType === CONST.IOU.REQUEST_TYPE.SCAN;
return transaction?.iouRequestType === CONST.IOU.REQUEST_TYPE.SCAN;
}

return Boolean(transaction?.receipt?.source);
}

function getRequestType(transaction: Transaction): ValueOf<typeof CONST.IOU.REQUEST_TYPE> {
function getRequestType(transaction: OnyxEntry<Transaction>): IOURequestType {
if (isDistanceRequest(transaction)) {
return CONST.IOU.REQUEST_TYPE.DISTANCE;
}
Expand Down
64 changes: 46 additions & 18 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2172,15 +2172,24 @@ function updateMoneyRequestTaxRate(
API.write('UpdateMoneyRequestTaxRate', params, onyxData);
}

type UpdateMoneyRequestDistanceParams = {
transactionID: string;
transactionThreadReportID: string;
waypoints: WaypointCollection;
policy?: OnyxEntry<OnyxTypes.Policy>;
policyTagList?: OnyxEntry<OnyxTypes.PolicyTagList>;
policyCategories?: OnyxEntry<OnyxTypes.PolicyCategories>;
};

/** Updates the waypoints of a distance money request */
function updateMoneyRequestDistance(
transactionID: string,
transactionThreadReportID: string,
waypoints: WaypointCollection,
policy: OnyxEntry<OnyxTypes.Policy>,
policyTagList: OnyxEntry<OnyxTypes.PolicyTagList>,
policyCategories: OnyxEntry<OnyxTypes.PolicyCategories>,
) {
function updateMoneyRequestDistance({
transactionID,
transactionThreadReportID,
waypoints,
policy = {} as OnyxTypes.Policy,
policyTagList = {},
policyCategories = {},
Comment on lines +2194 to +2196
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid empty object default values, is this necessary?

}: UpdateMoneyRequestDistanceParams) {
const transactionChanges: TransactionChanges = {
waypoints,
};
Expand Down Expand Up @@ -3782,21 +3791,39 @@ function editMoneyRequest(
}
}

type UpdateMoneyRequestAmountAndCurrencyParams = {
transactionID: string;
transactionThreadReportID: string;
currency: string;
amount: number;
policy?: OnyxEntry<OnyxTypes.Policy>;
policyTagList?: OnyxEntry<OnyxTypes.PolicyTagList>;
policyCategories?: OnyxEntry<OnyxTypes.PolicyCategories>;
};

/** Updates the amount and currency fields of a money request */
function updateMoneyRequestAmountAndCurrency(
transactionID: string,
transactionThreadReportID: string,
currency: string,
amount: number,
policy: OnyxEntry<OnyxTypes.Policy>,
policyTagList: OnyxEntry<OnyxTypes.PolicyTagList>,
policyCategories: OnyxEntry<OnyxTypes.PolicyCategories>,
) {
function updateMoneyRequestAmountAndCurrency({
transactionID,
transactionThreadReportID,
currency,
amount,
policy,
policyTagList,
policyCategories,
}: UpdateMoneyRequestAmountAndCurrencyParams) {
const transactionChanges = {
amount,
currency,
};
const {params, onyxData} = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList, policyCategories, true);
const {params, onyxData} = getUpdateMoneyRequestParams(
transactionID,
transactionThreadReportID,
transactionChanges,
policy ?? null,
policyTagList ?? null,
policyCategories ?? null,
true,
);
API.write(WRITE_COMMANDS.UPDATE_MONEY_REQUEST_AMOUNT_AND_CURRENCY, params, onyxData);
}

Expand Down Expand Up @@ -5435,6 +5462,7 @@ function savePreferredPaymentMethod(policyID: string, paymentMethod: PaymentMeth
Onyx.merge(`${ONYXKEYS.NVP_LAST_PAYMENT_METHOD}`, {[policyID]: paymentMethod});
}

export type {IOURequestType};
export {
setMoneyRequestParticipants,
createDistanceRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
import PropTypes from 'prop-types';
import React, {useEffect} from 'react';
import _ from 'underscore';
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView';
import ScreenWrapper from '@components/ScreenWrapper';
import Navigation from '@libs/Navigation/Navigation';
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {WithWritableReportOrNotFoundProps} from './step/withWritableReportOrNotFound';

const propTypes = {
/** Navigation route context info provided by react navigation */
route: PropTypes.shape({
/** Route specific parameters used on this screen */
params: PropTypes.shape({
/** The type of IOU report, i.e. bill, request, send */
iouType: PropTypes.oneOf(_.values(CONST.IOU.TYPE)).isRequired,

/** The type of IOU Request, i.e. manual, scan, distance */
iouRequestType: PropTypes.oneOf(_.values(CONST.IOU.REQUEST_TYPE)).isRequired,
}),
}).isRequired,
};
type IOURequestRedirectToStartPageProps = WithWritableReportOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.START>;

function IOURequestRedirectToStartPage({
route: {
params: {iouType, iouRequestType},
},
}) {
const isIouTypeValid = _.values(CONST.IOU.TYPE).includes(iouType);
const isIouRequestTypeValid = _.values(CONST.IOU.REQUEST_TYPE).includes(iouRequestType);
}: IOURequestRedirectToStartPageProps) {
const isIouTypeValid = Object.values(CONST.IOU.TYPE).includes(iouType);
const isIouRequestTypeValid = Object.values(CONST.IOU.REQUEST_TYPE).includes(iouRequestType);

useEffect(() => {
if (!isIouTypeValid || !isIouRequestTypeValid) {
Expand Down Expand Up @@ -64,6 +52,5 @@ function IOURequestRedirectToStartPage({
}

IOURequestRedirectToStartPage.displayName = 'IOURequestRedirectToStartPage';
IOURequestRedirectToStartPage.propTypes = propTypes;

export default IOURequestRedirectToStartPage;
Loading
Loading