-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Migrate QRShare to function component #32690
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
Merged
marcaaron
merged 1 commit into
Expensify:main
from
fabioh8010:feature/migrate-qrshare-to-function-component
Dec 12, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,53 @@ | ||
| import React, {Component} from 'react'; | ||
| import {withNetwork} from '@components/OnyxProvider'; | ||
| import React, {forwardRef, useImperativeHandle, useRef} from 'react'; | ||
| import getQrCodeFileName from '@components/QRShare/getQrCodeDownloadFileName'; | ||
| import {qrShareDefaultProps, qrSharePropTypes} from '@components/QRShare/propTypes'; | ||
| import useNetwork from '@hooks/useNetwork'; | ||
| import fileDownload from '@libs/fileDownload'; | ||
| import QRShare from '..'; | ||
|
|
||
| class QRShareWithDownload extends Component { | ||
| qrShareRef = React.createRef(); | ||
|
|
||
| constructor(props) { | ||
| super(props); | ||
|
|
||
| this.download = this.download.bind(this); | ||
| } | ||
|
|
||
| download() { | ||
| return new Promise((resolve, reject) => { | ||
| // eslint-disable-next-line es/no-optional-chaining | ||
| const svg = this.qrShareRef.current?.getSvg(); | ||
| if (svg == null) { | ||
| return reject(); | ||
| } | ||
|
|
||
| svg.toDataURL((dataURL) => resolve(fileDownload(dataURL, getQrCodeFileName(this.props.title)))); | ||
| }); | ||
| } | ||
|
|
||
| render() { | ||
| return ( | ||
| <QRShare | ||
| ref={this.qrShareRef} | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| {...this.props} | ||
| logo={this.props.network.isOffline ? null : this.props.logo} | ||
| /> | ||
| ); | ||
| } | ||
| function QRShareWithDownload({innerRef, ...props}) { | ||
| const {isOffline} = useNetwork(); | ||
| const qrShareRef = useRef(null); | ||
|
|
||
| useImperativeHandle( | ||
| innerRef, | ||
| () => ({ | ||
| download: () => | ||
| new Promise((resolve, reject) => { | ||
| // eslint-disable-next-line es/no-optional-chaining | ||
| const svg = qrShareRef.current?.getSvg(); | ||
| if (svg == null) { | ||
| return reject(); | ||
| } | ||
|
|
||
| svg.toDataURL((dataURL) => resolve(fileDownload(dataURL, getQrCodeFileName(props.title)))); | ||
| }), | ||
| }), | ||
| [props.title], | ||
| ); | ||
|
|
||
| return ( | ||
| <QRShare | ||
| ref={qrShareRef} | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| {...props} | ||
| logo={isOffline ? null : props.logo} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| QRShareWithDownload.propTypes = qrSharePropTypes; | ||
| QRShareWithDownload.defaultProps = qrShareDefaultProps; | ||
| QRShareWithDownload.displayName = 'QRShareWithDownload'; | ||
|
|
||
| const QRShareWithDownloadWithRef = forwardRef((props, ref) => ( | ||
| <QRShareWithDownload | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| {...props} | ||
| innerRef={ref} | ||
| /> | ||
| )); | ||
|
|
||
| QRShareWithDownloadWithRef.displayName = 'QRShareWithDownloadWithRef'; | ||
|
fabioh8010 marked this conversation as resolved.
|
||
|
|
||
| export default withNetwork()(QRShareWithDownload); | ||
| export default QRShareWithDownloadWithRef; | ||
59 changes: 34 additions & 25 deletions
59
src/components/QRShare/QRShareWithDownload/index.native.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,46 @@ | ||
| import React, {Component} from 'react'; | ||
| import React, {forwardRef, useImperativeHandle, useRef} from 'react'; | ||
| import ViewShot from 'react-native-view-shot'; | ||
| import {withNetwork} from '@components/OnyxProvider'; | ||
| import getQrCodeFileName from '@components/QRShare/getQrCodeDownloadFileName'; | ||
| import {qrShareDefaultProps, qrSharePropTypes} from '@components/QRShare/propTypes'; | ||
| import useNetwork from '@hooks/useNetwork'; | ||
| import fileDownload from '@libs/fileDownload'; | ||
| import QRShare from '..'; | ||
|
|
||
| class QRShareWithDownload extends Component { | ||
| qrCodeScreenshotRef = React.createRef(); | ||
| function QRShareWithDownload({innerRef, ...props}) { | ||
|
fabioh8010 marked this conversation as resolved.
|
||
| const {isOffline} = useNetwork(); | ||
| const qrCodeScreenshotRef = useRef(null); | ||
|
|
||
| constructor(props) { | ||
| super(props); | ||
| useImperativeHandle( | ||
| innerRef, | ||
| () => ({ | ||
| download: () => qrCodeScreenshotRef.current.capture().then((uri) => fileDownload(uri, getQrCodeFileName(props.title))), | ||
| }), | ||
| [props.title], | ||
| ); | ||
|
|
||
| this.download = this.download.bind(this); | ||
| } | ||
|
|
||
| download() { | ||
| return this.qrCodeScreenshotRef.current.capture().then((uri) => fileDownload(uri, getQrCodeFileName(this.props.title))); | ||
| } | ||
|
|
||
| render() { | ||
| return ( | ||
| <ViewShot ref={this.qrCodeScreenshotRef}> | ||
| <QRShare | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| {...this.props} | ||
| logo={this.props.network.isOffline ? null : this.props.logo} | ||
| /> | ||
| </ViewShot> | ||
| ); | ||
| } | ||
| return ( | ||
| <ViewShot ref={qrCodeScreenshotRef}> | ||
| <QRShare | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| {...props} | ||
| logo={isOffline ? null : props.logo} | ||
| /> | ||
| </ViewShot> | ||
| ); | ||
| } | ||
|
|
||
| QRShareWithDownload.propTypes = qrSharePropTypes; | ||
| QRShareWithDownload.defaultProps = qrShareDefaultProps; | ||
| QRShareWithDownload.displayName = 'QRShareWithDownload'; | ||
|
|
||
| const QRShareWithDownloadWithRef = forwardRef((props, ref) => ( | ||
| <QRShareWithDownload | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| {...props} | ||
| innerRef={ref} | ||
| /> | ||
| )); | ||
|
fabioh8010 marked this conversation as resolved.
|
||
|
|
||
| QRShareWithDownloadWithRef.displayName = 'QRShareWithDownloadWithRef'; | ||
|
|
||
| export default withNetwork()(QRShareWithDownload); | ||
| export default QRShareWithDownloadWithRef; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,96 +1,91 @@ | ||
| import React, {Component} from 'react'; | ||
| import React, {forwardRef, useImperativeHandle, useRef, useState} from 'react'; | ||
| import {View} from 'react-native'; | ||
| import _ from 'underscore'; | ||
| import ExpensifyWordmark from '@assets/images/expensify-wordmark.svg'; | ||
| import QRCode from '@components/QRCode'; | ||
| import Text from '@components/Text'; | ||
| import withLocalize, {withLocalizePropTypes} from '@components/withLocalize'; | ||
| import withTheme, {withThemePropTypes} from '@components/withTheme'; | ||
| import withThemeStyles, {withThemeStylesPropTypes} from '@components/withThemeStyles'; | ||
| import withWindowDimensions, {windowDimensionsPropTypes} from '@components/withWindowDimensions'; | ||
| import compose from '@libs/compose'; | ||
| import useTheme from '@styles/themes/useTheme'; | ||
| import useThemeStyles from '@styles/useThemeStyles'; | ||
| import variables from '@styles/variables'; | ||
| import {qrShareDefaultProps, qrSharePropTypes} from './propTypes'; | ||
|
|
||
| const propTypes = { | ||
| ...qrSharePropTypes, | ||
| ...windowDimensionsPropTypes, | ||
| ...withLocalizePropTypes, | ||
| ...withThemeStylesPropTypes, | ||
| ...withThemePropTypes, | ||
| }; | ||
| function QRShare({innerRef, url, title, subtitle, logo, logoRatio, logoMarginRatio}) { | ||
|
fabioh8010 marked this conversation as resolved.
|
||
| const styles = useThemeStyles(); | ||
| const theme = useTheme(); | ||
|
|
||
| class QRShare extends Component { | ||
| constructor(props) { | ||
| super(props); | ||
| const [qrCodeSize, setQrCodeSize] = useState(1); | ||
|
Contributor
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. Having small initial size for QR code made it change its size while page is opening. Ref: #44436 |
||
| const svgRef = useRef(null); | ||
|
|
||
| this.state = { | ||
| qrCodeSize: 1, | ||
| }; | ||
| useImperativeHandle( | ||
| innerRef, | ||
| () => ({ | ||
| getSvg: () => svgRef.current, | ||
| }), | ||
| [], | ||
| ); | ||
|
|
||
| this.onLayout = this.onLayout.bind(this); | ||
| this.getSvg = this.getSvg.bind(this); | ||
| } | ||
|
|
||
| onLayout(event) { | ||
| const onLayout = (event) => { | ||
| const containerWidth = event.nativeEvent.layout.width - variables.qrShareHorizontalPadding * 2 || 0; | ||
| setQrCodeSize(Math.max(1, containerWidth)); | ||
| }; | ||
|
|
||
| this.setState({ | ||
| qrCodeSize: Math.max(1, containerWidth), | ||
| }); | ||
| } | ||
| return ( | ||
| <View | ||
| style={styles.shareCodeContainer} | ||
| onLayout={onLayout} | ||
| > | ||
| <View style={styles.expensifyQrLogo}> | ||
| <ExpensifyWordmark | ||
| fill={theme.QRLogo} | ||
| width="100%" | ||
| height="100%" | ||
| /> | ||
| </View> | ||
|
|
||
| getSvg() { | ||
| return this.svg; | ||
| } | ||
| <QRCode | ||
| getRef={(svg) => (svgRef.current = svg)} | ||
| url={url} | ||
| logo={logo} | ||
| size={qrCodeSize} | ||
| logoRatio={logoRatio} | ||
| logoMarginRatio={logoMarginRatio} | ||
| /> | ||
|
|
||
| render() { | ||
| return ( | ||
| <View | ||
| style={this.props.themeStyles.shareCodeContainer} | ||
| onLayout={this.onLayout} | ||
| <Text | ||
| family="EXP_NEW_KANSAS_MEDIUM" | ||
| fontSize={variables.fontSizeXLarge} | ||
| numberOfLines={2} | ||
| style={styles.qrShareTitle} | ||
| > | ||
| <View style={this.props.themeStyles.expensifyQrLogo}> | ||
| <ExpensifyWordmark | ||
| fill={this.props.theme.QRLogo} | ||
| width="100%" | ||
| height="100%" | ||
| /> | ||
| </View> | ||
|
|
||
| <QRCode | ||
| getRef={(svg) => (this.svg = svg)} | ||
| url={this.props.url} | ||
| logo={this.props.logo} | ||
| size={this.state.qrCodeSize} | ||
| logoRatio={this.props.logoRatio} | ||
| logoMarginRatio={this.props.logoMarginRatio} | ||
| /> | ||
| {title} | ||
| </Text> | ||
|
|
||
| {!_.isEmpty(subtitle) && ( | ||
| <Text | ||
| family="EXP_NEW_KANSAS_MEDIUM" | ||
| fontSize={variables.fontSizeXLarge} | ||
| fontSize={variables.fontSizeLabel} | ||
| numberOfLines={2} | ||
| style={this.props.themeStyles.qrShareTitle} | ||
| style={[styles.mt1, styles.textAlignCenter]} | ||
| color={theme.textSupporting} | ||
| > | ||
| {this.props.title} | ||
| {subtitle} | ||
| </Text> | ||
|
|
||
| {!_.isEmpty(this.props.subtitle) && ( | ||
| <Text | ||
| fontSize={variables.fontSizeLabel} | ||
| numberOfLines={2} | ||
| style={[this.props.themeStyles.mt1, this.props.themeStyles.textAlignCenter]} | ||
| color={this.props.theme.textSupporting} | ||
| > | ||
| {this.props.subtitle} | ||
| </Text> | ||
| )} | ||
| </View> | ||
| ); | ||
| } | ||
| )} | ||
| </View> | ||
| ); | ||
| } | ||
| QRShare.propTypes = propTypes; | ||
|
|
||
| QRShare.propTypes = qrSharePropTypes; | ||
| QRShare.defaultProps = qrShareDefaultProps; | ||
| QRShare.displayName = 'QRShare'; | ||
|
|
||
| const QRShareWithRef = forwardRef((props, ref) => ( | ||
| <QRShare | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| {...props} | ||
| innerRef={ref} | ||
| /> | ||
| )); | ||
|
|
||
| QRShareWithRef.displayName = 'QRShareWithRef'; | ||
|
|
||
| export default compose(withLocalize, withWindowDimensions, withThemeStyles, withTheme)(QRShare); | ||
| export default QRShareWithRef; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.