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

Transactions: Use server based export for both sync download on browser and async email export #10049

Closed
wants to merge 13 commits into from
Closed
57 changes: 28 additions & 29 deletions client/transactions/list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { useDispatch } from '@wordpress/data';
import { useMemo } from '@wordpress/element';
import { __, _n, sprintf } from '@wordpress/i18n';

import {
TableCard,
Search,
Expand All @@ -20,9 +21,9 @@
updateQueryString,
} from '@woocommerce/navigation';
import {
downloadCSVFile,

Check warning on line 24 in client/transactions/list/index.tsx

View workflow job for this annotation

GitHub Actions / JS linting

'downloadCSVFile' is defined but never used
generateCSVDataFromTable,

Check warning on line 25 in client/transactions/list/index.tsx

View workflow job for this annotation

GitHub Actions / JS linting

'generateCSVDataFromTable' is defined but never used
generateCSVFileName,

Check warning on line 26 in client/transactions/list/index.tsx

View workflow job for this annotation

GitHub Actions / JS linting

'generateCSVFileName' is defined but never used
} from '@woocommerce/csv-export';
import apiFetch from '@wordpress/api-fetch';

Expand Down Expand Up @@ -96,6 +97,11 @@
labelInCsv?: string;
}

interface TransactionExportResponse {
download_url?: string;
exported_transactions: number;
}

const getPaymentSourceDetails = ( txn: Transaction ) => {
if ( ! txn.source_identifier ) {
return <Fragment></Fragment>;
Expand Down Expand Up @@ -658,7 +664,7 @@
window.confirm( confirmMessage )
) {
try {
await apiFetch( {
const response = await apiFetch< TransactionExportResponse >( {
path: getTransactionsCSV( {
userEmail,
locale,
Expand All @@ -684,16 +690,23 @@
method: 'POST',
} );

createNotice(
'success',
sprintf(
__(
'Your export will be emailed to %s',
'woocommerce-payments'
),
userEmail
)
);
if ( response?.download_url ) {
const link = document.createElement( 'a' );
link.href = response.download_url;
link.click();
jessy-p marked this conversation as resolved.
Show resolved Hide resolved
} else {
// Show email notification if no direct download URL
createNotice(
'success',
sprintf(
__(
'Your export will be emailed to %s',
'woocommerce-payments'
),
userEmail
)
);
}
} catch {
createNotice(
'error',
Expand All @@ -712,32 +725,18 @@
// We destructure page and path to get the right params.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { page, path, ...params } = getQuery();
const downloadType = totalRows > rows.length ? 'endpoint' : 'browser';

recordEvent( 'wcpay_transactions_download_csv_click', {
location: props.depositId ? 'deposit_details' : 'transactions',
download_type: downloadType,
download_type: 'endpoint',
exported_transactions: rows.length,
total_transactions: transactionsSummary.count,
} );

if ( 'endpoint' === downloadType ) {
if ( ! isDefaultSiteLanguage() && ! isExportModalDismissed() ) {
setCSVExportModalOpen( true );
} else {
endpointExport( '' );
}
if ( ! isDefaultSiteLanguage() && ! isExportModalDismissed() ) {
setCSVExportModalOpen( true );
} else {
const columnsToDisplayInCsv = columnsToDisplay.map( ( column ) => {
if ( column.labelInCsv ) {
return { ...column, label: column.labelInCsv };
}
return column;
} );
downloadCSVFile(
generateCSVFileName( title, params ),
generateCSVDataFromTable( columnsToDisplayInCsv, rows )
);
endpointExport( '' );
}

setIsDownloading( false );
Expand Down
Loading