From b4c6faae1717d59bde9cb589489be4e2f4c1cfe7 Mon Sep 17 00:00:00 2001 From: Piyush Singhania Date: Thu, 9 Dec 2021 15:55:37 +0530 Subject: [PATCH] Added feature to download json data --- src/background.js | 5 +++++ src/messages.js | 1 + src/pages/Report.vue | 15 +++++++++++++++ src/saveReportAsJson.js | 9 +++++++++ src/utils.js | 4 ++++ 5 files changed, 34 insertions(+) create mode 100644 src/saveReportAsJson.js diff --git a/src/background.js b/src/background.js index ecb74d6c..b3dca790 100644 --- a/src/background.js +++ b/src/background.js @@ -16,6 +16,7 @@ import path from 'path'; import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'; import { IPC_ACTIONS, IPC_MESSAGES } from './messages'; import saveHtmlAsPdf from './saveHtmlAsPdf'; +import saveReportAsJson from './saveReportAsJson'; const isDevelopment = process.env.NODE_ENV !== 'production'; const isMac = process.platform === 'darwin'; @@ -193,6 +194,10 @@ ipcMain.handle(IPC_ACTIONS.SAVE_HTML_AS_PDF, async (event, html, savePath) => { return await saveHtmlAsPdf(html, savePath); }); +ipcMain.handle(IPC_ACTIONS.SAVE_REPORT_AS_JSON, async (event, data, savePath) => { + return await saveReportAsJson(data, savePath); +}); + /* ------------------------------ * Register app lifecycle methods * ------------------------------*/ diff --git a/src/messages.js b/src/messages.js index f0bcadd4..2fcc1ce9 100644 --- a/src/messages.js +++ b/src/messages.js @@ -23,4 +23,5 @@ export const DB_CONN_FAILURE = { INVALID_FILE: 'invalid-file', CANT_OPEN: 'cant-open', CANT_CONNECT: 'cant-connect', + SAVE_REPORT_AS_JSON: 'save-report-as-json', }; diff --git a/src/pages/Report.vue b/src/pages/Report.vue index 22fe68a8..c37a2544 100644 --- a/src/pages/Report.vue +++ b/src/pages/Report.vue @@ -102,6 +102,7 @@ import Row from '@/components/Row'; import WithScroll from '@/components/WithScroll'; import FormControl from '@/components/Controls/FormControl'; import reportViewConfig from '@/../reports/view'; +import { makeJSON } from '@/utils'; import { ipcRenderer } from 'electron'; import { IPC_ACTIONS } from '@/messages'; @@ -275,6 +276,20 @@ export default { }, async downloadAsJson() { const savePath = await this.getSavePath(); + if (!savePath) return; + let jsonData = []; + let gstRecord = {}; + let keys = this.reportData.columns; + let rows = this.reportData.rows; + rows.forEach((values) => { + keys.forEach((key) => { + gstRecord[key.fieldname] = values[key.fieldname]; + }); + jsonData.push(gstRecord); + gstRecord = {}; + }); + console.log(jsonData); + makeJSON(JSON.stringify(jsonData), savePath); console.log('download complete'); }, async getSavePath() { diff --git a/src/saveReportAsJson.js b/src/saveReportAsJson.js new file mode 100644 index 00000000..f4c5c37f --- /dev/null +++ b/src/saveReportAsJson.js @@ -0,0 +1,9 @@ +import fs from 'fs'; +import { shell } from 'electron'; + +export default async function makeJSON(data, savePath) { + fs.writeFile(savePath, data, (error) => { + if (error) throw error; + return shell.openPath(savePath); + }); +} diff --git a/src/utils.js b/src/utils.js index d359423f..a9748fc7 100644 --- a/src/utils.js +++ b/src/utils.js @@ -173,6 +173,10 @@ export async function makePDF(html, savePath) { ipcRenderer.invoke(IPC_ACTIONS.SAVE_HTML_AS_PDF, html, savePath); } +export async function makeJSON(data, savePath) { + ipcRenderer.invoke(IPC_ACTIONS.SAVE_REPORT_AS_JSON, data, savePath); +} + export function getActionsForDocument(doc) { if (!doc) return [];