stores: add filters functional for options store

This commit is contained in:
Chika 2022-05-06 21:25:58 +03:00
parent 340203399a
commit 85036ff164
4 changed files with 90 additions and 13 deletions

View File

@ -4,9 +4,29 @@ import { makeAutoObservable } from 'mobx';
import { RootStore } from 'stores/root';
import { BaseOption } from './types';
const _EXCLUDE_RESET_ELEMENTS: Elements[] = [
'selectTechnicalCard',
'selectTownRegistration',
];
const _AUTO_SET_VALUE_ELEMENTS: Elements[] = [
'selectProduct',
'selectLeaseObjectType',
'selectBrand',
'selectModel',
'selectConfiguration',
'selectTracker',
'selectTelematic',
'selectTechnicalCard',
'selectFuelCard',
'selectRegistration',
'selectTownRegistration',
];
type Options = BaseOption[];
type CalculationOptions = Record<Elements, Options>;
type OptionsFilters = Record<Elements, (options: Options) => Options>;
type Filter = (options: Options) => Options;
type OptionsFilters = Record<Elements, Filter>;
export class OptionsStore {
root: RootStore;
@ -22,16 +42,18 @@ export class OptionsStore {
this.#options = initialOptions;
};
/** OPTIONS */
getOption(elementName: Elements) {
const value = this.root.$calculation.$values.getValueByElement(elementName);
return this.#options[elementName]?.find(x => x.value === value);
}
getOptions(elementName: Elements, { filtered = true }) {
getOptions(elementName: Elements, settings?: { filtered: true }) {
const options = this.#options[elementName];
if (!options) return;
if (!filtered) return options;
if (!settings?.filtered) return options;
const filter = this.#filters[elementName];
return filter ? filter(options) : options;
@ -52,4 +74,51 @@ export class OptionsStore {
this.root.$calculation.$values.clearValueOfElement(elementName);
}
/** */
/** FILTERS */
getFilter(elementName: Elements) {
return this.#filters[elementName];
}
setFilter(elementName: Elements, filter: Filter) {
this.#filters[elementName] = filter;
/**
* Проверяем, что значение есть в отфильтрованном списке,
* иначе сбрасываем значение
* (кроме исключений {@link _EXCLUDE_RESET_ELEMENTS})
*/
const filteredOptons = this.getOptions(elementName);
if (!_EXCLUDE_RESET_ELEMENTS.includes(elementName)) {
const elementValue =
this.root.$calculation.$values.getValueByElement(elementName);
if (
!filteredOptons?.length ||
!filteredOptons.some(x => x.value === elementValue)
)
this.root.$calculation.$values.clearValueOfElement(elementName);
return;
}
/**
* Если после фильтрации остается одна запись, то указываем ее
* (для элементов из списка {@link _AUTO_SET_VALUE_ELEMENTS})
*/
if (
filteredOptons?.length === 1 &&
_AUTO_SET_VALUE_ELEMENTS.includes(elementName)
) {
this.root.$calculation.$values.setValueOfElement(
elementName,
filteredOptons[0].value,
);
}
}
/** */
}

View File

@ -1,6 +1,4 @@
import { Value } from '../values/types';
export type BaseOption = {
name: string;
value: Value;
value: any;
};

View File

@ -26,19 +26,27 @@ export class ValuesStore {
return this.getValue(valueName) as ElementsTypes[E] | undefined;
}
getValues<K extends Values>(valuesNames: readonly K[]) {
getValues<V extends Values>(valuesNames: readonly V[]) {
return valuesNames.reduce((values, valueName) => {
values[valueName] = this.getValue(valueName);
return values;
}, {} as Pick<Partial<ValuesTypes>, typeof valuesNames[number]>);
}
setValue(valueName: Values, value: any) {
setValue<V extends Values>(valueName: V, value: ValuesTypes[V]) {
this.#values[valueName] = value;
}
setValues(values: Partial<ValuesTypes>, options: { replace?: boolean }) {
if (options.replace) this.#values = values;
setValueOfElement<E extends Elements>(
elementName: E,
value: ElementsTypes[E],
) {
const valueName = getValueName(elementName);
this.setValue(valueName, value);
}
setValues(values: Partial<ValuesTypes>, settings: { replace?: boolean }) {
if (settings?.replace) this.#values = values;
this.#values = Object.assign(this.#values, values);
}

View File

@ -1,4 +1,4 @@
export interface ValuesTypes {
interface Types {
lead: string;
opportunity: string;
quote: string;
@ -252,6 +252,8 @@ export interface ValuesTypes {
quoteUrl: string;
}
export type Values = keyof ValuesTypes;
export type ValuesTypes = {
[Key in keyof Types]: Types[Key] | null;
};
export type Value = ValuesTypes[Values] | undefined;
export type Values = keyof ValuesTypes;