stores(validation): fix useValidation hook
This commit is contained in:
parent
70ea06c588
commit
7af318617f
@ -3,6 +3,6 @@ import { useStore } from 'stores/hooks';
|
||||
|
||||
export function useValidation(elementName) {
|
||||
const { $calculation } = useStore();
|
||||
const validationResult = $calculation.$validation.getValidation(elementName);
|
||||
const validationResult = $calculation.$validation.observeValidation(elementName);
|
||||
return validationResult;
|
||||
}
|
||||
|
||||
@ -3,12 +3,13 @@
|
||||
/* eslint-disable object-curly-newline */
|
||||
import type { Elements } from 'Components/Calculation/config/map';
|
||||
import { makeAutoObservable, observable } from 'mobx';
|
||||
import { computedFn } from 'mobx-utils';
|
||||
import RootStore from 'stores/root';
|
||||
import type { Error, Messages } from './types';
|
||||
|
||||
export default class ValidationStore {
|
||||
root: RootStore;
|
||||
#messages: Partial<Messages> = {};
|
||||
messages: Partial<Messages> = {};
|
||||
|
||||
constructor(rootStore: RootStore) {
|
||||
makeAutoObservable(this);
|
||||
@ -17,34 +18,36 @@ export default class ValidationStore {
|
||||
|
||||
getValidation(elementName: Elements) {
|
||||
return {
|
||||
isValid: !this.#messages[elementName]?.length,
|
||||
messages: this.#messages[elementName]?.map((x) => x.text),
|
||||
isValid: !this.messages[elementName]?.length,
|
||||
messages: this.messages[elementName]?.map((x) => x.text),
|
||||
};
|
||||
}
|
||||
|
||||
addError(elementName: Elements, error: Error) {
|
||||
if (!this.#messages[elementName]) this.#messages[elementName] = observable([]);
|
||||
observeValidation = computedFn((elementName: Elements) => this.getValidation(elementName));
|
||||
|
||||
const errorIndex = this.#messages[elementName]?.findIndex((x) => x.name === error.name);
|
||||
addError = (elementName: Elements, error: Error) => {
|
||||
if (!this.messages[elementName]) this.messages[elementName] = observable([]);
|
||||
|
||||
const errorIndex = this.messages[elementName]?.findIndex((x) => x.name === error.name);
|
||||
const hasError = errorIndex !== undefined && errorIndex !== -1;
|
||||
|
||||
if (!hasError) {
|
||||
this.#messages[elementName]?.push(error);
|
||||
this.messages[elementName]?.push(error);
|
||||
} else {
|
||||
this.#messages[elementName]?.splice(errorIndex, 1, error);
|
||||
this.messages[elementName]?.splice(errorIndex, 1, error);
|
||||
}
|
||||
|
||||
// TODO: call notification
|
||||
|
||||
return () => this.#removeError(elementName, error.name);
|
||||
}
|
||||
};
|
||||
|
||||
#removeError(elementName: Elements, errorName: string) {
|
||||
const messageIndex = this.#messages[elementName]?.findIndex((x) => x.name === errorName);
|
||||
if (messageIndex) this.#messages[elementName]?.splice(messageIndex, 1);
|
||||
}
|
||||
#removeError = (elementName: Elements, errorName: string) => {
|
||||
const messageIndex = this.messages[elementName]?.findIndex((x) => x.name === errorName);
|
||||
if (messageIndex) this.messages[elementName]?.splice(messageIndex, 1);
|
||||
};
|
||||
|
||||
clearErrors(elementName: Elements) {
|
||||
this.#messages[elementName]?.clear();
|
||||
}
|
||||
clearErrors = (elementName: Elements) => {
|
||||
this.messages[elementName]?.clear();
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user