2023-02-06 12:19:39 +03:00

47 lines
1.1 KiB
TypeScript

import type { 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) => {
this.messages.add(message);
if (!silent) {
notification.error({
description: message,
key: this.params.err_key,
message: this.params.err_title,
});
}
return () => this.removeError(message);
};
public clearErrors = () => {
this.messages.clear();
notification.close(this.params.err_key);
};
}