2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00

feat: mark required fields

This commit is contained in:
18alantom 2022-02-23 11:33:15 +05:30
parent 15bb03136a
commit 5f4cbe1795
2 changed files with 68 additions and 29 deletions

View File

@ -17,7 +17,7 @@ type Exclusion = {
}; };
type Map = { type Map = {
[key: string]: string; [key: string]: string | boolean;
}; };
interface TemplateField { interface TemplateField {
@ -105,6 +105,7 @@ export class Importer {
parsedLabels: string[] = []; parsedLabels: string[] = [];
parsedValues: string[][] = []; parsedValues: string[][] = [];
assignedMap: Map = {}; // target: import assignedMap: Map = {}; // target: import
requiredMap: Map = {};
constructor(doctype: string) { constructor(doctype: string) {
this.doctype = doctype; this.doctype = doctype;
@ -115,10 +116,25 @@ export class Importer {
acc[k] = ''; acc[k] = '';
return acc; return acc;
}, {}); }, {});
this.requiredMap = this.templateFields.reduce((acc: Map, k) => {
acc[k.label] = k.required;
return acc;
}, {});
} }
get assignableLabels() { get assignableLabels() {
return Object.keys(this.map); const req: string[] = [];
const nreq: string[] = [];
Object.keys(this.map).forEach((k) => {
if (this.requiredMap[k]) {
req.push(k);
return;
}
nreq.push(k);
});
return [...req, ...nreq];
} }
get unassignedLabels() { get unassignedLabels() {
@ -129,25 +145,30 @@ export class Importer {
} }
get columnLabels() { get columnLabels() {
const assigned: string[] = []; const req: string[] = [];
const unassigned: string[] = []; const nreq: string[] = [];
this.assignableLabels.forEach((k) => { this.assignableLabels.forEach((k) => {
if (this.assignedMap[k]) { if (!this.assignedMap[k]) {
assigned.push(k);
return; return;
} }
unassigned.push(k);
if (this.requiredMap[k]) {
req.push(k);
return;
}
nreq.push(k);
}); });
return [...assigned]; return [...req, ...nreq];
} }
get assignedMatrix() { get assignedMatrix() {
this.indices = this.columnLabels this.indices = this.columnLabels
.map((k) => this.assignedMap[k]) .map((k) => this.assignedMap[k])
.filter(Boolean) .filter(Boolean)
.map((k) => this.parsedLabels.indexOf(k)); .map((k) => this.parsedLabels.indexOf(k as string));
const rows = this.parsedValues.length; const rows = this.parsedValues.length;
const cols = this.columnLabels.length; const cols = this.columnLabels.length;

View File

@ -47,21 +47,34 @@
</div> </div>
<!-- Label Assigner --> <!-- Label Assigner -->
<div v-if="fileName"> <div v-if="fileName" class="pb-4">
<h2 class="text-lg font-semibold">{{ t`Assign Imported Labels` }}</h2> <h2 class="text-lg font-semibold">{{ t`Assign Imported Labels` }}</h2>
<div class="gap-2 mt-4 grid grid-flow-col overflow-x-scroll pb-4"> <div class="gap-2 mt-4 grid grid-flow-col overflow-x-scroll">
<FormControl <div v-for="(f, k) in importer.assignableLabels" :key="f + '-' + k">
:show-label="true" <p class="text-gray-600 text-sm mb-1">
size="small" {{ f }}
class="w-28" <span
input-class="bg-gray-100" v-if="importer.requiredMap[f] && !importer.assignedMap[f]"
v-for="(f, k) in importer.assignableLabels" class="text-red-400"
:df="getAssignerField(f)" >*</span
:value="importer.assignedMap[f] ?? ''" >
@change="(v) => onAssignedChange(f, v)" </p>
:key="f + '-' + k" <FormControl
/> size="small"
class="w-28"
input-class="bg-gray-100"
:df="getAssignerField(f)"
:value="importer.assignedMap[f] ?? ''"
@change="(v) => onAssignedChange(f, v)"
/>
</div>
</div> </div>
<p
class="text-red-400 text-sm mt-1 -mb-1 p-0 h-0"
v-if="requiredUnassigned"
>
{{ t`* required fields` }}
</p>
</div> </div>
<!-- Data Verifier --> <!-- Data Verifier -->
@ -133,16 +146,16 @@
</div> </div>
</template> </template>
<script> <script>
import FormControl from '@/components/Controls/FormControl';
import PageHeader from '@/components/PageHeader.vue';
import { importable, Importer } from '@/dataImport';
import frappe from 'frappe';
import Button from '@/components/Button.vue'; import Button from '@/components/Button.vue';
import { ipcRenderer } from 'electron'; import FormControl from '@/components/Controls/FormControl';
import { IPC_ACTIONS } from '@/messages';
import { getSavePath, saveData, showToast } from '@/utils';
import DropdownWithActions from '@/components/DropdownWithActions.vue'; import DropdownWithActions from '@/components/DropdownWithActions.vue';
import FeatherIcon from '@/components/FeatherIcon.vue'; import FeatherIcon from '@/components/FeatherIcon.vue';
import PageHeader from '@/components/PageHeader.vue';
import { importable, Importer } from '@/dataImport';
import { IPC_ACTIONS } from '@/messages';
import { getSavePath, saveData, showToast } from '@/utils';
import { ipcRenderer } from 'electron';
import frappe from 'frappe';
export default { export default {
components: { components: {
PageHeader, PageHeader,
@ -159,6 +172,11 @@ export default {
}; };
}, },
computed: { computed: {
requiredUnassigned() {
return this.importer.assignableLabels
.filter((k) => this.importer.requiredMap[k])
.some((k) => !this.importer.assignedMap[k]);
},
assignedMatrix() { assignedMatrix() {
return this.importer.assignedMatrix; return this.importer.assignedMatrix;
}, },