Skip to content

Commit

Permalink
Refactor: Simplify CSV export to always use endpoint download
Browse files Browse the repository at this point in the history
  • Loading branch information
Jessy committed Dec 17, 2024
1 parent eb54bb9 commit 30ad58b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 132 deletions.
43 changes: 3 additions & 40 deletions client/deposits/list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -312,48 +312,11 @@ export const DepositsList = (): JSX.Element => {

const onDownload = async () => {
setIsDownloading( true );
const downloadType = totalRows > rows.length ? 'endpoint' : 'browser';

if ( 'endpoint' === downloadType ) {
if ( ! isDefaultSiteLanguage() && ! isExportModalDismissed() ) {
setCSVExportModalOpen( true );
} else {
endpointExport( '' );
}
if ( ! isDefaultSiteLanguage() && ! isExportModalDismissed() ) {
setCSVExportModalOpen( true );
} else {
const params = getQuery();

const csvColumns = [
{
...columns[ 0 ],
label: __( 'Payout Id', 'woocommerce-payments' ),
},
...columns.slice( 1 ),
];

const csvRows = rows.map( ( row ) => [
row[ 0 ],
{
...row[ 1 ],
value: dateI18n(
'Y-m-d',
moment.utc( row[ 1 ].value ).toISOString(),
true
),
},
...row.slice( 2 ),
] );

downloadCSVFile(
generateCSVFileName( title, params ),
generateCSVDataFromTable( csvColumns, csvRows )
);

recordEvent( 'wcpay_deposits_download', {
exported_deposits: rows.length,
total_deposits: depositsSummary.count,
download_type: 'browser',
} );
endpointExport( '' );
}

setIsDownloading( false );
Expand Down
68 changes: 4 additions & 64 deletions client/disputes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -444,71 +444,11 @@ export const DisputesList = (): JSX.Element => {

const onDownload = async () => {
setIsDownloading( true );
const title = __( 'Disputes', 'woocommerce-payments' );
const downloadType = totalRows > rows.length ? 'endpoint' : 'browser';

if ( 'endpoint' === downloadType ) {
if ( ! isDefaultSiteLanguage() && ! isExportModalDismissed() ) {
setCSVExportModalOpen( true );
} else {
endpointExport( '' );
}

if ( ! isDefaultSiteLanguage() && ! isExportModalDismissed() ) {
setCSVExportModalOpen( true );
} else {
const csvColumns = [
{
...headers[ 0 ],
label: __( 'Dispute Id', 'woocommerce-payments' ),
},
...headers.slice( 1, -1 ), // Remove details (position 0) and action (last position) column headers.
];

const csvRows = rows.map( ( row ) => {
return [
...row.slice( 0, 3 ), // Amount, Currency, Status.
{
// Reason.
...row[ 3 ],
value:
disputeStatusMapping[ row[ 3 ].value ?? '' ]
.message,
},
{
// Source.
...row[ 4 ],
value: formatStringValue(
( row[ 4 ].value ?? '' ).toString()
),
},
...row.slice( 5, 10 ), // Order #, Customer, Email, Country.
{
// Disputed On.
...row[ 10 ],
value: dateI18n(
'Y-m-d',
moment( row[ 10 ].value ).toISOString()
),
},
{
// Respond by.
...row[ 11 ],
value: dateI18n(
'Y-m-d / g:iA',
moment( row[ 11 ].value ).toISOString()
),
},
];
} );

downloadCSVFile(
generateCSVFileName( title, getQuery() ),
generateCSVDataFromTable( csvColumns, csvRows )
);

recordEvent( 'wcpay_disputes_download', {
exported_disputes: csvRows.length,
total_disputes: disputesSummary.count,
download_type: 'browser',
} );
endpointExport( '' );
}

setIsDownloading( false );
Expand Down
31 changes: 3 additions & 28 deletions client/transactions/list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -711,35 +711,10 @@ export const TransactionsList = (
const onDownload = async () => {
setIsDownloading( true );

// 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,
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

0 comments on commit 30ad58b

Please sign in to comment.