Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions src/languages/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,9 @@ export default {
private: 'Private',
},
},
statementPage: {
generatingPDF: 'We\'re generating your PDF right now. Please come back later!',
},
keyboardShortcutModal: {
title: 'Keyboard Shortcuts',
subtitle: 'Save time with these handy keyboard shortcuts:',
Expand Down
3 changes: 3 additions & 0 deletions src/languages/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,9 @@ export default {
private: 'Privada',
},
},
statementPage: {
generatingPDF: 'Estamos generando tu PDF ahora mismo. ¡Por favor, vuelve más tarde!',
},
keyboardShortcutModal: {
title: 'Atajos de teclado',
subtitle: 'Ahorra tiempo con estos atajos de teclado:',
Expand Down
42 changes: 29 additions & 13 deletions src/libs/actions/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,21 +461,37 @@ function joinScreenShare(accessToken, roomName) {
/**
* Downloads the statement PDF for the provided period
* @param {String} period YYYYMM format
* @returns {Promise<Void>}
*/
function generateStatementPDF(period) {
Onyx.merge(ONYXKEYS.WALLET_STATEMENT, {isGenerating: true});
return DeprecatedAPI.GetStatementPDF({period})
.then((response) => {
if (response.jsonCode !== 200 || !response.filename) {
Log.info('[User] Failed to generate statement PDF', false, {response});
return;
}

Onyx.merge(ONYXKEYS.WALLET_STATEMENT, {[period]: response.filename});
}).finally(() => {
Onyx.merge(ONYXKEYS.WALLET_STATEMENT, {isGenerating: false});
});
API.read('GetStatementPDF', {period}, {
optimisticData: [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.WALLET_STATEMENT,
value: {
isGenerating: true,
},
},
],
successData: [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.WALLET_STATEMENT,
value: {
isGenerating: false,
},
},
],
failureData: [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.WALLET_STATEMENT,
value: {
isGenerating: false,
},
},
],
});
}

export {
Expand Down
64 changes: 24 additions & 40 deletions src/pages/wallet/WalletStatementPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import * as User from '../../libs/actions/User';
import fileDownload from '../../libs/fileDownload';
import Growl from '../../libs/Growl';
import CONST from '../../CONST';
import makeCancellablePromise from '../../libs/MakeCancellablePromise';
import FullPageOfflineBlockingView from '../../components/BlockingViews/FullPageOfflineBlockingView';
import {withNetwork} from '../../components/OnyxProvider';
import networkPropTypes from '../../components/networkPropTypes';

const propTypes = {
/** The route object passed to this page from the navigator */
Expand All @@ -31,10 +33,13 @@ const propTypes = {
}).isRequired,

walletStatement: PropTypes.shape({
/** Whether the PDF file available for download or not */
/** Whether we are currently generating a PDF version of the statement */
isGenerating: PropTypes.bool,
}),

/** Information about the network */
network: networkPropTypes.isRequired,

...withLocalizePropTypes,
};

Expand All @@ -48,50 +53,26 @@ class WalletStatementPage extends React.Component {
constructor(props) {
super(props);

this.state = {
isDownloading: false,
};
this.processDownload = this.processDownload.bind(this);
this.yearMonth = lodashGet(this.props.route.params, 'yearMonth', null);
this.generatePDFPromise = null;
}

componentDidMount() {
this.generatePDFPromise = makeCancellablePromise(User.generateStatementPDF(this.yearMonth));
}

componentWillUnmount() {
if (!this.generatePDFPromise) {
processDownload(yearMonth) {
if (this.props.walletStatement.isGenerating) {
return;
}

this.generatePDFPromise.cancel();
this.generatePDFPromise = null;
}

processDownload(yearMonth) {
if (this.state.isDownloading) {
if (this.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 pdfURL = `${CONFIG.EXPENSIFY.EXPENSIFY_URL}secure?secureType=pdfreport&filename=${fileName}&downloadName=${downloadFileName}`;
fileDownload(pdfURL, downloadFileName);
return;
}

this.setState({
isDownloading: true,
});

if (!this.props.walletStatement[yearMonth] || this.props.walletStatement.isGenerating) {
Growl.show(this.props.translate('common.genericErrorMessage'), CONST.GROWL.ERROR, 5000);
this.setState({
isDownloading: false,
});
} else {
const fileName = `Expensify_Statement_${yearMonth}.pdf`;
const pdfURL = `${CONFIG.EXPENSIFY.EXPENSIFY_URL}secure?secureType=pdfreport&filename=${this.props.walletStatement[yearMonth]}&downloadName=${fileName}`;
fileDownload(pdfURL, fileName).then(() => {
this.setState({
isDownloading: false,
});
});
}
Growl.show(this.props.translate('statementPage.generatingPDF'), CONST.GROWL.NOTICE, 5000);
User.generateStatementPDF(this.yearMonth);
}

render() {
Expand All @@ -106,13 +87,15 @@ class WalletStatementPage extends React.Component {
<ScreenWrapper>
<HeaderWithCloseButton
title={Str.recapitalize(title)}
shouldShowDownloadButton
shouldShowDownloadButton={!this.props.network.isOffline || this.props.walletStatement.isGenerating}
onCloseButtonPress={() => Navigation.dismissModal(true)}
onDownloadButtonPress={() => this.processDownload(this.yearMonth)}
/>
<WalletStatementModal
statementPageURL={url}
/>
<FullPageOfflineBlockingView>
<WalletStatementModal
statementPageURL={url}
/>
</FullPageOfflineBlockingView>
</ScreenWrapper>
);
}
Expand All @@ -131,4 +114,5 @@ export default compose(
key: ONYXKEYS.WALLET_STATEMENT,
},
}),
withNetwork(),
)(WalletStatementPage);