From afa261c592752b6d88d09feed1ca569dee3b9779 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Tue, 21 Dec 2021 13:24:13 +0530 Subject: [PATCH] refactor: move gstSavePath to utils --- accounting/gst.js | 30 +++++------------------------- src/initialization.js | 23 +++-------------------- src/utils.js | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 45 deletions(-) diff --git a/accounting/gst.js b/accounting/gst.js index 11650dab..eafee4ec 100644 --- a/accounting/gst.js +++ b/accounting/gst.js @@ -1,9 +1,8 @@ -import { IPC_ACTIONS } from '@/messages'; import { makeJSON, showMessageDialog } from '@/utils'; -import { ipcRenderer } from 'electron'; import frappe from 'frappejs'; import { sleep, _ } from 'frappejs/utils'; import { DateTime } from 'luxon'; +import { getSavePath } from '../src/utils'; /** * GST is a map which gives a final rate for any given gst item @@ -72,8 +71,9 @@ export async function generateGstr1Json(getReportData) { rows, filters: { transferType, toDate }, } = getReportData(); - const savePath = await getSavePath('gstr-1'); - if (!savePath) return; + + const { filePath, canceled } = await getSavePath('gstr-1', 'json'); + if (canceled || !filePath) return; const gstData = { version: 'GST3.0.4', @@ -96,7 +96,7 @@ export async function generateGstr1Json(getReportData) { await sleep(1); const jsonData = JSON.stringify(gstData); - makeJSON(jsonData, savePath); + makeJSON(jsonData, filePath); } async function generateB2bData(invoices) { @@ -161,23 +161,3 @@ async function generateB2clData(invoices) { async function generateB2csData(invoices) { return []; } - -async function getSavePath(name) { - const options = { - title: _('Select folder'), - defaultPath: `${name}.json`, - }; - - let { filePath } = await ipcRenderer.invoke( - IPC_ACTIONS.GET_SAVE_FILEPATH, - options - ); - - if (filePath) { - if (!filePath.endsWith('.json')) { - filePath = filePath + '.json'; - } - } - - return filePath; -} diff --git a/src/initialization.js b/src/initialization.js index fd7f2e6b..e2eb4772 100644 --- a/src/initialization.js +++ b/src/initialization.js @@ -1,36 +1,19 @@ import config from '@/config'; -import { ipcRenderer } from 'electron'; -import { _ } from 'frappejs'; import SQLiteDatabase from 'frappejs/backends/sqlite'; import fs from 'fs'; import models from '../models'; import regionalModelUpdates from '../models/regionalModelUpdates'; import postStart from '../server/postStart'; -import { DB_CONN_FAILURE, IPC_ACTIONS } from './messages'; +import { DB_CONN_FAILURE } from './messages'; import migrate from './migrate'; +import { getSavePath } from './utils'; export async function createNewDatabase() { - const options = { - title: _('Select folder'), - defaultPath: 'books.db', - }; - - let { canceled, filePath } = await ipcRenderer.invoke( - IPC_ACTIONS.GET_SAVE_FILEPATH, - options - ); - + const { canceled, filePath } = await getSavePath('books', 'db'); if (canceled || filePath.length === 0) { return ''; } - if (!filePath.endsWith('.db')) { - showMessageDialog({ - message: "Please select a filename ending with '.db'.", - }); - return ''; - } - if (fs.existsSync(filePath)) { fs.unlinkSync(filePath); } diff --git a/src/utils.js b/src/utils.js index a9748fc7..23c16c6e 100644 --- a/src/utils.js +++ b/src/utils.js @@ -306,3 +306,19 @@ export function fuzzyMatch(keyword, candidate) { export function openSettings(tab) { routeTo({ path: '/settings', query: { tab } }); } + +export async function getSavePath(name, extention) { + let { canceled, filePath } = await ipcRenderer.invoke( + IPC_ACTIONS.GET_SAVE_FILEPATH, + { + title: _('Select Folder'), + defaultPath: `${name}.${extention}`, + } + ); + + if (filePath && !filePath.endsWith(extention)) { + filePath = filePath + extention; + } + + return { canceled, filePath }; +}