diff --git a/stores/calculation/options/index.ts b/stores/calculation/options/index.ts index 40cea46..cf3e387 100644 --- a/stores/calculation/options/index.ts +++ b/stores/calculation/options/index.ts @@ -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; -type OptionsFilters = Record Options>; +type Filter = (options: Options) => Options; +type OptionsFilters = Record; 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, + ); + } + } + + /** */ } diff --git a/stores/calculation/options/types.ts b/stores/calculation/options/types.ts index 794f318..b60366f 100644 --- a/stores/calculation/options/types.ts +++ b/stores/calculation/options/types.ts @@ -1,6 +1,4 @@ -import { Value } from '../values/types'; - export type BaseOption = { name: string; - value: Value; + value: any; }; diff --git a/stores/calculation/values/index.ts b/stores/calculation/values/index.ts index 6ee12ad..e8402cf 100644 --- a/stores/calculation/values/index.ts +++ b/stores/calculation/values/index.ts @@ -26,19 +26,27 @@ export class ValuesStore { return this.getValue(valueName) as ElementsTypes[E] | undefined; } - getValues(valuesNames: readonly K[]) { + getValues(valuesNames: readonly V[]) { return valuesNames.reduce((values, valueName) => { values[valueName] = this.getValue(valueName); return values; }, {} as Pick, typeof valuesNames[number]>); } - setValue(valueName: Values, value: any) { + setValue(valueName: V, value: ValuesTypes[V]) { this.#values[valueName] = value; } - setValues(values: Partial, options: { replace?: boolean }) { - if (options.replace) this.#values = values; + setValueOfElement( + elementName: E, + value: ElementsTypes[E], + ) { + const valueName = getValueName(elementName); + this.setValue(valueName, value); + } + + setValues(values: Partial, settings: { replace?: boolean }) { + if (settings?.replace) this.#values = values; this.#values = Object.assign(this.#values, values); } diff --git a/stores/calculation/values/types.ts b/stores/calculation/values/types.ts index cd59c36..5a8fec5 100644 --- a/stores/calculation/values/types.ts +++ b/stores/calculation/values/types.ts @@ -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;