2
0
mirror of https://github.com/frappe/books.git synced 2024-11-08 14:50:56 +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 { 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 {
return prefix + next.toString().padStart(padZeros ?? 4, '0');
}
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() {
let current = this.get('current') as number | null;

View File

@ -35,9 +35,11 @@ export default defineComponent({
return {
timerId: null,
barcode: '',
cooldown: '',
} as {
timerId: null | ReturnType<typeof setInterval>;
timerId: null | ReturnType<typeof setTimeout>;
barcode: string;
cooldown: string;
};
},
mounted() {
@ -64,6 +66,17 @@ export default defineComponent({
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', {
filters: { barcode },
fields: ['name'],
@ -97,12 +110,10 @@ export default defineComponent({
return await this.setItemFromBarcode();
}
if (this.timerId !== null) {
clearInterval(this.timerId);
}
this.clearInterval();
this.barcode += key;
this.timerId = setInterval(async () => {
this.timerId = setTimeout(async () => {
await this.setItemFromBarcode();
this.barcode = '';
}, 20);
@ -115,9 +126,15 @@ export default defineComponent({
await this.selectItem(this.barcode);
this.barcode = '';
if (this.timerId !== null) {
clearInterval(this.timerId);
this.clearInterval();
},
clearInterval() {
if (this.timerId === null) {
return;
}
clearInterval(this.timerId);
this.timerId = null;
},
error(message: string) {
showToast({ type: 'error', message });

View File

@ -131,7 +131,8 @@ export default {
},
async openNewDoc() {
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 { openQuickEdit } = await import('src/utils/ui');