2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 11:29:03 +00:00

Added feature to download json data

This commit is contained in:
Piyush Singhania 2021-12-09 15:55:37 +05:30 committed by Alan
parent 482f7875bb
commit b4c6faae17
5 changed files with 34 additions and 0 deletions

View File

@ -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
* ------------------------------*/

View File

@ -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',
};

View File

@ -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() {

9
src/saveReportAsJson.js Normal file
View File

@ -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);
});
}

View File

@ -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 [];