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 'EReceipt.js' component to TypeScript #34619

Merged
merged 7 commits into from
Jan 22, 2024
Merged
Changes from 6 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
49 changes: 23 additions & 26 deletions src/components/EReceipt.js → src/components/EReceipt.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PropTypes from 'prop-types';
import React from 'react';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import useLocalize from '@hooks/useLocalize';
import useStyleUtils from '@hooks/useStyleUtils';
Expand All @@ -11,48 +11,47 @@ import * as ReportUtils from '@libs/ReportUtils';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Transaction} from '@src/types/onyx';
import EReceiptThumbnail from './EReceiptThumbnail';
import Icon from './Icon';
import * as Expensicons from './Icon/Expensicons';
import Text from './Text';
import transactionPropTypes from './transactionPropTypes';

const propTypes = {
/* TransactionID of the transaction this EReceipt corresponds to */
transactionID: PropTypes.string.isRequired,

/* Onyx Props */
transaction: transactionPropTypes,
type EReceiptOnyxProps = {
transaction: OnyxEntry<Transaction>;
};

const defaultProps = {
transaction: {},
type EReceiptProps = EReceiptOnyxProps & {
/* TransactionID of the transaction this EReceipt corresponds to */
transactionID: string;
};

function EReceipt({transaction, transactionID}) {
function EReceipt({transaction, transactionID}: EReceiptProps) {
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {translate} = useLocalize();

// Get receipt colorway, or default to Yellow.
const {backgroundColor: primaryColor, color: secondaryColor} = StyleUtils.getEReceiptColorStyles(StyleUtils.getEReceiptColorCode(transaction));
const colorStyles = StyleUtils.getEReceiptColorStyles(StyleUtils.getEReceiptColorCode(transaction));
const primaryColor = colorStyles?.backgroundColor;
const secondaryColor = colorStyles?.color;

const transactionDetails = ReportUtils.getTransactionDetails(transaction, CONST.DATE.MONTH_DAY_YEAR_FORMAT);
const transactionAmount = transactionDetails?.amount;
const transactionCurrency = transactionDetails?.currency ?? '';
const transactionMerchant = transactionDetails?.merchant;
const transactionDate = transactionDetails?.created;
const transactionCardID = transactionDetails?.cardID;

const {
spcheema marked this conversation as resolved.
Show resolved Hide resolved
amount: transactionAmount,
currency: transactionCurrency,
merchant: transactionMerchant,
created: transactionDate,
cardID: transactionCardID,
} = ReportUtils.getTransactionDetails(transaction, CONST.DATE.MONTH_DAY_YEAR_FORMAT);
const formattedAmount = CurrencyUtils.convertToDisplayString(transactionAmount, transactionCurrency);
const currency = CurrencyUtils.getCurrencySymbol(transactionCurrency);
const amount = formattedAmount.replace(currency, '');
const cardDescription = CardUtils.getCardDescription(transactionCardID);
const amount = currency ? formattedAmount.replace(currency, '') : formattedAmount;
const cardDescription = transactionCardID ? CardUtils.getCardDescription(transactionCardID) : '';

const secondaryTextColorStyle = StyleUtils.getColorStyle(secondaryColor);
const secondaryTextColorStyle = secondaryColor ? StyleUtils.getColorStyle(secondaryColor) : undefined;

return (
<View style={[styles.eReceiptContainer, StyleUtils.getBackgroundColorStyle(primaryColor)]}>
<View style={[styles.eReceiptContainer, primaryColor ? StyleUtils.getBackgroundColorStyle(primaryColor) : undefined]}>
<View style={styles.fullScreen}>
<EReceiptThumbnail transactionID={transactionID} />
</View>
Expand Down Expand Up @@ -99,10 +98,8 @@ function EReceipt({transaction, transactionID}) {
}

EReceipt.displayName = 'EReceipt';
EReceipt.propTypes = propTypes;
EReceipt.defaultProps = defaultProps;

export default withOnyx({
export default withOnyx<EReceiptProps, EReceiptOnyxProps>({
transaction: {
key: ({transactionID}) => `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
},
Expand Down
Loading