48 lines
883 B
TypeScript
48 lines
883 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];
|
|
}
|
|
|
|
addError = (message: string) => {
|
|
this.messages.add(message);
|
|
|
|
const removeError = () => {
|
|
this.messages.delete(message);
|
|
notification.close(this.params.err_key);
|
|
};
|
|
|
|
notification.error({
|
|
key: this.params.err_key,
|
|
message: this.params.err_title,
|
|
description: message,
|
|
});
|
|
|
|
return removeError;
|
|
};
|
|
|
|
clearErrors = () => {
|
|
this.messages.clear();
|
|
};
|
|
}
|