2023-02-13 18:30:05 +03:00

47 lines
1.1 KiB
TypeScript

import type { RemoveError, ValidationConfig } from './types';
import { makeAutoObservable } from 'mobx';
import notification from 'ui/elements/notification';
export default class Validation {
private params: ValidationConfig;
private messages: Set<string>;
constructor(config: ValidationConfig) {
this.params = config;
this.messages = new Set();
makeAutoObservable(this);
}
public get hasErrors() {
return this.messages.size > 0;
}
public getMessages() {
return [...this.messages];
}
public removeError = (message: string) => {
this.messages.delete(message);
if (this.messages.size === 0) notification.close(this.params.err_key);
};
public addError = (message: string, silent?: boolean) => {
if (!silent && !this.messages.has(message)) {
notification.error({
description: message,
key: this.params.err_key,
message: this.params.err_title,
});
}
this.messages.add(message);
return (() => this.removeError(message)) as RemoveError;
};
public clearErrors = () => {
this.messages.clear();
notification.close(this.params.err_key);
};
}