2
0
mirror of https://github.com/frappe/books.git synced 2025-01-11 02:36:14 +00:00
books/src/pages/DatabaseSelector.vue

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

213 lines
5.4 KiB
Vue
Raw Normal View History

2018-10-23 12:42:36 +00:00
<template>
<div
2022-04-22 11:02:03 +00:00
class="py-10 flex-1 bg-white flex justify-center items-center"
:class="{
'pointer-events-none': loadingDatabase,
'window-drag': platform !== 'Windows',
}"
>
<WindowControls v-if="platform === 'Mac'" class="absolute top-6 left-5" />
2022-04-22 11:02:03 +00:00
<div
class="w-full w-600 shadow rounded-lg border relative"
style="height: 700px"
>
<!-- Welcome to Frappe Books -->
<div class="px-6 py-10">
<h1 class="text-2xl font-semibold select-none">
{{ t`Welcome to Frappe Books` }}
</h1>
<p class="text-gray-600 text-base select-none">
{{
t`Create a new file or select an existing one from your computer`
}}
</p>
</div>
<hr />
<!-- New File (Blue Icon) -->
<div
@click="newDatabase"
class="
px-6
h-18
flex flex-row
items-center
cursor-pointer
gap-4
p-2
hover:bg-gray-100
"
>
<div class="w-8 h-8 rounded-full bg-blue-500 relative flex-center">
<feather-icon name="plus" class="text-white w-5 h-5" />
</div>
<div>
<p class="font-medium">
{{ t`New File` }}
</p>
<p class="text-sm text-gray-600">
{{ t`Create a new file and store it in your computer.` }}
</p>
</div>
</div>
<!-- Existing File (Green Icon) -->
<div
@click="existingDatabase"
class="
px-6
h-18
flex flex-row
items-center
cursor-pointer
gap-4
p-2
hover:bg-gray-100
"
>
<div class="w-8 h-8 rounded-full bg-green-500 relative flex-center">
<feather-icon name="upload" class="w-4 h-4 text-white" />
</div>
<div>
<p class="font-medium">
{{ t`Existing File` }}
</p>
<p class="text-sm text-gray-600">
{{ t`Load an existing .db file from your computer.` }}
</p>
2018-10-23 12:42:36 +00:00
</div>
</div>
<hr />
<!-- File List -->
<div class="overflow-scroll" style="max-height: 340px">
<div
class="
h-18
px-6
flex
gap-4
items-center
hover:bg-gray-100
cursor-pointer
"
v-for="(file, i) in files"
:key="file.dbPath"
@click="selectFile(file)"
>
<div
class="
w-8
h-8
rounded-full
flex
justify-center
items-center
bg-gray-200
text-gray-500
font-semibold
text-base
"
>
{{ i + 1 }}
</div>
<div>
<p class="font-medium">
{{ file.companyName }}
</p>
<p class="text-sm text-gray-600">
2022-04-22 11:02:03 +00:00
{{ file.modified }}
</p>
</div>
2018-10-23 12:42:36 +00:00
</div>
</div>
<hr v-if="files?.length" />
<!-- Language Selector -->
2022-04-22 11:02:03 +00:00
<div
class="w-full flex justify-end absolute px-6 py-6"
style="top: 100%; transform: translateY(-100%)"
>
<LanguageSelector class="w-40" />
</div>
</div>
2018-10-23 12:42:36 +00:00
</div>
</template>
<script>
import { ipcRenderer } from 'electron';
2022-03-30 10:57:45 +00:00
import fs from 'fs';
2022-04-22 11:02:03 +00:00
import { cloneDeep } from 'lodash';
2022-03-30 10:57:45 +00:00
import { DateTime } from 'luxon';
2022-04-20 06:38:47 +00:00
import LanguageSelector from 'src/components/Controls/LanguageSelector.vue';
import WindowControls from 'src/components/WindowControls.vue';
import { fyo } from 'src/initFyo';
2022-04-22 11:02:03 +00:00
import { getSavePath } from 'src/utils/ipcCalls';
import { IPC_ACTIONS } from 'utils/messages';
2018-10-23 12:42:36 +00:00
export default {
name: 'DatabaseSelector',
2022-04-22 11:02:03 +00:00
emits: ['file-selected'],
data() {
return {
loadingDatabase: false,
fileSelectedFrom: null,
files: [],
};
},
mounted() {
this.setFiles();
window.ds = this;
},
2018-10-23 12:42:36 +00:00
methods: {
setFiles() {
const files = cloneDeep(fyo.config.get('files', []));
this.files = files.filter(({ dbPath }) => fs.existsSync(dbPath));
2022-04-22 11:02:03 +00:00
for (const file of this.files) {
const stats = fs.statSync(file.dbPath);
2022-04-22 11:02:03 +00:00
file.modified = DateTime.fromJSDate(stats.mtime).toRelative();
}
},
async newDatabase() {
this.fileSelectedFrom = 'New File';
2022-04-22 11:02:03 +00:00
const { filePath, canceled } = await getSavePath('books', 'db');
if (canceled || !filePath) {
return;
}
this.connectToDatabase(filePath, true);
2018-10-23 12:42:36 +00:00
},
async existingDatabase() {
this.fileSelectedFrom = 'Existing File';
const filePath = (
await ipcRenderer.invoke(IPC_ACTIONS.GET_OPEN_FILEPATH, {
2022-02-09 07:17:54 +00:00
title: this.t`Select file`,
properties: ['openFile'],
filters: [{ name: 'SQLite DB File', extensions: ['db'] }],
})
)?.filePaths?.[0];
this.connectToDatabase(filePath);
},
async selectFile(file) {
this.fileSelectedFrom = file;
await this.connectToDatabase(file.dbPath);
},
2022-04-22 11:02:03 +00:00
async connectToDatabase(filePath, isNew) {
if (!filePath) {
return;
}
2022-04-22 11:02:03 +00:00
if (isNew) {
this.$emit('file-selected', filePath, isNew);
return;
2021-11-05 19:41:39 +00:00
}
2022-04-22 11:02:03 +00:00
this.$emit('file-selected', filePath, !!isNew);
},
},
components: { LanguageSelector, WindowControls },
2018-10-23 12:42:36 +00:00
};
</script>