2
0
mirror of https://github.com/frappe/books.git synced 2025-01-26 16:48:28 +00:00
books/src/pages/DataImport.vue

631 lines
17 KiB
Vue
Raw Normal View History

2022-02-21 16:26:57 +05:30
<template>
2022-03-01 14:16:12 +05:30
<div class="flex flex-col overflow-hidden w-full">
2022-02-21 16:26:57 +05:30
<PageHeader>
<template #title>
<h1 class="text-2xl font-bold">
{{ t`Data Import` }}
</h1>
</template>
<template #actions>
2022-02-22 14:13:56 +05:30
<DropdownWithActions
class="ml-2"
:actions="actions"
v-if="(canCancel || importType) && !complete"
2022-02-22 14:13:56 +05:30
/>
2022-02-21 16:26:57 +05:30
<Button
v-if="importType && !complete"
2022-02-22 14:13:56 +05:30
type="primary"
class="text-sm ml-2"
@click="handlePrimaryClick"
>{{ primaryLabel }}</Button
2022-02-21 16:26:57 +05:30
>
</template>
</PageHeader>
<div
class="flex px-8 mt-2 text-base w-full flex-col gap-8"
v-if="!complete"
>
2022-02-22 14:13:56 +05:30
<!-- Type selector -->
<div class="flex flex-row justify-start items-center w-full gap-2">
2022-02-22 14:13:56 +05:30
<FormControl
:df="importableDf"
input-class="bg-gray-100 text-gray-900 text-base"
class="w-40"
:value="importType"
size="small"
@change="setImportType"
/>
2022-02-22 14:13:56 +05:30
<p
class="text-base text-base ml-2"
:class="fileName ? 'text-gray-900 font-semibold' : 'text-gray-700'"
>
<span v-if="fileName" class="font-normal"
>{{ t`Selected file` }}
</span>
{{ helperText }}{{ fileName ? ',' : '' }}
<span v-if="fileName" class="font-normal">
2022-02-22 18:33:21 +05:30
{{ t`verify the imported data and click on` }} </span
>{{ ' ' }}<span v-if="fileName">{{ t`Import Data` }}</span>
2022-02-22 14:13:56 +05:30
</p>
</div>
2022-02-23 17:15:20 +05:30
<!-- Settings -->
<div v-if="fileName" class="">
<h2 class="text-lg font-semibold">{{ t`Importer Settings` }}</h2>
<div class="mt-4 flex gap-2">
<div
v-if="file && isSubmittable"
class="
justify-center
items-center
gap-2
flex
justify-between
items-center
bg-gray-100
px-2
rounded
text-gray-900
w-40
"
>
<p>{{ frappe.t`Submit on Import` }}</p>
<FormControl
size="small"
input-class="bg-gray-100"
:df="{
fieldname: 'shouldSubmit',
fieldtype: 'Check',
}"
:value="Number(importer.shouldSubmit)"
@change="(value) => (importer.shouldSubmit = !!value)"
/>
</div>
<div
class="
flex flex-row
justify-center
items-center
justify-center
items-center
gap-2
flex
justify-between
items-center
bg-gray-100
pl-2
rounded
text-gray-900
w-40
"
>
<p class="text-gray-900">{{ t`Label Index` }}</p>
<input
type="number"
class="
bg-gray-100
outline-none
focus:bg-gray-200
px-2
py-1
rounded-md
w-10
text-right
"
min="1"
:max="importer.csv.length - 1"
:value="labelIndex + 1"
@change="setLabelIndex"
/>
</div>
<button
class="w-28 bg-gray-100 focus:bg-gray-200 rounded-md"
v-if="canReset"
@click="
() => {
importer.initialize(0, true);
canReset = false;
}
"
>
<span class="text-gray-900">
{{ t`Reset` }}
</span>
</button>
2022-02-23 17:15:20 +05:30
</div>
</div>
2022-02-22 14:13:56 +05:30
<!-- Label Assigner -->
2022-02-23 11:33:15 +05:30
<div v-if="fileName" class="pb-4">
2022-02-22 14:13:56 +05:30
<h2 class="text-lg font-semibold">{{ t`Assign Imported Labels` }}</h2>
<div
class="gap-2 mt-4 grid grid-flow-col overflow-x-scroll no-scrollbar"
>
<div
v-for="(f, k) in importer.assignableLabels"
:key="'assigner-' + f + '-' + k"
>
2022-02-23 11:33:15 +05:30
<p class="text-gray-600 text-sm mb-1">
{{ f }}
<span
v-if="importer.requiredMap[f] && !importer.assignedMap[f]"
class="text-red-400"
>*</span
>
</p>
<FormControl
size="small"
class="w-28"
input-class="bg-gray-100"
:df="getAssignerField(f)"
:value="importer.assignedMap[f] ?? ''"
@change="(v) => onAssignedChange(f, v)"
/>
</div>
2022-02-21 16:26:57 +05:30
</div>
2022-02-23 11:33:15 +05:30
<p
class="text-red-400 text-sm mt-1 -mb-1 p-0 h-0"
2022-02-23 17:15:20 +05:30
v-if="isRequiredUnassigned"
2022-02-23 11:33:15 +05:30
>
{{ t`* required fields` }}
</p>
2022-02-21 16:26:57 +05:30
</div>
2022-02-22 14:13:56 +05:30
<!-- Data Verifier -->
<div v-if="fileName">
2022-02-22 18:33:21 +05:30
<h2 class="-mt-4 text-lg font-semibold pb-1">
2022-02-22 14:13:56 +05:30
{{ t`Verify Imported Data` }}
</h2>
2022-02-22 18:33:21 +05:30
<div class="overflow-scroll mt-4 pb-4">
<!-- Column Name Rows -->
<div
class="grid grid-flow-col pb-4 border-b gap-2 sticky top-0 bg-white"
style="width: fit-content"
v-if="importer.columnLabels.length > 0"
>
<div class="w-4 h-4" />
<p
v-for="(c, i) in importer.columnLabels"
class="px-2 w-28 font-semibold text-gray-600"
:key="'column-' + i"
>
{{ c }}
</p>
</div>
<div v-else>
<p class="text-gray-600">
{{ t`No labels have been assigned.` }}
</p>
</div>
<!-- Data Rows -->
<div
v-if="importer.columnLabels.length > 0"
style="max-height: 400px"
2022-02-22 18:33:21 +05:30
>
<div
class="grid grid-flow-col mt-4 pb-4 border-b gap-2 items-center"
style="width: fit-content"
v-for="(r, i) in assignedMatrix"
:key="'matrix-row-' + i"
>
<button
2022-03-01 14:16:12 +05:30
class="
w-4
h-4
text-gray-600
hover:text-gray-900
cursor-pointer
outline-none
"
@click="
() => {
importer.dropRow(i);
canReset = true;
}
"
2022-02-22 18:33:21 +05:30
>
<FeatherIcon name="x" />
</button>
<input
v-for="(c, j) in r"
type="text"
class="
w-28
text-gray-900
px-2
py-1
outline-none
rounded
focus:bg-gray-200
"
@change="
(e) => {
onValueChange(e, i, j);
canReset = true;
}
"
2022-02-22 18:33:21 +05:30
:key="'matrix-cell-' + i + '-' + j"
:value="c"
/>
</div>
<button
class="
text-gray-600
hover:text-gray-900
flex flex-row
w-full
mt-4
outline-none
"
@click="
() => {
importer.addRow();
canReset = true;
}
"
>
<FeatherIcon name="plus" class="w-4 h-4" />
<p class="pl-4">
{{ t`Add Row` }}
</p>
</button>
2022-02-22 18:33:21 +05:30
</div>
</div>
2022-02-22 14:13:56 +05:30
</div>
2022-02-21 16:26:57 +05:30
</div>
<div v-if="complete" class="flex justify-center h-full items-center">
<div
class="
flex flex-col
justify-center
items-center
gap-8
rounded-lg
shadow-md
p-6
"
style="width: 450px"
>
<h2 class="text-xl font-semibold mt-4">{{ t`Import Success` }} 🎉</h2>
<p class="text-lg text-center">
{{ t`Successfully created the following ${names.length} entries:` }}
</p>
<div class="max-h-96 overflow-y-scroll">
<div
v-for="(n, i) in names"
:key="'name-' + i"
class="grid grid-cols-2 gap-2 border-b pb-2 mb-2 pr-4 text-lg w-60"
style="grid-template-columns: 2rem auto"
>
<p class="text-right">{{ i + 1 }}.</p>
<p>
{{ n }}
</p>
</div>
</div>
<div class="flex w-full justify-between">
<Button type="secondary" class="text-sm w-32" @click="clear">{{
t`Import More`
}}</Button>
<Button type="primary" class="text-sm w-32" @click="showMe">{{
t`Show Me`
}}</Button>
</div>
</div>
</div>
<div
v-if="!importType"
2022-03-01 14:16:12 +05:30
class="flex justify-center h-full w-full items-center mb-16"
>
<HowTo link="https://youtu.be/ukHAgcnVxTQ">
{{ t`How to Use Data Import?` }}
</HowTo>
</div>
2022-03-01 14:16:12 +05:30
<Loading
2022-03-01 14:27:51 +05:30
v-if="isMakingEntries"
:open="isMakingEntries"
2022-03-01 14:16:12 +05:30
:percent="percentLoading"
:message="messageLoading"
/>
2022-02-21 16:26:57 +05:30
</div>
</template>
<script>
2022-02-23 11:33:15 +05:30
import Button from '@/components/Button.vue';
2022-02-21 16:26:57 +05:30
import FormControl from '@/components/Controls/FormControl';
2022-02-23 11:33:15 +05:30
import DropdownWithActions from '@/components/DropdownWithActions.vue';
import FeatherIcon from '@/components/FeatherIcon.vue';
import HowTo from '@/components/HowTo.vue';
2022-02-21 16:26:57 +05:30
import PageHeader from '@/components/PageHeader.vue';
2022-02-21 17:57:01 +05:30
import { importable, Importer } from '@/dataImport';
2022-02-21 16:26:57 +05:30
import { IPC_ACTIONS } from '@/messages';
import { getSavePath, saveData, showMessageDialog } from '@/utils';
2022-02-23 11:33:15 +05:30
import { ipcRenderer } from 'electron';
import frappe from 'frappe';
2022-03-01 14:16:12 +05:30
import Loading from '../components/Loading.vue';
2022-02-21 16:26:57 +05:30
export default {
2022-02-22 18:33:21 +05:30
components: {
PageHeader,
FormControl,
Button,
DropdownWithActions,
FeatherIcon,
HowTo,
2022-03-01 14:16:12 +05:30
Loading,
2022-02-22 18:33:21 +05:30
},
2022-02-21 16:26:57 +05:30
data() {
return {
canReset: false,
complete: false,
names: ['Bat', 'Baseball', 'Other Shit'],
2022-02-21 16:26:57 +05:30
file: null,
2022-02-21 17:57:01 +05:30
importer: null,
2022-02-21 16:26:57 +05:30
importType: '',
2022-03-01 14:27:51 +05:30
isMakingEntries: false,
2022-03-01 14:16:12 +05:30
percentLoading: 0,
messageLoading: '',
2022-02-21 16:26:57 +05:30
};
},
computed: {
labelIndex() {
return this.importer.labelIndex;
2022-02-23 17:15:20 +05:30
},
2022-02-23 11:33:15 +05:30
requiredUnassigned() {
2022-02-23 17:15:20 +05:30
return this.importer.assignableLabels.filter(
(k) => this.importer.requiredMap[k] && !this.importer.assignedMap[k]
);
},
isRequiredUnassigned() {
return this.requiredUnassigned.length > 0;
2022-02-23 11:33:15 +05:30
},
2022-02-22 18:33:21 +05:30
assignedMatrix() {
return this.importer.assignedMatrix;
},
2022-02-22 14:13:56 +05:30
actions() {
const actions = [];
const secondaryAction = {
2022-02-22 14:13:56 +05:30
component: {
template: '<span>{{ t`Save Template` }}</span>',
2022-02-22 14:13:56 +05:30
},
condition: () => true,
action: this.handleSecondaryClick,
2022-02-22 14:13:56 +05:30
};
actions.push(secondaryAction);
2022-02-22 14:13:56 +05:30
if (this.file) {
actions.push({
component: {
template: '<span>{{ t`Change File` }}</span>',
},
condition: () => true,
action: this.selectFile,
});
}
const cancelAction = {
2022-02-22 14:13:56 +05:30
component: {
template: '<span class="text-red-700" >{{ t`Cancel` }}</span>',
2022-02-22 14:13:56 +05:30
},
condition: () => true,
action: this.clear,
2022-02-22 14:13:56 +05:30
};
actions.push(cancelAction);
return actions;
2022-02-22 14:13:56 +05:30
},
2022-02-21 16:26:57 +05:30
fileName() {
if (!this.file) {
return '';
}
return this.file.name;
},
helperText() {
if (!this.importType) {
return this.t`Set an Import Type`;
} else if (!this.fileName) {
return this.t`Select a file for import`;
}
return this.fileName;
},
primaryLabel() {
return this.file ? this.t`Import Data` : this.t`Select File`;
},
isSubmittable() {
const doctype = this.importer?.doctype;
if (doctype) {
return frappe.models[doctype].isSubmittable ?? false;
}
return false;
},
2022-02-21 16:26:57 +05:30
importableDf() {
return {
fieldname: 'importType',
label: this.t`Import Type`,
fieldtype: 'AutoComplete',
placeholder: 'Import Type',
getList: () => importable.map((i) => frappe.models[i].label),
};
},
labelDoctypeMap() {
return importable
.map((i) => ({
name: i,
label: frappe.models[i].label,
}))
.reduce((acc, { name, label }) => {
acc[label] = name;
return acc;
}, {});
},
canCancel() {
return !!(this.file || this.importType);
},
},
deactivated() {
2022-02-25 14:14:25 +05:30
if (!this.complete) {
return;
}
2022-03-01 14:16:12 +05:30
this.clear();
},
2022-02-21 16:26:57 +05:30
methods: {
showMe() {
const doctype = this.importer.doctype;
this.clear();
this.$router.push(`/list/${doctype}`);
},
clear() {
2022-02-21 16:26:57 +05:30
this.file = null;
this.names = [];
2022-02-21 17:57:01 +05:30
this.importer = null;
2022-02-21 16:26:57 +05:30
this.importType = '';
this.complete = false;
this.canReset = false;
this.isMakingEntries = false;
this.percentLoading = 0;
this.messageLoading = '';
2022-02-21 16:26:57 +05:30
},
2022-02-21 17:57:01 +05:30
handlePrimaryClick() {
if (!this.file) {
this.selectFile();
return;
}
this.importData();
},
handleSecondaryClick() {
2022-02-22 18:33:21 +05:30
if (!this.importer) {
2022-02-21 17:57:01 +05:30
return;
}
2022-02-22 18:33:21 +05:30
this.saveTemplate();
2022-02-21 17:57:01 +05:30
},
2022-02-23 17:15:20 +05:30
setLabelIndex(e) {
const labelIndex = (e.target.value ?? 1) - 1;
this.importer.initialize(labelIndex);
},
2022-02-21 17:57:01 +05:30
async saveTemplate() {
const template = this.importer.template;
const templateName = this.importType + ' ' + this.t`Template`;
const { cancelled, filePath } = await getSavePath(templateName, 'csv');
if (cancelled || filePath === '') {
return;
}
await saveData(template, filePath);
},
2022-02-22 14:13:56 +05:30
getAssignerField(targetLabel) {
const assigned = this.importer.assignedMap[targetLabel];
return {
fieldname: 'assignerField',
label: targetLabel,
placeholder: `Select Label`,
fieldtype: 'Select',
options: [
'',
...(assigned ? [assigned] : []),
...this.importer.unassignedLabels,
],
default: assigned ?? '',
};
},
onAssignedChange(target, value) {
this.importer.assignedMap[target] = value;
},
2022-02-22 18:33:21 +05:30
onValueChange(event, i, j) {
this.importer.updateValue(event.target.value, i, j);
},
async importData() {
2022-03-01 14:27:51 +05:30
if (this.isMakingEntries || this.complete) {
return;
}
2022-02-23 17:15:20 +05:30
if (this.isRequiredUnassigned) {
showMessageDialog({
message: this.t`Required Fields not Assigned`,
description: this
.t`Please assign the following fields ${this.requiredUnassigned.join(
', '
)}`,
});
return;
}
if (this.importer.assignedMatrix.length === 0) {
showMessageDialog({
message: this.t`No Data to Import`,
description: this.t`Please select a file with data to import.`,
});
return;
}
2022-03-01 14:16:12 +05:30
const { success, names, message } = await this.importer.importData(
this.setLoadingStatus
);
if (!success) {
showMessageDialog({
message: this.t`Import Failed`,
description: message,
});
return;
}
this.names = names;
this.complete = true;
},
2022-02-21 17:57:01 +05:30
setImportType(importType) {
2022-02-22 18:33:21 +05:30
if (this.importType) {
this.clear();
2022-02-22 18:33:21 +05:30
}
2022-02-21 17:57:01 +05:30
this.importType = importType;
this.importer = new Importer(this.labelDoctypeMap[this.importType]);
},
2022-03-01 14:16:12 +05:30
setLoadingStatus(isMakingEntries, entriesMade, totalEntries) {
2022-03-01 14:27:51 +05:30
this.isMakingEntries = isMakingEntries;
2022-03-01 14:16:12 +05:30
this.percentLoading = entriesMade / totalEntries;
this.messageLoading = isMakingEntries
? `${entriesMade} entries made out of ${totalEntries}...`
: '';
},
2022-02-21 17:57:01 +05:30
async selectFile() {
2022-02-21 16:26:57 +05:30
const options = {
title: this.t`Select File`,
properties: ['openFile'],
filters: [{ name: 'CSV', extensions: ['csv'] }],
};
const { success, canceled, filePath, data, name } =
await ipcRenderer.invoke(IPC_ACTIONS.GET_FILE, options);
2022-02-22 14:13:56 +05:30
if (!success && !canceled) {
showMessageDialog({ message: this.t`File selection failed.` });
2022-02-21 16:26:57 +05:30
}
if (!success || canceled) {
return;
}
2022-02-22 14:13:56 +05:30
const text = new TextDecoder().decode(data);
const isValid = this.importer.selectFile(text);
if (!isValid) {
showMessageDialog({
message: this.t`Bad import data.`,
description: this.t`Could not select file.`,
2022-02-22 14:13:56 +05:30
});
return;
}
2022-02-21 16:26:57 +05:30
this.file = {
name,
filePath,
2022-02-22 14:13:56 +05:30
text,
2022-02-21 16:26:57 +05:30
};
},
},
};
</script>