stores(options): fix useOptions hook

This commit is contained in:
Chika 2022-05-14 15:11:37 +03:00
parent 1cf69a122a
commit be1ffcc624
2 changed files with 26 additions and 20 deletions

View File

@ -3,7 +3,10 @@ import { useStore } from 'stores/hooks';
export function useOptions(elementName) {
const { $calculation } = useStore();
const options = $calculation.$options.observeOptions(elementName);
return {
options: $calculation.$options.getOptions(elementName),
options,
};
}

View File

@ -6,6 +6,7 @@
import type { Elements } from 'Components/Calculation/config/map';
import { mergeWith } from 'lodash';
import { makeAutoObservable } from 'mobx';
import { computedFn } from 'mobx-utils';
import RootStore from 'stores/root';
import { CalculationOptions, Filter, Options, OptionsFilters } from './types';
@ -27,8 +28,8 @@ const AUTO_SET_VALUE_ELEMENTS: Elements[] = [
export default class OptionsStore {
root: RootStore;
#options: Partial<CalculationOptions> = {};
#filters: Partial<OptionsFilters> = {};
options: Partial<CalculationOptions> = {};
filters: Partial<OptionsFilters> = {};
constructor(rootStore: RootStore) {
makeAutoObservable(this);
@ -36,13 +37,13 @@ export default class OptionsStore {
}
hydrate = (initialOptions: Partial<CalculationOptions>) => {
this.#options = initialOptions;
this.options = initialOptions;
};
/** **************** OPTIONS **************** */
getOption(elementName: Elements) {
const value = this.root.$calculation.$values.getValueByElement(elementName);
return this.#options[elementName]?.find((x) => x.value === value);
return this.options[elementName]?.find((x) => x.value === value);
}
getOptions(
@ -51,37 +52,39 @@ export default class OptionsStore {
filtered: true;
}
) {
const options = this.#options[elementName];
const options = this.options[elementName];
if (!settings?.filtered) return options;
const filter = this.#filters[elementName];
const filter = this.filters[elementName];
return filter ? options && filter(options) : options;
}
setOptions(elementName: Elements, options: Options) {
this.#options[elementName] = options;
}
observeOptions = computedFn((elementName: Elements) => this.getOptions(elementName));
mergeOptions(options: Partial<CalculationOptions>) {
mergeWith(this.#options, options, (objValue, srcValue) =>
setOptions = (elementName: Elements, options: Options) => {
this.options[elementName] = options;
};
mergeOptions = (options: Partial<CalculationOptions>) => {
mergeWith(this.options, options, (objValue, srcValue) =>
objValue === undefined ? srcValue : objValue
);
}
};
clearOptions(elementName: Elements) {
this.#options[elementName] = [];
clearOptions = (elementName: Elements) => {
this.options[elementName] = [];
this.root.$calculation.$values.clearValueOfElement(elementName);
}
};
/** **************** FILTERS **************** */
getFilter(elementName: Elements) {
return this.#filters[elementName];
return this.filters[elementName];
}
setFilter(elementName: Elements, filter: Filter) {
this.#filters[elementName] = filter;
setFilter = (elementName: Elements, filter: Filter) => {
this.filters[elementName] = filter;
/**
* Проверяем, что значение есть в отфильтрованном списке,
@ -106,5 +109,5 @@ export default class OptionsStore {
if (filteredOptons?.length === 1 && AUTO_SET_VALUE_ELEMENTS.includes(elementName)) {
this.root.$calculation.$values.setValueOfElement(elementName, filteredOptons[0].value);
}
}
};
}