Skip to content

Commit

Permalink
fix(ux): remove file selection on create
Browse files Browse the repository at this point in the history
  • Loading branch information
18alantom committed Jul 11, 2023
1 parent 952241b commit 23916a3
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 32 deletions.
7 changes: 7 additions & 0 deletions main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ const ipc = {
)) as ConfigFilesWithModified[];
},

async getDbDefaultPath(companyName: string) {
return (await ipcRenderer.invoke(
IPC_ACTIONS.GET_DB_DEFAULT_PATH,
companyName
)) as string;
},

async getEnv() {
return (await ipcRenderer.invoke(IPC_ACTIONS.GET_ENV)) as {
isDevelopment: boolean;
Expand Down
18 changes: 17 additions & 1 deletion main/registerIpcMainActionListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from 'electron';
import { autoUpdater } from 'electron-updater';
import { constants } from 'fs';
import fs from 'fs/promises';
import fs from 'fs-extra';
import path from 'path';
import { SelectFileOptions, SelectFileReturn } from 'utils/types';
import databaseManager from '../backend/database/manager';
Expand Down Expand Up @@ -38,6 +38,22 @@ export default function registerIpcMainActionListeners(main: Main) {
return true;
});

ipcMain.handle(
IPC_ACTIONS.GET_DB_DEFAULT_PATH,
async (_, companyName: string) => {
let root = app.getPath('documents');
if (main.isDevelopment) {
root = 'dbs';
}

const dbsPath = path.join(root, 'Frappe Books');
const backupPath = path.join(dbsPath, 'backups');
await fs.ensureDir(backupPath);

return path.join(dbsPath, `${companyName}.books.db`);
}
);

ipcMain.handle(
IPC_ACTIONS.GET_OPEN_FILEPATH,
async (_, options: OpenDialogOptions) => {
Expand Down
21 changes: 9 additions & 12 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<DatabaseSelector
v-if="activeScreen === 'DatabaseSelector'"
ref="databaseSelector"
@new-database="newDatabase"
@file-selected="fileSelected"
/>
<SetupWizard
Expand Down Expand Up @@ -136,7 +137,7 @@ export default defineComponent({
return;
}
await this.fileSelected(lastSelectedFilePath, false);
await this.fileSelected(lastSelectedFilePath);
},
async setSearcher(): Promise<void> {
this.searcher = new Search(fyo);
Expand All @@ -156,13 +157,11 @@ export default defineComponent({
await this.setSearcher();
updateConfigFiles(fyo);
},
async fileSelected(filePath: string, isNew?: boolean): Promise<void> {
newDatabase() {
this.activeScreen = Screen.SetupWizard;
},
async fileSelected(filePath: string): Promise<void> {
fyo.config.set('lastSelectedFilePath', filePath);
if (isNew) {
this.activeScreen = Screen.SetupWizard;
return;
}
if (filePath !== ':memory:' && !(await ipc.checkDbAccess(filePath))) {
await showDialog({
title: this.t`Cannot open file`,
Expand All @@ -183,12 +182,10 @@ export default defineComponent({
}
},
async setupComplete(setupWizardOptions: SetupWizardOptions): Promise<void> {
const filePath = fyo.config.get('lastSelectedFilePath');
if (typeof filePath !== 'string') {
return;
}
const companyName = setupWizardOptions.companyName;
const filePath = await ipc.getDbDefaultPath(companyName);
await setupInstance(filePath, setupWizardOptions, fyo);
fyo.config.set('lastSelectedFilePath', filePath);
await this.setDesk(filePath);
},
async showSetupWizardOrDesk(filePath: string): Promise<void> {
Expand Down
20 changes: 5 additions & 15 deletions src/pages/DatabaseSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export default defineComponent({
Modal,
Button,
},
emits: ['file-selected'],
emits: ['file-selected', 'new-database'],
data() {
return {
openModal: false,
Expand Down Expand Up @@ -363,17 +363,12 @@ export default defineComponent({
(a, b) => Date.parse(b.modified) - Date.parse(a.modified)
);
},
async newDatabase() {
newDatabase() {
if (this.creatingDemo) {
return;
}
const { filePath, canceled } = await getSavePath('books', 'db');
if (canceled || !filePath) {
return;
}
this.emitFileSelected(filePath, true);
this.$emit('new-database');
},
async existingDatabase() {
if (this.creatingDemo) {
Expand All @@ -390,17 +385,12 @@ export default defineComponent({
this.emitFileSelected(file.dbPath);
},
emitFileSelected(filePath: string, isNew?: boolean) {
emitFileSelected(filePath: string) {
if (!filePath) {
return;
}
if (isNew) {
this.$emit('file-selected', filePath, isNew);
return;
}
this.$emit('file-selected', filePath, !!isNew);
this.$emit('file-selected', filePath);
},
},
});
Expand Down
4 changes: 0 additions & 4 deletions uitest/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ const appSourcePath = path.join(root, 'dist_electron', 'build', 'main.js');
});

test('fill setup form', async (t) => {
await electronApp.evaluate(({ dialog }, filePath) => {
dialog.showSaveDialog = () =>
Promise.resolve({ canceled: false, filePath });
}, ':memory:');
await window.getByTestId('create-new-file').click();
await window.getByTestId('submit-button').waitFor();

Expand Down
1 change: 1 addition & 0 deletions utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export enum IPC_ACTIONS {
GET_DB_LIST = 'get-db-list',
GET_TEMPLATES = 'get-templates',
DELETE_FILE = 'delete-file',
GET_DB_DEFAULT_PATH = 'get-db-default-path',
// Database messages
DB_CREATE = 'db-create',
DB_CONNECT = 'db-connect',
Expand Down

0 comments on commit 23916a3

Please sign in to comment.