-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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 EReceiptThumbnail.js component to TypeScript #32913
Changes from 5 commits
d96c046
5845c36
730d681
3ec7530
12aa175
008d2ad
0c298d6
81a1c64
4964077
ea0ddea
3f67d65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,31 +1,29 @@ | ||||
import PropTypes from 'prop-types'; | ||||
import React, {useMemo, useState} from 'react'; | ||||
import {View} from 'react-native'; | ||||
import {withOnyx} from 'react-native-onyx'; | ||||
import {LayoutChangeEvent, View} from 'react-native'; | ||||
import {OnyxEntry, withOnyx} from 'react-native-onyx'; | ||||
import * as ReportUtils from '@libs/ReportUtils'; | ||||
import useStyleUtils from '@styles/useStyleUtils'; | ||||
import useThemeStyles from '@styles/useThemeStyles'; | ||||
import variables from '@styles/variables'; | ||||
import CONST from '@src/CONST'; | ||||
import ONYXKEYS from '@src/ONYXKEYS'; | ||||
import type {Transaction} from '@src/types/onyx'; | ||||
import Icon from './Icon'; | ||||
import * as eReceiptBGs from './Icon/EReceiptBGs'; | ||||
import * as Expensicons from './Icon/Expensicons'; | ||||
import * as MCCIcons from './Icon/MCCIcons'; | ||||
import Image from './Image'; | ||||
import transactionPropTypes from './transactionPropTypes'; | ||||
|
||||
const propTypes = { | ||||
/* TransactionID of the transaction this EReceipt corresponds to */ | ||||
// eslint-disable-next-line react/no-unused-prop-types | ||||
transactionID: PropTypes.string.isRequired, | ||||
|
||||
/* Onyx Props */ | ||||
transaction: transactionPropTypes, | ||||
type EReceiptThumbnailOnyxProps = { | ||||
transaction: OnyxEntry<Transaction>; | ||||
}; | ||||
|
||||
const defaultProps = { | ||||
transaction: {}, | ||||
type EReceiptThumbnailProps = EReceiptThumbnailOnyxProps & { | ||||
/** TransactionID of the transaction this EReceipt corresponds to | ||||
* It's used by withOnyx HOC | ||||
*/ | ||||
// eslint-disable-next-line react/no-unused-prop-types | ||||
transactionID: string; | ||||
JKobrynski marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
}; | ||||
|
||||
const backgroundImages = { | ||||
|
@@ -37,30 +35,36 @@ const backgroundImages = { | |||
[CONST.ERECEIPT_COLORS.PINK]: eReceiptBGs.EReceiptBG_Pink, | ||||
}; | ||||
|
||||
function EReceiptThumbnail({transaction}) { | ||||
function EReceiptThumbnail({transaction}: EReceiptThumbnailProps) { | ||||
const styles = useThemeStyles(); | ||||
const StyleUtils = useStyleUtils(); | ||||
// Get receipt colorway, or default to Yellow. | ||||
const {backgroundColor: primaryColor, color: secondaryColor} = StyleUtils.getEReceiptColorStyles(StyleUtils.getEReceiptColorCode(transaction)); | ||||
|
||||
const [containerWidth, setContainerWidth] = useState(0); | ||||
const [containerHeight, setContainerHeight] = useState(0); | ||||
|
||||
const onContainerLayout = (event) => { | ||||
const backgroundImage = useMemo(() => backgroundImages[StyleUtils.getEReceiptColorCode(transaction)], [StyleUtils, transaction]); | ||||
|
||||
// Get receipt colorway, or default to Yellow. | ||||
const colorStyles = StyleUtils.getEReceiptColorStyles(StyleUtils.getEReceiptColorCode(transaction)); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think this comment can be removed, it is not valuable and I don't think it's accurate anymore. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||||
const primaryColor = colorStyles?.backgroundColor; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't these consts (and many others in this file) have type declarations? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||
const secondaryColor = colorStyles?.color; | ||||
|
||||
const onContainerLayout = (event: LayoutChangeEvent) => { | ||||
const {width, height} = event.nativeEvent.layout; | ||||
setContainerWidth(width); | ||||
setContainerHeight(height); | ||||
}; | ||||
|
||||
const {mccGroup: transactionMCCGroup} = ReportUtils.getTransactionDetails(transaction); | ||||
const MCCIcon = MCCIcons[`${transactionMCCGroup}`]; | ||||
const transactionDetails = ReportUtils.getTransactionDetails(transaction); | ||||
const transactionMCCGroup = transactionDetails?.mccGroup; | ||||
const MCCIcon = transactionMCCGroup ? MCCIcons[`${transactionMCCGroup}`] : undefined; | ||||
|
||||
const isSmall = containerWidth && containerWidth < variables.eReceiptThumbnailSmallBreakpoint; | ||||
const isMedium = containerWidth && containerWidth < variables.eReceiptThumbnailMediumBreakpoint; | ||||
|
||||
let receiptIconWidth = variables.eReceiptIconWidth; | ||||
let receiptIconHeight = variables.eReceiptIconHeight; | ||||
let receiptMCCSize = variables.eReceiptMCCHeightWidth; | ||||
let receiptIconWidth: number = variables.eReceiptIconWidth; | ||||
let receiptIconHeight: number = variables.eReceiptIconHeight; | ||||
let receiptMCCSize: number = variables.eReceiptMCCHeightWidth; | ||||
|
||||
if (isSmall) { | ||||
receiptIconWidth = variables.eReceiptIconWidthSmall; | ||||
|
@@ -72,13 +76,11 @@ function EReceiptThumbnail({transaction}) { | |||
receiptMCCSize = variables.eReceiptMCCHeightWidthMedium; | ||||
} | ||||
|
||||
const backgroundImage = useMemo(() => backgroundImages[StyleUtils.getEReceiptColorCode(transaction)], [StyleUtils, transaction]); | ||||
|
||||
return ( | ||||
<View | ||||
style={[ | ||||
styles.flex1, | ||||
StyleUtils.getBackgroundColorStyle(primaryColor), | ||||
primaryColor ? StyleUtils.getBackgroundColorStyle(primaryColor) : {}, | ||||
styles.overflowHidden, | ||||
styles.alignItemsCenter, | ||||
containerHeight && containerHeight < variables.eReceiptThumnailCenterReceiptBreakpoint ? styles.justifyContentCenter : {}, | ||||
|
@@ -114,10 +116,8 @@ function EReceiptThumbnail({transaction}) { | |||
} | ||||
|
||||
EReceiptThumbnail.displayName = 'EReceiptThumbnail'; | ||||
EReceiptThumbnail.propTypes = propTypes; | ||||
EReceiptThumbnail.defaultProps = defaultProps; | ||||
|
||||
export default withOnyx({ | ||||
export default withOnyx<EReceiptThumbnailProps, EReceiptThumbnailOnyxProps>({ | ||||
transaction: { | ||||
key: ({transactionID}) => `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, | ||||
}, | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prop comments are preferred to be single lines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!