2
0
mirror of https://github.com/frappe/books.git synced 2025-01-25 16:18:33 +00:00
books/src/pages/DatabaseSelector.vue

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

244 lines
6.8 KiB
Vue
Raw Normal View History

2018-10-23 18:12:36 +05:30
<template>
<div
2022-04-22 16:32:03 +05:30
class="py-10 flex-1 bg-white flex justify-center items-center"
:class="{
'pointer-events-none': loadingDatabase,
'window-drag': platform !== 'Windows',
}"
>
2022-04-22 16:32:03 +05:30
<div
class="w-full w-600 shadow rounded-lg border relative"
style="height: 700px"
>
<div class="px-6 py-8">
<h1 class="text-2xl font-semibold">
{{ t`Welcome to Frappe Books` }}
</h1>
<p class="text-gray-600 text-base" v-if="!showFiles">
{{
t`Create a new file or select an existing one from your computer`
}}
</p>
<p class="text-gray-600 text-base" v-if="showFiles">
{{ t`Select a file to load the company transactions` }}
</p>
</div>
2022-04-22 16:32:03 +05:30
<div class="px-12 mt-6 window-no-drag" v-if="!showFiles">
<div class="flex">
<div
@click="newDatabase"
class="
w-1/2
border
rounded-xl
flex flex-col
items-center
py-8
px-5
cursor-pointer
hover:shadow
"
>
<div
class="w-14 h-14 rounded-full bg-blue-200 relative flex-center"
>
<div
class="w-12 h-12 absolute rounded-full bg-blue-500 flex-center"
>
<feather-icon name="plus" class="text-white w-5 h-5" />
</div>
</div>
<div class="mt-5 font-medium">
<template
v-if="loadingDatabase && fileSelectedFrom === 'New File'"
>
{{ t`Loading...` }}
</template>
<template v-else>
{{ t`New File` }}
</template>
</div>
<div class="mt-2 text-sm text-gray-600 text-center">
{{ t`Create a new file and store it in your computer.` }}
</div>
</div>
<div
@click="existingDatabase"
class="
ml-6
w-1/2
border
rounded-xl
flex flex-col
items-center
py-8
px-5
cursor-pointer
hover:shadow
"
>
<div
class="w-14 h-14 rounded-full bg-green-200 relative flex-center"
>
<div class="w-12 h-12 rounded-full bg-green-500 flex-center">
<feather-icon name="upload" class="w-4 h-4 text-white" />
</div>
</div>
<div class="mt-5 font-medium">
<template
v-if="loadingDatabase && fileSelectedFrom === 'Existing File'"
>
{{ t`Loading...` }}
</template>
<template v-else>
{{ t`Existing File` }}
</template>
</div>
<div class="mt-2 text-sm text-gray-600 text-center">
{{ t`Load an existing .db file from your computer.` }}
</div>
</div>
2018-10-23 18:12:36 +05:30
</div>
<a
v-if="files.length > 0"
class="text-brand text-sm mt-4 inline-block cursor-pointer"
@click="showFiles = true"
>
2022-03-30 16:27:45 +05:30
{{ t`Select from existing files` }}
</a>
</div>
<div v-if="showFiles">
<div class="px-12 mt-6">
<div
class="
py-2
px-4
text-sm
flex
justify-between
items-center
hover:bg-gray-100
cursor-pointer
border-b
"
:class="{ 'border-t': i === 0 }"
v-for="(file, i) in files"
:key="file.filePath"
@click="selectFile(file)"
>
<div class="flex items-baseline">
<span>
<template v-if="loadingDatabase && fileSelectedFrom === file">
{{ t`Loading...` }}
</template>
<template v-else>
{{ file.companyName }}
</template>
</span>
</div>
<div class="text-gray-700">
2022-04-22 16:32:03 +05:30
{{ file.modified }}
</div>
</div>
2018-10-23 18:12:36 +05:30
</div>
<div class="px-12 mt-4">
<a
class="text-brand text-sm cursor-pointer"
@click="showFiles = false"
>
2022-03-30 16:27:45 +05:30
{{ t`Select file manually` }}
</a>
</div>
2018-10-23 18:12:36 +05:30
</div>
2022-04-22 16:32:03 +05:30
<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 18:12:36 +05:30
</div>
</template>
<script>
import { ipcRenderer } from 'electron';
2022-03-30 16:27:45 +05:30
import fs from 'fs';
2022-04-22 16:32:03 +05:30
import { cloneDeep } from 'lodash';
2022-03-30 16:27:45 +05:30
import { DateTime } from 'luxon';
2022-04-20 12:08:47 +05:30
import LanguageSelector from 'src/components/Controls/LanguageSelector.vue';
import { fyo } from 'src/initFyo';
2022-04-22 16:32:03 +05:30
import { getSavePath } from 'src/utils/ipcCalls';
import { IPC_ACTIONS } from 'utils/messages';
2018-10-23 18:12:36 +05:30
export default {
name: 'DatabaseSelector',
2022-04-22 16:32:03 +05:30
emits: ['file-selected'],
data() {
return {
loadingDatabase: false,
fileSelectedFrom: null,
showFiles: false,
files: [],
};
},
mounted() {
this.setFiles();
this.showFiles = this.files.length > 0;
},
watch: {
showFiles() {
this.setFiles();
},
},
2018-10-23 18:12:36 +05:30
methods: {
setFiles() {
2022-04-22 16:32:03 +05:30
this.files = cloneDeep(fyo.config.get('files', [])).filter(
({ filePath }) => fs.existsSync(filePath)
);
for (const file of this.files) {
const stats = fs.statSync(file.filePath);
file.modified = DateTime.fromJSDate(stats.mtime).toRelative();
}
},
async newDatabase() {
this.fileSelectedFrom = 'New File';
2022-04-22 16:32:03 +05:30
const { filePath, canceled } = await getSavePath('books', 'db');
if (canceled || !filePath) {
return;
}
this.connectToDatabase(filePath, true);
2018-10-23 18:12:36 +05:30
},
async existingDatabase() {
this.fileSelectedFrom = 'Existing File';
const filePath = (
await ipcRenderer.invoke(IPC_ACTIONS.GET_OPEN_FILEPATH, {
2022-02-09 12:47:54 +05:30
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.filePath);
},
2022-04-22 16:32:03 +05:30
async connectToDatabase(filePath, isNew) {
if (!filePath) {
return;
}
2022-04-22 16:32:03 +05:30
if (isNew) {
this.$emit('file-selected', filePath, isNew);
return;
2021-11-06 01:11:39 +05:30
}
2022-04-22 16:32:03 +05:30
this.$emit('file-selected', filePath, !!isNew);
},
},
components: { LanguageSelector },
2018-10-23 18:12:36 +05:30
};
</script>