2
0
mirror of https://github.com/frappe/books.git synced 2024-11-08 14:50:56 +00:00

incr: remove src imports from ipcCalls.ts

- simplify ipcCalls
This commit is contained in:
18alantom 2023-07-10 12:27:13 +05:30
parent 62a920ced7
commit ea079db92e
10 changed files with 136 additions and 103 deletions

View File

@ -5,11 +5,11 @@ import { ModelNameEnum } from 'models/types';
import { codeStateMap } from 'regional/in'; import { codeStateMap } from 'regional/in';
import { ExportExtention } from 'reports/types'; import { ExportExtention } from 'reports/types';
import { showDialog } from 'src/utils/interactive'; import { showDialog } from 'src/utils/interactive';
import { getSavePath } from 'src/utils/ipcCalls';
import { invertMap } from 'utils'; import { invertMap } from 'utils';
import { getCsvData, saveExportData } from '../commonExporter'; import { getCsvData, saveExportData } from '../commonExporter';
import { BaseGSTR } from './BaseGSTR'; import { BaseGSTR } from './BaseGSTR';
import { TransferTypeEnum } from './types'; import { TransferTypeEnum } from './types';
import { getSavePath } from 'src/utils/ui';
const GST = { const GST = {
'GST-0': 0, 'GST-0': 0,

View File

@ -1,11 +1,12 @@
import { t } from 'fyo'; import { t } from 'fyo';
import { Action } from 'fyo/model/types'; import { Action } from 'fyo/model/types';
import { Verb } from 'fyo/telemetry/types'; import { Verb } from 'fyo/telemetry/types';
import { getSavePath, saveData, showExportInFolder } from 'src/utils/ipcCalls'; import { saveData } from 'src/utils/ipcCalls';
import { getIsNullOrUndef } from 'utils'; import { getIsNullOrUndef } from 'utils';
import { generateCSV } from 'utils/csvParser'; import { generateCSV } from 'utils/csvParser';
import { Report } from './Report'; import { Report } from './Report';
import { ExportExtention, ReportCell } from './types'; import { ExportExtention, ReportCell } from './types';
import { getSavePath, showExportInFolder } from 'src/utils/ui';
interface JSONExport { interface JSONExport {
columns: { fieldname: string; label: string }[]; columns: { fieldname: string; label: string }[];

View File

@ -54,6 +54,7 @@ import { initializeInstance } from './utils/initialization';
import * as injectionKeys from './utils/injectionKeys'; import * as injectionKeys from './utils/injectionKeys';
import { showDialog } from './utils/interactive'; import { showDialog } from './utils/interactive';
import { checkDbAccess, checkForUpdates } from './utils/ipcCalls'; import { checkDbAccess, checkForUpdates } from './utils/ipcCalls';
import { setLanguageMap } from './utils/language';
import { updateConfigFiles } from './utils/misc'; import { updateConfigFiles } from './utils/misc';
import { updatePrintTemplates } from './utils/printTemplates'; import { updatePrintTemplates } from './utils/printTemplates';
import { Search } from './utils/search'; import { Search } from './utils/search';
@ -147,6 +148,7 @@ export default defineComponent({
await this.setDeskRoute(); await this.setDeskRoute();
await fyo.telemetry.start(true); await fyo.telemetry.start(true);
await checkForUpdates(); await checkForUpdates();
await setLanguageMap();
this.dbPath = filePath; this.dbPath = filePath;
this.companyName = (await fyo.getValue( this.companyName = (await fyo.getValue(
ModelNameEnum.AccountingSettings, ModelNameEnum.AccountingSettings,

View File

@ -95,7 +95,7 @@ import {
getExportTableFields, getExportTableFields,
getJsonExportData, getJsonExportData,
} from 'src/utils/export'; } from 'src/utils/export';
import { getSavePath, saveData, showExportInFolder } from 'src/utils/ipcCalls'; import { saveData } from 'src/utils/ipcCalls';
import { ExportField, ExportFormat, ExportTableField } from 'src/utils/types'; import { ExportField, ExportFormat, ExportTableField } from 'src/utils/types';
import { QueryFilter } from 'utils/db/types'; import { QueryFilter } from 'utils/db/types';
import { defineComponent, PropType } from 'vue'; import { defineComponent, PropType } from 'vue';
@ -105,6 +105,7 @@ import Int from './Controls/Int.vue';
import Select from './Controls/Select.vue'; import Select from './Controls/Select.vue';
import FormHeader from './FormHeader.vue'; import FormHeader from './FormHeader.vue';
import { Verb } from 'fyo/telemetry/types'; import { Verb } from 'fyo/telemetry/types';
import { getSavePath, showExportInFolder } from 'src/utils/ui';
interface ExportWizardData { interface ExportWizardData {
useListFilters: boolean; useListFilters: boolean;

View File

@ -246,13 +246,9 @@ import Loading from 'src/components/Loading.vue';
import Modal from 'src/components/Modal.vue'; import Modal from 'src/components/Modal.vue';
import { fyo } from 'src/initFyo'; import { fyo } from 'src/initFyo';
import { showDialog } from 'src/utils/interactive'; import { showDialog } from 'src/utils/interactive';
import { import { getDbList } from 'src/utils/ipcCalls';
deleteDb,
getDbList,
getSavePath,
getSelectedFilePath,
} from 'src/utils/ipcCalls';
import { updateConfigFiles } from 'src/utils/misc'; import { updateConfigFiles } from 'src/utils/misc';
import { deleteDb, getSavePath, getSelectedFilePath } from 'src/utils/ui';
import type { ConfigFilesWithModified } from 'utils/types'; import type { ConfigFilesWithModified } from 'utils/types';
import { defineComponent } from 'vue'; import { defineComponent } from 'vue';

View File

@ -388,10 +388,10 @@ import PageHeader from 'src/components/PageHeader.vue';
import { Importer, TemplateField, getColumnLabel } from 'src/importer'; import { Importer, TemplateField, getColumnLabel } from 'src/importer';
import { fyo } from 'src/initFyo'; import { fyo } from 'src/initFyo';
import { showDialog } from 'src/utils/interactive'; import { showDialog } from 'src/utils/interactive';
import { getSavePath, saveData } from 'src/utils/ipcCalls'; import { saveData } from 'src/utils/ipcCalls';
import { docsPathMap } from 'src/utils/misc'; import { docsPathMap } from 'src/utils/misc';
import { docsPathRef } from 'src/utils/refs'; import { docsPathRef } from 'src/utils/refs';
import { selectTextFile } from 'src/utils/ui'; import { getSavePath, selectTextFile } from 'src/utils/ui';
import { defineComponent } from 'vue'; import { defineComponent } from 'vue';
import Loading from '../components/Loading.vue'; import Loading from '../components/Loading.vue';

View File

@ -233,23 +233,23 @@ import ShortcutKeys from 'src/components/ShortcutKeys.vue';
import { handleErrorWithDialog } from 'src/errorHandling'; import { handleErrorWithDialog } from 'src/errorHandling';
import { shortcutsKey } from 'src/utils/injectionKeys'; import { shortcutsKey } from 'src/utils/injectionKeys';
import { showDialog, showToast } from 'src/utils/interactive'; import { showDialog, showToast } from 'src/utils/interactive';
import { getSavePath } from 'src/utils/ipcCalls';
import { docsPathMap } from 'src/utils/misc'; import { docsPathMap } from 'src/utils/misc';
import { import {
PrintTemplateHint, PrintTemplateHint,
baseTemplate, baseTemplate,
getPrintTemplatePropHints, getPrintTemplatePropHints,
getPrintTemplatePropValues, getPrintTemplatePropValues,
} from 'src/utils/printTemplates'; } from 'src/utils/printTemplates';
import { docsPathRef, showSidebar } from 'src/utils/refs'; import { docsPathRef, showSidebar } from 'src/utils/refs';
import { DocRef, PrintValues } from 'src/utils/types'; import { DocRef, PrintValues } from 'src/utils/types';
import { import {
ShortcutKey, ShortcutKey,
focusOrSelectFormControl, focusOrSelectFormControl,
getActionsForDoc, getActionsForDoc,
getDocFromNameIfExistsElseNew, getDocFromNameIfExistsElseNew,
openSettings, getSavePath,
selectTextFile, openSettings,
selectTextFile,
} from 'src/utils/ui'; } from 'src/utils/ui';
import { useDocShortcuts } from 'src/utils/vueUtils'; import { useDocShortcuts } from 'src/utils/vueUtils';
import { getMapFromList } from 'utils/index'; import { getMapFromList } from 'utils/index';

View File

@ -2,8 +2,12 @@
* Utils that make ipcRenderer calls. * Utils that make ipcRenderer calls.
*/ */
const { ipcRenderer } = require('electron'); const { ipcRenderer } = require('electron');
import { t } from 'fyo'; import type {
import { BaseError } from 'fyo/utils/errors'; OpenDialogOptions,
OpenDialogReturnValue,
SaveDialogOptions,
SaveDialogReturnValue,
} from 'electron';
import type { BackendResponse } from 'utils/ipc/types'; import type { BackendResponse } from 'utils/ipc/types';
import { IPC_ACTIONS, IPC_CHANNELS, IPC_MESSAGES } from 'utils/messages'; import { IPC_ACTIONS, IPC_CHANNELS, IPC_MESSAGES } from 'utils/messages';
import type { import type {
@ -13,9 +17,6 @@ import type {
SelectFileReturn, SelectFileReturn,
TemplateFile, TemplateFile,
} from 'utils/types'; } from 'utils/types';
import { showDialog, showToast } from './interactive';
import { setLanguageMap } from './language';
import type { OpenDialogReturnValue } from 'electron';
export function reloadWindow() { export function reloadWindow() {
return ipcRenderer.send(IPC_MESSAGES.RELOAD_MAIN_WINDOW); return ipcRenderer.send(IPC_MESSAGES.RELOAD_MAIN_WINDOW);
@ -29,12 +30,11 @@ export async function getLanguageMap(code: string) {
}; };
} }
export async function getSelectedFilePath(): Promise<OpenDialogReturnValue> { export async function getOpenFilePath(options: OpenDialogOptions) {
return (await ipcRenderer.invoke(IPC_ACTIONS.GET_OPEN_FILEPATH, { return (await ipcRenderer.invoke(
title: t`Select file`, IPC_ACTIONS.GET_OPEN_FILEPATH,
properties: ['openFile'], options
filters: [{ name: 'SQLite DB File', extensions: ['db'] }], )) as OpenDialogReturnValue;
})) as OpenDialogReturnValue;
} }
export async function getTemplates(): Promise<TemplateFile[]> { export async function getTemplates(): Promise<TemplateFile[]> {
@ -61,43 +61,17 @@ export async function checkDbAccess(filePath: string) {
export async function checkForUpdates() { export async function checkForUpdates() {
await ipcRenderer.invoke(IPC_ACTIONS.CHECK_FOR_UPDATES); await ipcRenderer.invoke(IPC_ACTIONS.CHECK_FOR_UPDATES);
await setLanguageMap();
} }
export function openLink(link: string) { export function openLink(link: string) {
ipcRenderer.send(IPC_MESSAGES.OPEN_EXTERNAL, link); ipcRenderer.send(IPC_MESSAGES.OPEN_EXTERNAL, link);
} }
export async function deleteDb(filePath: string) { export async function deleteFile(filePath: string) {
const { error } = (await ipcRenderer.invoke( return (await ipcRenderer.invoke(
IPC_ACTIONS.DELETE_FILE, IPC_ACTIONS.DELETE_FILE,
filePath filePath
)) as BackendResponse; )) as BackendResponse;
if (error?.code === 'EBUSY') {
await showDialog({
title: t`Delete Failed`,
detail: t`Please restart and try again.`,
type: 'error',
});
} else if (error?.code === 'ENOENT') {
await showDialog({
title: t`Delete Failed`,
detail: t`File ${filePath} does not exist.`,
type: 'error',
});
} else if (error?.code === 'EPERM') {
await showDialog({
title: t`Cannot Delete`,
detail: t`Close Frappe Books and try manually.`,
type: 'error',
});
} else if (error) {
const err = new BaseError(500, error.message);
err.name = error.name;
err.stack = error.stack;
throw err;
}
} }
export async function saveData(data: string, savePath: string) { export async function saveData(data: string, savePath: string) {
@ -113,47 +87,21 @@ export async function makePDF(
savePath: string, savePath: string,
width: number, width: number,
height: number height: number
): Promise<void> { ): Promise<boolean> {
const success = (await ipcRenderer.invoke( return (await ipcRenderer.invoke(
IPC_ACTIONS.SAVE_HTML_AS_PDF, IPC_ACTIONS.SAVE_HTML_AS_PDF,
html, html,
savePath, savePath,
width, width,
height height
)) as boolean; )) as boolean;
if (success) {
showExportInFolder(t`Save as PDF Successful`, savePath);
} else {
showToast({ message: t`Export Failed`, type: 'error' });
}
} }
export function showExportInFolder(message: string, filePath: string) { export async function getSaveFilePath(options: SaveDialogOptions) {
showToast({ return (await ipcRenderer.invoke(
message, IPC_ACTIONS.GET_SAVE_FILEPATH,
actionText: t`Open Folder`, options
type: 'success', )) as SaveDialogReturnValue;
action: () => {
showItemInFolder(filePath);
},
});
}
export async function getSavePath(name: string, extention: string) {
const response = (await ipcRenderer.invoke(IPC_ACTIONS.GET_SAVE_FILEPATH, {
title: t`Select Folder`,
defaultPath: `${name}.${extention}`,
})) as { canceled: boolean; filePath?: string };
const canceled = response.canceled;
let filePath = response.filePath;
if (filePath && !filePath.endsWith(extention) && filePath !== ':memory:') {
filePath = `${filePath}.${extention}`;
}
return { canceled, filePath };
} }
export async function getDbList() { export async function getDbList() {

View File

@ -1,13 +1,18 @@
import { Fyo } from 'fyo'; import { Fyo, t } from 'fyo';
import { Doc } from 'fyo/model/doc'; import { Doc } from 'fyo/model/doc';
import { Invoice } from 'models/baseModels/Invoice/Invoice'; import { Invoice } from 'models/baseModels/Invoice/Invoice';
import { ModelNameEnum } from 'models/types'; import { ModelNameEnum } from 'models/types';
import { FieldTypeEnum, Schema, TargetField } from 'schemas/types'; import { FieldTypeEnum, Schema, TargetField } from 'schemas/types';
import { getValueMapFromList } from 'utils/index'; import { getValueMapFromList } from 'utils/index';
import { TemplateFile } from 'utils/types'; import { TemplateFile } from 'utils/types';
import { getSavePath, getTemplates, makePDF } from './ipcCalls'; import { showToast } from './interactive';
import { getTemplates, makePDF } from './ipcCalls';
import { PrintValues } from './types'; import { PrintValues } from './types';
import { getDocFromNameIfExistsElseNew } from './ui'; import {
getDocFromNameIfExistsElseNew,
getSavePath,
showExportInFolder,
} from './ui';
export type PrintTemplateHint = { export type PrintTemplateHint = {
[key: string]: string | PrintTemplateHint | PrintTemplateHint[]; [key: string]: string | PrintTemplateHint | PrintTemplateHint[];
@ -223,13 +228,18 @@ export async function getPathAndMakePDF(
width: number, width: number,
height: number height: number
) { ) {
const { filePath } = await getSavePath(name, 'pdf'); const { filePath: savePath } = await getSavePath(name, 'pdf');
if (!filePath) { if (!savePath) {
return; return;
} }
const html = constructPrintDocument(innerHTML); const html = constructPrintDocument(innerHTML);
await makePDF(html, filePath, width, height); const success = await makePDF(html, savePath, width, height);
if (success) {
showExportInFolder(t`Save as PDF Successful`, savePath);
} else {
showToast({ message: t`Export Failed`, type: 'error' });
}
} }
function constructPrintDocument(innerHTML: string) { function constructPrintDocument(innerHTML: string) {

View File

@ -6,7 +6,12 @@ import { t } from 'fyo';
import type { Doc } from 'fyo/model/doc'; import type { Doc } from 'fyo/model/doc';
import { Action } from 'fyo/model/types'; import { Action } from 'fyo/model/types';
import { getActions } from 'fyo/utils'; import { getActions } from 'fyo/utils';
import { getDbError, LinkValidationError, ValueError } from 'fyo/utils/errors'; import {
BaseError,
getDbError,
LinkValidationError,
ValueError,
} from 'fyo/utils/errors';
import { Invoice } from 'models/baseModels/Invoice/Invoice'; import { Invoice } from 'models/baseModels/Invoice/Invoice';
import { PurchaseInvoice } from 'models/baseModels/PurchaseInvoice/PurchaseInvoice'; import { PurchaseInvoice } from 'models/baseModels/PurchaseInvoice/PurchaseInvoice';
import { SalesInvoice } from 'models/baseModels/SalesInvoice/SalesInvoice'; import { SalesInvoice } from 'models/baseModels/SalesInvoice/SalesInvoice';
@ -18,11 +23,18 @@ import { Schema } from 'schemas/types';
import { handleErrorWithDialog } from 'src/errorHandling'; import { handleErrorWithDialog } from 'src/errorHandling';
import { fyo } from 'src/initFyo'; import { fyo } from 'src/initFyo';
import router from 'src/router'; import router from 'src/router';
import { assertIsType } from 'utils/index';
import { SelectFileOptions } from 'utils/types'; import { SelectFileOptions } from 'utils/types';
import { RouteLocationRaw } from 'vue-router'; import { RouteLocationRaw } from 'vue-router';
import { evaluateHidden } from './doc'; import { evaluateHidden } from './doc';
import { showDialog, showToast } from './interactive'; import { showDialog, showToast } from './interactive';
import { selectFile } from './ipcCalls'; import {
deleteFile,
getOpenFilePath,
getSaveFilePath,
selectFile,
showItemInFolder,
} from './ipcCalls';
import { showSidebar } from './refs'; import { showSidebar } from './refs';
import { import {
ActionGroup, ActionGroup,
@ -31,7 +43,6 @@ import {
ToastOptions, ToastOptions,
UIGroupedFields, UIGroupedFields,
} from './types'; } from './types';
import { assertIsType } from 'utils/index';
export const toastDurationMap = { short: 2_500, long: 5_000 } as const; export const toastDurationMap = { short: 2_500, long: 5_000 } as const;
@ -953,3 +964,67 @@ export const paperSizeMap: Record<
height: -1, height: -1,
}, },
}; };
export function showExportInFolder(message: string, filePath: string) {
showToast({
message,
actionText: t`Open Folder`,
type: 'success',
action: () => {
showItemInFolder(filePath);
},
});
}
export async function deleteDb(filePath: string) {
const { error } = await deleteFile(filePath);
if (error?.code === 'EBUSY') {
await showDialog({
title: t`Delete Failed`,
detail: t`Please restart and try again.`,
type: 'error',
});
} else if (error?.code === 'ENOENT') {
await showDialog({
title: t`Delete Failed`,
detail: t`File ${filePath} does not exist.`,
type: 'error',
});
} else if (error?.code === 'EPERM') {
await showDialog({
title: t`Cannot Delete`,
detail: t`Close Frappe Books and try manually.`,
type: 'error',
});
} else if (error) {
const err = new BaseError(500, error.message);
err.name = error.name;
err.stack = error.stack;
throw err;
}
}
export async function getSelectedFilePath() {
return getOpenFilePath({
title: t`Select file`,
properties: ['openFile'],
filters: [{ name: 'SQLite DB File', extensions: ['db'] }],
});
}
export async function getSavePath(name: string, extention: string) {
const response = await getSaveFilePath({
title: t`Select folder`,
defaultPath: `${name}.${extention}`,
});
const canceled = response.canceled;
let filePath = response.filePath;
if (filePath && !filePath.endsWith(extention) && filePath !== ':memory:') {
filePath = `${filePath}.${extention}`;
}
return { canceled, filePath };
}