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

refactor: WalletStatementPage to function component #22152

Merged
merged 4 commits into from
Jul 27, 2023
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
84 changes: 41 additions & 43 deletions src/pages/wallet/WalletStatementPage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, {useEffect} from 'react';
import PropTypes from 'prop-types';
import lodashGet from 'lodash/get';
import {withOnyx} from 'react-native-onyx';
Expand Down Expand Up @@ -51,67 +51,65 @@ const defaultProps = {
preferredLocale: CONST.LOCALES.DEFAULT,
};

class WalletStatementPage extends React.Component {
constructor(props) {
super(props);
function WalletStatementPage(props) {
const yearMonth = lodashGet(props.route.params, 'yearMonth', null);

this.processDownload = this.processDownload.bind(this);
this.yearMonth = lodashGet(this.props.route.params, 'yearMonth', null);
}

componentDidMount() {
useEffect(() => {
const currentYearMonth = moment().format('YYYYMM');
if (!this.yearMonth || this.yearMonth.length !== 6 || this.yearMonth > currentYearMonth) {
if (!yearMonth || yearMonth.length !== 6 || yearMonth > currentYearMonth) {
Navigation.dismissModal();
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps -- we want this effect to run only on mount
}, []);

useEffect(() => {
moment.locale(props.preferredLocale);
}, [props.preferredLocale]);

processDownload(yearMonth) {
if (this.props.walletStatement.isGenerating) {
const processDownload = () => {
if (props.walletStatement.isGenerating) {
s-alves10 marked this conversation as resolved.
Show resolved Hide resolved
return;
}

if (this.props.walletStatement[yearMonth]) {
if (props.walletStatement[yearMonth]) {
// We already have a file URL for this statement, so we can download it immediately
const downloadFileName = `Expensify_Statement_${yearMonth}.pdf`;
const fileName = this.props.walletStatement[yearMonth];
const fileName = props.walletStatement[yearMonth];
const pdfURL = `${CONFIG.EXPENSIFY.EXPENSIFY_URL}secure?secureType=pdfreport&filename=${fileName}&downloadName=${downloadFileName}`;
fileDownload(pdfURL, downloadFileName);
return;
}

Growl.show(this.props.translate('statementPage.generatingPDF'), CONST.GROWL.SUCCESS, 3000);
User.generateStatementPDF(this.yearMonth);
}

render() {
moment.locale(this.props.preferredLocale);
const year = this.yearMonth.substring(0, 4) || moment().year();
const month = this.yearMonth.substring(4) || moment().month();
const monthName = moment(month, 'M').format('MMMM');
const title = `${monthName} ${year} statement`;
const url = `${CONFIG.EXPENSIFY.EXPENSIFY_URL}statement.php?period=${this.yearMonth}`;

return (
<ScreenWrapper
shouldShowOfflineIndicator={false}
includeSafeAreaPaddingBottom={false}
>
<HeaderWithBackButton
title={Str.recapitalize(title)}
shouldShowDownloadButton={!this.props.network.isOffline || this.props.walletStatement.isGenerating}
onDownloadButtonPress={() => this.processDownload(this.yearMonth)}
/>
<FullPageOfflineBlockingView>
<WalletStatementModal statementPageURL={url} />
</FullPageOfflineBlockingView>
</ScreenWrapper>
);
}
Growl.show(props.translate('statementPage.generatingPDF'), CONST.GROWL.SUCCESS, 3000);
User.generateStatementPDF(yearMonth);
};

const year = yearMonth.substring(0, 4) || moment().year();
const month = yearMonth.substring(4) || moment().month();
const monthName = moment(month, 'M').format('MMMM');
const title = `${monthName} ${year} statement`;
const url = `${CONFIG.EXPENSIFY.EXPENSIFY_URL}statement.php?period=${yearMonth}`;

return (
<ScreenWrapper
shouldShowOfflineIndicator={false}
includeSafeAreaPaddingBottom={false}
>
<HeaderWithBackButton
title={Str.recapitalize(title)}
shouldShowDownloadButton={!props.network.isOffline || props.walletStatement.isGenerating}
onDownloadButtonPress={processDownload}
/>
<FullPageOfflineBlockingView>
<WalletStatementModal statementPageURL={url} />
</FullPageOfflineBlockingView>
</ScreenWrapper>
);
}

WalletStatementPage.propTypes = propTypes;
WalletStatementPage.defaultProps = defaultProps;
s-alves10 marked this conversation as resolved.
Show resolved Hide resolved
WalletStatementPage.displayName = 'WalletStatementPage';

export default compose(
withLocalize,
Expand Down
Loading