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

Rewrite WalletStatementModal to functional component #26064

Merged
Changes from all 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
72 changes: 32 additions & 40 deletions src/components/WalletStatementModal/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, {useState} from 'react';
import {withOnyx} from 'react-native-onyx';
import lodashGet from 'lodash/get';
import {View} from 'react-native';
Expand All @@ -13,63 +13,55 @@ import ROUTES from '../../ROUTES';
import Navigation from '../../libs/Navigation/Navigation';
import * as Report from '../../libs/actions/Report';

class WalletStatementModal extends React.Component {
constructor(props) {
super(props);

this.state = {
isLoading: true,
};
}
function WalletStatementModal({statementPageURL, session}) {
const [isLoading, setIsLoading] = useState(true);
const authToken = lodashGet(session, 'authToken', null);

/**
* Handles in-app navigation for iframe links
*
* @param {MessageEvent} e
* @param {MessageEvent} event
*/
navigate(e) {
if (!e.data || !e.data.type || (e.data.type !== 'STATEMENT_NAVIGATE' && e.data.type !== 'CONCIERGE_NAVIGATE')) {
const navigate = (event) => {
if (!event.data || !event.data.type || (event.data.type !== 'STATEMENT_NAVIGATE' && event.data.type !== 'CONCIERGE_NAVIGATE')) {
return;
}

if (e.data.type === 'CONCIERGE_NAVIGATE') {
if (event.data.type === 'CONCIERGE_NAVIGATE') {
Report.navigateToConciergeChat();
}

if (e.data.type === 'STATEMENT_NAVIGATE' && e.data.url) {
if (event.data.type === 'STATEMENT_NAVIGATE' && event.data.url) {
const iouRoutes = [ROUTES.IOU_REQUEST, ROUTES.IOU_SEND, ROUTES.IOU_BILL];
const navigateToIOURoute = _.find(iouRoutes, (iouRoute) => e.data.url.includes(iouRoute));
const navigateToIOURoute = _.find(iouRoutes, (iouRoute) => event.data.url.includes(iouRoute));
if (navigateToIOURoute) {
Navigation.navigate(navigateToIOURoute);
}
}
}
};

render() {
const authToken = lodashGet(this.props, 'session.authToken', null);
return (
<>
{this.state.isLoading && <FullScreenLoadingIndicator />}
<View style={[styles.flex1]}>
<iframe
src={`${this.props.statementPageURL}&authToken=${authToken}`}
title="Statements"
height="100%"
width="100%"
seamless="seamless"
frameBorder="0"
onLoad={() => {
this.setState({isLoading: false});
return (
<>
{isLoading && <FullScreenLoadingIndicator />}
<View style={[styles.flex1]}>
<iframe
src={`${statementPageURL}&authToken=${authToken}`}
title="Statements"
height="100%"
width="100%"
seamless="seamless"
frameBorder="0"
onLoad={() => {
setIsLoading(false);

// We listen to a message sent from the iframe to the parent component when a link is clicked.
// This lets us handle navigation in the app, outside of the iframe.
window.onmessage = (e) => this.navigate(e);
}}
/>
</View>
</>
);
}
// We listen to a message sent from the iframe to the parent component when a link is clicked.
// This lets us handle navigation in the app, outside of the iframe.
window.onmessage = navigate;
}}
/>
</View>
</>
);
}

WalletStatementModal.propTypes = walletStatementPropTypes;
Expand Down
Loading