Skip to content

Commit

Permalink
Merge pull request #34092 from VickyStash/ts-migration/iou
Browse files Browse the repository at this point in the history
[TS migration] Migrate 'IOU.js' lib to TypeScript
  • Loading branch information
roryabraham authored Feb 5, 2024
2 parents 3e7839e + 5359633 commit b7a3fca
Show file tree
Hide file tree
Showing 42 changed files with 1,678 additions and 1,456 deletions.
2 changes: 1 addition & 1 deletion src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,9 @@ type OnyxValues = {
[ONYXKEYS.COLLECTION.TRANSACTION]: OnyxTypes.Transaction;
[ONYXKEYS.COLLECTION.TRANSACTION_DRAFT]: OnyxTypes.Transaction;
[ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS]: OnyxTypes.TransactionViolations;
[ONYXKEYS.COLLECTION.SPLIT_TRANSACTION_DRAFT]: OnyxTypes.Transaction;
[ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS]: OnyxTypes.RecentlyUsedTags;
[ONYXKEYS.COLLECTION.SELECTED_TAB]: string;
[ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS]: OnyxTypes.TransactionViolation[];
[ONYXKEYS.COLLECTION.PRIVATE_NOTES_DRAFT]: string;
[ONYXKEYS.COLLECTION.NEXT_STEP]: OnyxTypes.ReportNextStep;

Expand Down
2 changes: 1 addition & 1 deletion src/components/AttachmentModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ function AttachmentModal({
* Detach the receipt and close the modal.
*/
const deleteAndCloseModal = useCallback(() => {
IOU.detachReceipt(transaction?.transactionID);
IOU.detachReceipt(transaction?.transactionID ?? '');
setIsDeleteReceiptConfirmModalVisible(false);
Navigation.dismissModal(report?.reportID);
}, [transaction, report]);
Expand Down
2 changes: 1 addition & 1 deletion src/components/FormAlertWithSubmitButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import FormAlertWrapper from './FormAlertWrapper';

type FormAlertWithSubmitButtonProps = {
/** Error message to display above button */
message?: string;
message?: string | null;

/** Whether the button is disabled */
isDisabled?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/components/FormAlertWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type FormAlertWrapperProps = {
isMessageHtml?: boolean;

/** Error message to display above button */
message?: string;
message?: string | null;

/** Props to detect online status */
network: Network;
Expand Down
11 changes: 5 additions & 6 deletions src/components/OfflineWithFeedback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ type OfflineWithFeedbackProps = ChildrenProps & {

type StrikethroughProps = Partial<ChildrenProps> & {style: Array<ViewStyle | TextStyle | ImageStyle>};

function omitBy<T>(obj: Record<string, T> | undefined | null, predicate: (value: T) => boolean) {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-unused-vars
return Object.fromEntries(Object.entries(obj ?? {}).filter(([_, value]) => !predicate(value)));
}

function OfflineWithFeedback({
pendingAction,
canDismissError = true,
Expand All @@ -84,8 +79,12 @@ function OfflineWithFeedback({
const {isOffline} = useNetwork();

const hasErrors = !isEmptyObject(errors ?? {});

// Some errors have a null message. This is used to apply opacity only and to avoid showing redundant messages.
const errorMessages = omitBy(errors, (e: string | ReceiptError) => e === null);
const errorEntries = Object.entries(errors ?? {});
const filteredErrorEntries = errorEntries.filter((errorEntry): errorEntry is [string, string | ReceiptError] => errorEntry[1] !== null);
const errorMessages = Object.fromEntries(filteredErrorEntries);

const hasErrorMessages = !isEmptyObject(errorMessages);
const isOfflinePendingAction = !!isOffline && !!pendingAction;
const isUpdateOrDeleteError = hasErrors && (pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE || pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE);
Expand Down
2 changes: 2 additions & 0 deletions src/components/TextInput/BaseTextInput/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ function BaseTextInput(

const hasLabel = Boolean(label?.length);
const isReadOnly = inputProps.readOnly ?? inputProps.disabled;
// Disabling this line for safeness as nullish coalescing works only if the value is undefined or null, and errorText can be an empty string
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const inputHelpText = errorText || hint;
const placeholderValue = !!prefixCharacter || isFocused || !hasLabel || (hasLabel && forceActiveLabel) ? placeholder : undefined;
const maxHeight = StyleSheet.flatten(containerStyles)?.maxHeight;
Expand Down
2 changes: 2 additions & 0 deletions src/components/TextInput/BaseTextInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ function BaseTextInput(

const hasLabel = Boolean(label?.length);
const isReadOnly = inputProps.readOnly ?? inputProps.disabled;
// Disabling this line for safeness as nullish coalescing works only if the value is undefined or null, and errorText can be an empty string
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const inputHelpText = errorText || hint;
const newPlaceholder = !!prefixCharacter || isFocused || !hasLabel || (hasLabel && forceActiveLabel) ? placeholder : undefined;
const maxHeight = StyleSheet.flatten(containerStyles).maxHeight;
Expand Down
6 changes: 6 additions & 0 deletions src/libs/API/parameters/ApproveMoneyRequestParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type ApproveMoneyRequestParams = {
reportID: string;
approvedReportActionID: string;
};

export default ApproveMoneyRequestParams;
13 changes: 13 additions & 0 deletions src/libs/API/parameters/CompleteSplitBillParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type CompleteSplitBillParams = {
transactionID: string;
amount?: number;
currency?: string;
created?: string;
merchant?: string;
comment?: string;
category?: string;
tag?: string;
splits: string;
};

export default CompleteSplitBillParams;
17 changes: 17 additions & 0 deletions src/libs/API/parameters/CreateDistanceRequestParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type CreateDistanceRequestParams = {
comment: string;
iouReportID: string;
chatReportID: string;
transactionID: string;
reportActionID: string;
createdChatReportActionID: string;
createdIOUReportActionID: string;
reportPreviewReportActionID: string;
waypoints: string;
created: string;
category?: string;
tag?: string;
billable?: boolean;
};

export default CreateDistanceRequestParams;
6 changes: 6 additions & 0 deletions src/libs/API/parameters/DeleteMoneyRequestParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type DeleteMoneyRequestParams = {
transactionID: string;
reportActionID: string;
};

export default DeleteMoneyRequestParams;
5 changes: 5 additions & 0 deletions src/libs/API/parameters/DetachReceiptParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type DetachReceiptParams = {
transactionID: string;
};

export default DetachReceiptParams;
14 changes: 14 additions & 0 deletions src/libs/API/parameters/EditMoneyRequestParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type EditMoneyRequestParams = {
transactionID: string;
reportActionID: string;
created?: string;
amount?: number;
currency?: string;
comment?: string;
merchant?: string;
category?: string;
billable?: boolean;
tag?: string;
};

export default EditMoneyRequestParams;
11 changes: 11 additions & 0 deletions src/libs/API/parameters/PayMoneyRequestParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type CONST from '@src/CONST';
import type DeepValueOf from '@src/types/utils/DeepValueOf';

type PayMoneyRequestParams = {
iouReportID: string;
chatReportID: string;
reportActionID: string;
paymentMethodType: DeepValueOf<typeof CONST.IOU.PAYMENT_TYPE>;
};

export default PayMoneyRequestParams;
6 changes: 6 additions & 0 deletions src/libs/API/parameters/ReplaceReceiptParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type ReplaceReceiptParams = {
transactionID: string;
receipt: File;
};

export default ReplaceReceiptParams;
29 changes: 29 additions & 0 deletions src/libs/API/parameters/RequestMoneyParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type {ValueOf} from 'type-fest';
import type CONST from '@src/CONST';
import type {Receipt} from '@src/types/onyx/Transaction';

type RequestMoneyParams = {
debtorEmail: string;
debtorAccountID: number;
amount: number;
currency: string;
comment: string;
created: string;
merchant: string;
iouReportID: string;
chatReportID: string;
transactionID: string;
reportActionID: string;
createdChatReportActionID: string;
createdIOUReportActionID: string;
reportPreviewReportActionID: string;
receipt: Receipt;
receiptState?: ValueOf<typeof CONST.IOU.RECEIPT_STATE>;
category?: string;
tag?: string;
taxCode: string;
taxAmount: number;
billable?: boolean;
};

export default RequestMoneyParams;
15 changes: 15 additions & 0 deletions src/libs/API/parameters/SendMoneyParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type CONST from '@src/CONST';
import type DeepValueOf from '@src/types/utils/DeepValueOf';

type SendMoneyParams = {
iouReportID: string;
chatReportID: string;
reportActionID: string;
paymentMethodType: DeepValueOf<typeof CONST.IOU.PAYMENT_TYPE>;
transactionID: string;
newIOUReportDetails: string;
createdReportActionID: string;
reportPreviewReportActionID: string;
};

export default SendMoneyParams;
17 changes: 17 additions & 0 deletions src/libs/API/parameters/SplitBillParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type SplitBillParams = {
reportID: string;
amount: number;
splits: string;
comment: string;
currency: string;
merchant: string;
category: string;
tag: string;
billable: boolean;
transactionID: string;
reportActionID: string;
createdReportActionID?: string;
policyID?: string;
};

export default SplitBillParams;
17 changes: 17 additions & 0 deletions src/libs/API/parameters/StartSplitBillParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type {Receipt} from '@src/types/onyx/Transaction';

type StartSplitBillParams = {
chatReportID: string;
reportActionID: string;
transactionID: string;
splits: string;
receipt: Receipt;
comment: string;
category: string;
tag: string;
isFromGroupDM: boolean;
createdReportActionID?: string;
billable: boolean;
};

export default StartSplitBillParams;
7 changes: 7 additions & 0 deletions src/libs/API/parameters/SubmitReportParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type SubmitReportParams = {
reportID: string;
managerAccountID?: number;
reportActionID: string;
};

export default SubmitReportParams;
9 changes: 9 additions & 0 deletions src/libs/API/parameters/UpdateMoneyRequestParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type {TransactionDetails} from '@libs/ReportUtils';

type UpdateMoneyRequestParams = Partial<TransactionDetails> & {
reportID?: string;
transactionID: string;
reportActionID?: string;
};

export default UpdateMoneyRequestParams;
14 changes: 14 additions & 0 deletions src/libs/API/parameters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,17 @@ export type {default as CompleteEngagementModalParams} from './CompleteEngagemen
export type {default as SetNameValuePairParams} from './SetNameValuePairParams';
export type {default as SetReportFieldParams} from './SetReportFieldParams';
export type {default as SetReportNameParams} from './SetReportNameParams';
export type {default as CompleteSplitBillParams} from './CompleteSplitBillParams';
export type {default as UpdateMoneyRequestParams} from './UpdateMoneyRequestParams';
export type {default as RequestMoneyParams} from './RequestMoneyParams';
export type {default as SplitBillParams} from './SplitBillParams';
export type {default as DeleteMoneyRequestParams} from './DeleteMoneyRequestParams';
export type {default as CreateDistanceRequestParams} from './CreateDistanceRequestParams';
export type {default as StartSplitBillParams} from './StartSplitBillParams';
export type {default as SendMoneyParams} from './SendMoneyParams';
export type {default as ApproveMoneyRequestParams} from './ApproveMoneyRequestParams';
export type {default as EditMoneyRequestParams} from './EditMoneyRequestParams';
export type {default as ReplaceReceiptParams} from './ReplaceReceiptParams';
export type {default as SubmitReportParams} from './SubmitReportParams';
export type {default as DetachReceiptParams} from './DetachReceiptParams';
export type {default as PayMoneyRequestParams} from './PayMoneyRequestParams';
50 changes: 50 additions & 0 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,31 @@ const WRITE_COMMANDS = {
SET_NAME_VALUE_PAIR: 'SetNameValuePair',
SET_REPORT_FIELD: 'Report_SetFields',
SET_REPORT_NAME: 'RenameReport',
COMPLETE_SPLIT_BILL: 'CompleteSplitBill',
UPDATE_MONEY_REQUEST_DATE: 'UpdateMoneyRequestDate',
UPDATE_MONEY_REQUEST_BILLABLE: 'UpdateMoneyRequestBillable',
UPDATE_MONEY_REQUEST_MERCHANT: 'UpdateMoneyRequestMerchant',
UPDATE_MONEY_REQUEST_TAG: 'UpdateMoneyRequestTag',
UPDATE_MONEY_REQUEST_DISTANCE: 'UpdateMoneyRequestDistance',
UPDATE_MONEY_REQUEST_CATEGORY: 'UpdateMoneyRequestCategory',
UPDATE_MONEY_REQUEST_DESCRIPTION: 'UpdateMoneyRequestDescription',
UPDATE_MONEY_REQUEST_AMOUNT_AND_CURRENCY: 'UpdateMoneyRequestAmountAndCurrency',
UPDATE_DISTANCE_REQUEST: 'UpdateDistanceRequest',
REQUEST_MONEY: 'RequestMoney',
SPLIT_BILL: 'SplitBill',
SPLIT_BILL_AND_OPEN_REPORT: 'SplitBillAndOpenReport',
DELETE_MONEY_REQUEST: 'DeleteMoneyRequest',
CREATE_DISTANCE_REQUEST: 'CreateDistanceRequest',
START_SPLIT_BILL: 'StartSplitBill',
SEND_MONEY_ELSEWHERE: 'SendMoneyElsewhere',
SEND_MONEY_WITH_WALLET: 'SendMoneyWithWallet',
APPROVE_MONEY_REQUEST: 'ApproveMoneyRequest',
EDIT_MONEY_REQUEST: 'EditMoneyRequest',
REPLACE_RECEIPT: 'ReplaceReceipt',
SUBMIT_REPORT: 'SubmitReport',
DETACH_RECEIPT: 'DetachReceipt',
PAY_MONEY_REQUEST_WITH_WALLET: 'PayMoneyRequestWithWallet',
PAY_MONEY_REQUEST: 'PayMoneyRequest',
} as const;

type WriteCommand = ValueOf<typeof WRITE_COMMANDS>;
Expand Down Expand Up @@ -229,6 +254,31 @@ type WriteCommandParameters = {
[WRITE_COMMANDS.SET_NAME_VALUE_PAIR]: Parameters.SetNameValuePairParams;
[WRITE_COMMANDS.SET_REPORT_FIELD]: Parameters.SetReportFieldParams;
[WRITE_COMMANDS.SET_REPORT_NAME]: Parameters.SetReportNameParams;
[WRITE_COMMANDS.COMPLETE_SPLIT_BILL]: Parameters.CompleteSplitBillParams;
[WRITE_COMMANDS.UPDATE_MONEY_REQUEST_DATE]: Parameters.UpdateMoneyRequestParams;
[WRITE_COMMANDS.UPDATE_MONEY_REQUEST_MERCHANT]: Parameters.UpdateMoneyRequestParams;
[WRITE_COMMANDS.UPDATE_MONEY_REQUEST_BILLABLE]: Parameters.UpdateMoneyRequestParams;
[WRITE_COMMANDS.UPDATE_MONEY_REQUEST_TAG]: Parameters.UpdateMoneyRequestParams;
[WRITE_COMMANDS.UPDATE_MONEY_REQUEST_DISTANCE]: Parameters.UpdateMoneyRequestParams;
[WRITE_COMMANDS.UPDATE_MONEY_REQUEST_CATEGORY]: Parameters.UpdateMoneyRequestParams;
[WRITE_COMMANDS.UPDATE_MONEY_REQUEST_DESCRIPTION]: Parameters.UpdateMoneyRequestParams;
[WRITE_COMMANDS.UPDATE_DISTANCE_REQUEST]: Parameters.UpdateMoneyRequestParams;
[WRITE_COMMANDS.UPDATE_MONEY_REQUEST_AMOUNT_AND_CURRENCY]: Parameters.UpdateMoneyRequestParams;
[WRITE_COMMANDS.REQUEST_MONEY]: Parameters.RequestMoneyParams;
[WRITE_COMMANDS.SPLIT_BILL]: Parameters.SplitBillParams;
[WRITE_COMMANDS.SPLIT_BILL_AND_OPEN_REPORT]: Parameters.SplitBillParams;
[WRITE_COMMANDS.DELETE_MONEY_REQUEST]: Parameters.DeleteMoneyRequestParams;
[WRITE_COMMANDS.CREATE_DISTANCE_REQUEST]: Parameters.CreateDistanceRequestParams;
[WRITE_COMMANDS.START_SPLIT_BILL]: Parameters.StartSplitBillParams;
[WRITE_COMMANDS.SEND_MONEY_ELSEWHERE]: Parameters.SendMoneyParams;
[WRITE_COMMANDS.SEND_MONEY_WITH_WALLET]: Parameters.SendMoneyParams;
[WRITE_COMMANDS.APPROVE_MONEY_REQUEST]: Parameters.ApproveMoneyRequestParams;
[WRITE_COMMANDS.EDIT_MONEY_REQUEST]: Parameters.EditMoneyRequestParams;
[WRITE_COMMANDS.REPLACE_RECEIPT]: Parameters.ReplaceReceiptParams;
[WRITE_COMMANDS.SUBMIT_REPORT]: Parameters.SubmitReportParams;
[WRITE_COMMANDS.DETACH_RECEIPT]: Parameters.DetachReceiptParams;
[WRITE_COMMANDS.PAY_MONEY_REQUEST_WITH_WALLET]: Parameters.PayMoneyRequestParams;
[WRITE_COMMANDS.PAY_MONEY_REQUEST]: Parameters.PayMoneyRequestParams;
};

const READ_COMMANDS = {
Expand Down
Loading

0 comments on commit b7a3fca

Please sign in to comment.