Skip to content

Commit

Permalink
Revert "Generate email from table (finos#3325)" (finos#3334)
Browse files Browse the repository at this point in the history
  • Loading branch information
akphi authored Jul 10, 2024
1 parent b547759 commit 2b91a8b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 205 deletions.
2 changes: 0 additions & 2 deletions packages/legend-application-repl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@
"dependencies": {
"@ag-grid-community/client-side-row-model": "32.0.0",
"@ag-grid-community/core": "32.0.0",
"@ag-grid-community/csv-export": "32.0.0",
"@ag-grid-community/react": "32.0.0",
"@ag-grid-community/styles": "32.0.0",
"@ag-grid-enterprise/clipboard": "32.0.0",
"@ag-grid-enterprise/core": "32.0.0",
"@ag-grid-enterprise/excel-export": "32.0.0",
"@ag-grid-enterprise/menu": "32.0.0",
"@ag-grid-enterprise/range-selection": "32.0.0",
"@ag-grid-enterprise/row-grouping": "32.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ import type {
DataCubeConfigurationColorKey,
} from '../../../stores/dataCube/core/DataCubeConfiguration.js';
import { generateBaseGridOptions } from '../../../stores/dataCube/grid/DataCubeGridQuerySnapshotAnalyzer.js';
import { ModuleRegistry } from '@ag-grid-community/core';
import { CsvExportModule } from '@ag-grid-community/csv-export';
import { ExcelExportModule } from '@ag-grid-enterprise/excel-export';

// NOTE: This is a workaround to prevent ag-grid license key check from flooding the console screen
// with its stack trace in Chrome.
Expand All @@ -64,8 +61,6 @@ console.error = (message?: unknown, ...agrs: unknown[]): void => {
console.log(`%c ${message}`, 'color: silver'); // eslint-disable-line no-console
};

ModuleRegistry.registerModules(ExcelExportModule);

function textColorStyle(
key: DataCubeConfigurationColorKey,
configuration: DataCubeConfiguration,
Expand Down Expand Up @@ -335,14 +330,12 @@ const DataCubeGridClient = observer(() => {
modules={[
// community
ClientSideRowModelModule,
CsvExportModule,
// enterprise
ServerSideRowModelModule,
RowGroupingModule,
MenuModule,
ClipboardModule,
RangeSelectionModule,
ExcelExportModule,
]}
{...generateBaseGridOptions(dataCube)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export function buildGridMenu(
const result: (string | MenuItemDef)[] = [
{
name: 'Export',
menuItem: WIP_GridMenuItem,
cssClasses: ['!opacity-100'],
subMenu: [
{
name: 'HTML',
Expand All @@ -66,15 +68,15 @@ export function buildGridMenu(
},
{
name: 'Excel',
action: () => {
dataCube.grid.generateExcelFile();
},
menuItem: WIP_GridMenuItem,
cssClasses: ['!opacity-100'],
disabled: true,
},
{
name: 'CSV',
action: () => {
dataCube.grid.generateCSVFile();
},
menuItem: WIP_GridMenuItem,
cssClasses: ['!opacity-100'],
disabled: true,
},
'separator',
{
Expand All @@ -87,31 +89,33 @@ export function buildGridMenu(
},
{
name: 'Email',
menuItem: WIP_GridMenuItem,
cssClasses: ['!opacity-100'],
subMenu: [
{
name: 'HTML',
action: () => {
dataCube.grid.generateEmail(true, true);
},
menuItem: WIP_GridMenuItem,
cssClasses: ['!opacity-100'],
disabled: true,
},
{
name: 'Plain Text',
action: () => {
dataCube.grid.generateEmail(false, true);
},
menuItem: WIP_GridMenuItem,
cssClasses: ['!opacity-100'],
disabled: true,
},
'separator',
{
name: 'HTML Attachment',
action: () => {
dataCube.grid.generateEmail(true, true);
},
menuItem: WIP_GridMenuItem,
cssClasses: ['!opacity-100'],
disabled: true,
},
{
name: 'Plain Text',
action: () => {
dataCube.grid.generateEmail(false, true);
},
menuItem: WIP_GridMenuItem,
cssClasses: ['!opacity-100'],
disabled: true,
},
{
name: 'PDF Attachment',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@
* limitations under the License.
*/

import {
ContentType,
downloadFileUsingDataURI,
guaranteeNonNullable,
hashArray,
} from '@finos/legend-shared';
import { guaranteeNonNullable, hashArray } from '@finos/legend-shared';
import { action, makeObservable, observable, runInAction } from 'mobx';
import type { GridApi } from '@ag-grid-community/core';
import type { DataCubeState } from '../DataCubeState.js';
Expand Down Expand Up @@ -71,10 +66,6 @@ export class DataCubeGridState extends DataCubeQuerySnapshotSubscriber {

scrollHintText: observable,
setScrollHintText: action,

generateCSVFile: action,
generateExcelFile: action,
generateEmail: action,
});

this.datasourceConfiguration = new DataCubeGridDatasourceConfiguration({});
Expand Down Expand Up @@ -148,170 +139,4 @@ export class DataCubeGridState extends DataCubeQuerySnapshotSubscriber {
await this.dataCube.replStore.client.getGridClientLicenseKey(),
);
}

generateCSVFile = () => {
const params = {
fileName: 'csv_data.csv',
};
if (this._client) {
this._client.exportDataAsCsv(params);
} else {
console.error('Grid API not set');
}
};

generateExcelFile = () => {
if (!this._client) {
throw new Error('Grid API is not initialized.');
}
const params = {
fileName: 'excel_data.xlsx',
processCellCallbacks: (cell: any) => {
return cell ? cell.toString() : '';
},
};
this._client.exportDataAsExcel(params);
};

blobToBase64 = (blob: Blob): Promise<string> => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => {
const result = reader.result;
if (typeof result === 'string') {
const base64Data = result.split(',')[1];
if (base64Data) {
resolve(base64Data);
} else {
console.error('Base64 conversion failed: Invalid base64 data.');
reject(
new Error('Failed to convert blob to base64. Base64 is invalid.'),
);
}
} else {
console.error(
'Base64 conversion failed: FileReader result is not a string.',
);
reject(
new Error(
'Failed to convert blob to base64. FileReader result is not a string.',
),
);
}
};
reader.onerror = (error) => {
console.error('Base64 conversion failed: FileReader error.', error);
reject(
new Error('Failed to convert blob to base64: FileReader error.'),
);
};
reader.readAsDataURL(blob);
});
};

generateEmail = async (isHtml: boolean, includeAttachments: boolean) => {
try {
const textContent =
'Please find the attached CSV and Excel(.xlsx) files.';
const htmlContent = `
<html>
<body>
<p>${textContent}</p>
<body>
</html>
`;

const emailBody = isHtml ? htmlContent : textContent;
const contentType = isHtml ? 'text/html' : 'text/plain';
const boundaryMixed = 'boundary----------123456789----------987654321';
const boundaryAlternative =
'boundary----------0987654321----------1234567890';

let emlContent = `
From:
To:
Subject: subject of the some files generated
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="${boundaryMixed}"
--${boundaryMixed}
Content-Type: multipart/alternative; boundary="${boundaryAlternative}"
--${boundaryAlternative}
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit
${textContent}
--${boundaryAlternative}
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: 7bit
${htmlContent}
--${boundaryAlternative}--
`;

if (includeAttachments) {
const csvContent =
this._client?.getDataAsCsv({
fileName: 'csv_data.csv',
columnSeparator: ',',
suppressQuotes: true,
}) ?? '';
const csvBlob = new Blob([csvContent], { type: 'text/csv' });

const xlsxContent = this._client?.getDataAsExcel({
fileName: 'excel_data.xlsx',
processCellCallback: (cell: any) => {
return cell ? cell.toString() : '';
},
});
let xlsxBlob;
if (xlsxContent instanceof Blob) {
xlsxBlob = xlsxContent;
} else if (typeof xlsxContent === 'string') {
xlsxBlob = new Blob([xlsxContent], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
});
} else {
throw new Error('Failed to generate Excel content.');
}

emlContent += `
--${boundaryMixed}
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; name="excel_data.xlsx"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="excel_data.xlsx"
${await this.blobToBase64(xlsxBlob)}
--${boundaryMixed}
Content-Type: text/csv; name="csv_data.csv"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="csv_data.csv"
${await this.blobToBase64(csvBlob)};
--${boundaryMixed}--
`;
} else {
emlContent += `
Content-Type: ${contentType}; charset= "UTF-8"
Content-Transfer-Encoding: 7bit
${emailBody}
`;
}

downloadFileUsingDataURI(
'emlFile.eml',
emlContent,
ContentType.MESSAGE_RFC822,
); //NEED TO ADD MESSAGE_RFC822 to CONTENT-TYPE
} catch (error) {
console.error('Error creating EML file:', error);
}
};
}
2 changes: 0 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2477,12 +2477,10 @@ __metadata:
dependencies:
"@ag-grid-community/client-side-row-model": "npm:32.0.0"
"@ag-grid-community/core": "npm:32.0.0"
"@ag-grid-community/csv-export": "npm:32.0.0"
"@ag-grid-community/react": "npm:32.0.0"
"@ag-grid-community/styles": "npm:32.0.0"
"@ag-grid-enterprise/clipboard": "npm:32.0.0"
"@ag-grid-enterprise/core": "npm:32.0.0"
"@ag-grid-enterprise/excel-export": "npm:32.0.0"
"@ag-grid-enterprise/menu": "npm:32.0.0"
"@ag-grid-enterprise/range-selection": "npm:32.0.0"
"@ag-grid-enterprise/row-grouping": "npm:32.0.0"
Expand Down

0 comments on commit 2b91a8b

Please sign in to comment.