2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 10:58:59 +00:00

fix(ux): validate number series names

- show create quick edit even if no name
- prevent barcode double entry
This commit is contained in:
18alantom 2023-05-09 11:59:07 +05:30
parent aff4e27d88
commit ff916a59d2
3 changed files with 45 additions and 9 deletions

View File

@ -1,11 +1,29 @@
import { Doc } from 'fyo/model/doc'; import { Doc } from 'fyo/model/doc';
import { ReadOnlyMap } from 'fyo/model/types'; import { ReadOnlyMap, ValidationMap } from 'fyo/model/types';
import { ValidationError } from 'fyo/utils/errors';
const invalidNumberSeries = /[/\=\?\&\%]/;
function getPaddedName(prefix: string, next: number, padZeros: number): string { function getPaddedName(prefix: string, next: number, padZeros: number): string {
return prefix + next.toString().padStart(padZeros ?? 4, '0'); return prefix + next.toString().padStart(padZeros ?? 4, '0');
} }
export default class NumberSeries extends Doc { export default class NumberSeries extends Doc {
validations: ValidationMap = {
name: (value) => {
if (typeof value !== 'string') {
return;
}
if (invalidNumberSeries.test(value)) {
throw new ValidationError(
this.fyo
.t`The following characters cannot be used ${'/, ?, &, =, %'} in a Number Series name.`
);
}
},
};
setCurrent() { setCurrent() {
let current = this.get('current') as number | null; let current = this.get('current') as number | null;

View File

@ -35,9 +35,11 @@ export default defineComponent({
return { return {
timerId: null, timerId: null,
barcode: '', barcode: '',
cooldown: '',
} as { } as {
timerId: null | ReturnType<typeof setInterval>; timerId: null | ReturnType<typeof setTimeout>;
barcode: string; barcode: string;
cooldown: string;
}; };
}, },
mounted() { mounted() {
@ -64,6 +66,17 @@ export default defineComponent({
return this.error(this.t`Invalid barcode value ${barcode}.`); return this.error(this.t`Invalid barcode value ${barcode}.`);
} }
/**
* Between two entries of the same item, this adds
* a cooldown period of 100ms. This is to prevent
* double entry.
*/
if (this.cooldown === barcode) {
return;
}
this.cooldown = barcode;
setTimeout(() => (this.cooldown = ''), 100);
const items = (await this.fyo.db.getAll('Item', { const items = (await this.fyo.db.getAll('Item', {
filters: { barcode }, filters: { barcode },
fields: ['name'], fields: ['name'],
@ -97,12 +110,10 @@ export default defineComponent({
return await this.setItemFromBarcode(); return await this.setItemFromBarcode();
} }
if (this.timerId !== null) { this.clearInterval();
clearInterval(this.timerId);
}
this.barcode += key; this.barcode += key;
this.timerId = setInterval(async () => { this.timerId = setTimeout(async () => {
await this.setItemFromBarcode(); await this.setItemFromBarcode();
this.barcode = ''; this.barcode = '';
}, 20); }, 20);
@ -115,9 +126,15 @@ export default defineComponent({
await this.selectItem(this.barcode); await this.selectItem(this.barcode);
this.barcode = ''; this.barcode = '';
if (this.timerId !== null) { this.clearInterval();
clearInterval(this.timerId); },
clearInterval() {
if (this.timerId === null) {
return;
} }
clearInterval(this.timerId);
this.timerId = null;
}, },
error(message: string) { error(message: string) {
showToast({ type: 'error', message }); showToast({ type: 'error', message });

View File

@ -131,7 +131,8 @@ export default {
}, },
async openNewDoc() { async openNewDoc() {
const schemaName = this.df.target; const schemaName = this.df.target;
const name = this.linkValue; const name =
this.linkValue || fyo.doc.getTemporaryName(fyo.schemaMap[schemaName]);
const filters = await this.getCreateFilters(); const filters = await this.getCreateFilters();
const { openQuickEdit } = await import('src/utils/ui'); const { openQuickEdit } = await import('src/utils/ui');