2
0
mirror of https://github.com/frappe/books.git synced 2024-11-09 23:30:56 +00:00

fix(ux): check db access before opening

This commit is contained in:
18alantom 2023-06-29 11:02:09 +05:30
parent 16ba36982f
commit c1b3687221
5 changed files with 37 additions and 5 deletions

View File

@ -7,6 +7,7 @@ import {
ipcMain,
} from 'electron';
import { autoUpdater } from 'electron-updater';
import { constants } from 'fs';
import fs from 'fs/promises';
import path from 'path';
import { SelectFileOptions, SelectFileReturn } from 'utils/types';
@ -27,6 +28,16 @@ import {
import { saveHtmlAsPdf } from './saveHtmlAsPdf';
export default function registerIpcMainActionListeners(main: Main) {
ipcMain.handle(IPC_ACTIONS.CHECK_DB_ACCESS, async (_, filePath: string) => {
try {
await fs.access(filePath, constants.W_OK | constants.R_OK);
} catch (err) {
return false;
}
return true;
});
ipcMain.handle(
IPC_ACTIONS.GET_OPEN_FILEPATH,
async (_, options: OpenDialogOptions) => {

View File

@ -52,7 +52,8 @@ import './styles/index.css';
import { connectToDatabase, dbErrorActionSymbols } from './utils/db';
import { initializeInstance } from './utils/initialization';
import * as injectionKeys from './utils/injectionKeys';
import { checkForUpdates } from './utils/ipcCalls';
import { showDialog } from './utils/interactive';
import { checkDbAccess, checkForUpdates } from './utils/ipcCalls';
import { updateConfigFiles } from './utils/misc';
import { updatePrintTemplates } from './utils/printTemplates';
import { Search } from './utils/search';
@ -161,6 +162,18 @@ export default defineComponent({
return;
}
if (!(await checkDbAccess(filePath))) {
await showDialog({
title: this.t`Cannot open file`,
type: 'error',
detail: this
.t`Frappe Books does not have access to the selected file: ${filePath}`,
});
fyo.config.set('lastSelectedFilePath', null);
return;
}
try {
await this.showSetupWizardOrDesk(filePath);
} catch (error) {

View File

@ -37,10 +37,10 @@
<div>
<p class="font-medium">
{{ t`New File` }}
{{ t`New Company` }}
</p>
<p class="text-sm text-gray-600">
{{ t`Create a new file and store it in your computer.` }}
{{ t`Create a new company and store it on your computer.` }}
</p>
</div>
</div>
@ -56,10 +56,10 @@
</div>
<div>
<p class="font-medium">
{{ t`Existing File` }}
{{ t`Existing Company` }}
</p>
<p class="text-sm text-gray-600">
{{ t`Load an existing .db file from your computer.` }}
{{ t`Load an existing company from your computer.` }}
</p>
</div>
</div>

View File

@ -51,6 +51,13 @@ export async function selectFile(
)) as SelectFileReturn;
}
export async function checkDbAccess(filePath: string) {
return (await ipcRenderer.invoke(
IPC_ACTIONS.CHECK_DB_ACCESS,
filePath
)) as boolean;
}
export async function checkForUpdates() {
await ipcRenderer.invoke(IPC_ACTIONS.CHECK_FOR_UPDATES);
await setLanguageMap();

View File

@ -19,6 +19,7 @@ export enum IPC_ACTIONS {
SEND_ERROR = 'send-error',
GET_LANGUAGE_MAP = 'get-language-map',
CHECK_FOR_UPDATES = 'check-for-updates',
CHECK_DB_ACCESS = 'check-db-access',
SELECT_FILE = 'select-file',
GET_CREDS = 'get-creds',
GET_DB_LIST = 'get-db-list',