2022-10-26 19:07:46 +03:00

49 lines
979 B
TypeScript

import notification from 'Elements/notification';
import { makeAutoObservable } from 'mobx';
type Params = {
err_key: string;
err_title: string;
};
export default class Validation {
params: Params;
messages: Set<string>;
constructor(params: Params) {
this.params = params;
this.messages = new Set();
makeAutoObservable(this);
}
get hasErrors() {
return this.messages.size > 0;
}
getMessages() {
return [...this.messages];
}
removeError = (message: string) => {
this.messages.delete(message);
if (this.messages.size === 0) notification.close(this.params.err_key);
};
addError = (message: string) => {
this.messages.add(message);
notification.error({
key: this.params.err_key,
message: this.params.err_title,
description: message,
});
return () => this.removeError(message);
};
clearErrors = () => {
notification.close(this.params.err_key);
this.messages.clear();
};
}