stores: add options store

This commit is contained in:
Chika 2022-05-06 17:10:46 +03:00
parent 5ae2a6ca53
commit 340203399a
8 changed files with 250 additions and 157 deletions

View File

@ -0,0 +1,162 @@
import { Values, ValuesTypes } from 'stores/calculation/values/types';
import { Elements } from '../types/elements';
function wrapElementsMap<T extends Record<string, Values>>(arg: T) {
return arg;
}
export const elementsToValues = wrapElementsMap({
selectLead: 'lead',
selectOpportunity: 'opportunity',
selectQuote: 'quote',
cbxRecalcWithRevision: 'recalcWithRevision',
selectProduct: 'product',
selectClientRisk: 'clientRisk',
selectClientType: 'clientType',
tbxLeaseObjectPrice: 'leaseObjectPrice',
tbxLeaseObjectPriceWthtVAT: 'leaseObjectPriceWthtVAT',
tbxVATInLeaseObjectPrice: 'VATInLeaseObjectPrice',
tbxEngineHours: 'engineHours',
selectSupplierCurrency: 'supplierCurrency',
tbxSupplierDiscountRub: 'supplierDiscountRub',
tbxSupplierDiscountPerc: 'supplierDiscountPerc',
tbxLeasingPeriod: 'leasingPeriod',
tbxFirstPaymentPerc: 'firstPaymentPerc',
tbxFirstPaymentRub: 'firstPaymentRub',
tbxLastPaymentPerc: 'lastPaymentPerc',
tbxLastPaymentRub: 'lastPaymentRub',
radioLastPaymentRule: 'lastPaymentRule',
tbxRedemptionPaymentSum: 'redemptionPaymentSum',
radioBalanceHolder: 'balanceHolder',
radioGraphType: 'graphType',
tbxParmentsDecreasePercent: 'parmentsDecreasePercent',
selectSeasonType: 'seasonType',
selectHighSeasonStart: 'highSeasonStart',
tbxComissionPerc: 'comissionPerc',
tbxComissionRub: 'comissionRub',
tbxSaleBonus: 'saleBonus',
tbxIRR_Perc: 'IRR_Perc',
selectLeaseObjectType: 'leaseObjectType',
radioDeliveryTime: 'deliveryTime',
labelDepreciationGroup: 'depreciationGroup',
tbxLeaseObjectCount: 'leaseObjectCount',
cbxWithTrailer: 'withTrailer',
cbxLeaseObjectUsed: 'leaseObjectUsed',
tbxMaxMass: 'maxMass',
tbxCountSeats: 'countSeats',
tbxMaxSpeed: 'maxSpeed',
selectBrand: 'brand',
selectModel: 'model',
selectConfiguration: 'configuration',
tbxLeaseObjectYear: 'leaseObjectYear',
selectEngineType: 'engineType',
selectLeaseObjectCategory: 'leaseObjectCategory',
tbxLeaseObjectMotorPower: 'leaseObjectMotorPower',
tbxEngineVolume: 'engineVolume',
selectLeaseObjectUseFor: 'leaseObjectUseFor',
selectDealer: 'dealer',
selectDealerPerson: 'dealerPerson',
selectDealerRewardCondition: 'dealerRewardCondition',
tbxDealerRewardSumm: 'dealerRewardSumm',
selectDealerBroker: 'dealerBroker',
selectDealerBrokerRewardCondition: 'dealerBrokerRewardCondition',
tbxDealerBrokerRewardSumm: 'dealerBrokerRewardSumm',
selectIndAgent: 'indAgent',
selectIndAgentRewardCondition: 'indAgentRewardCondition',
tbxIndAgentRewardSumm: 'indAgentRewardSumm',
selectCalcDoubleAgent: 'calcDoubleAgent',
selectCalcDoubleAgentRewardCondition: 'calcDoubleAgentRewardCondition',
tbxCalcDoubleAgentRewardSumm: 'calcDoubleAgentRewardSumm',
selectCalcBroker: 'calcBroker',
selectCalcBrokerRewardCondition: 'calcBrokerRewardCondition',
tbxCalcBrokerRewardSum: 'calcBrokerRewardSum',
selectCalcFinDepartment: 'calcFinDepartment',
selectFinDepartmentRewardCondtion: 'finDepartmentRewardCondtion',
tbxFinDepartmentRewardSumm: 'finDepartmentRewardSumm',
selectGPSBrand: 'GPSBrand',
selectGPSModel: 'GPSModel',
selectRegionRegistration: 'regionRegistration',
selectTownRegistration: 'townRegistration',
radioInfuranceOPF: 'infuranceOPF',
radioInsKaskoType: 'insKaskoType',
cbxInsDecentral: 'insDecentral',
tbxInsFranchise: 'insFranchise',
cbxInsUnlimitDrivers: 'insUnlimitDrivers',
tbxInsAgeDrivers: 'insAgeDrivers',
tbxInsExpDrivers: 'insExpDrivers',
tbxINNForCalc: 'INNForCalc',
cbxLastPaymentRedemption: 'lastPaymentRedemption',
cbxPriceWithDiscount: 'priceWithDiscount',
cbxFullPriceWithDiscount: 'fullPriceWithDiscount',
cbxCostIncrease: 'costIncrease',
cbxInsurance: 'insurance',
cbxRegistrationQuote: 'registrationQuote',
cbxTechnicalCardQuote: 'technicalCardQuote',
cbxNSIB: 'NSIB',
tbxQuoteName: 'quoteName',
radioQuoteContactGender: 'quoteContactGender',
cbxQuoteRedemptionGraph: 'quoteRedemptionGraph',
cbxShowFinGAP: 'showFinGAP',
selectTarif: 'tarif',
tbxCreditRate: 'creditRate',
selectRate: 'rate',
tbxMaxPriceChange: 'maxPriceChange',
tbxImporterRewardPerc: 'importerRewardPerc',
tbxImporterRewardRub: 'importerRewardRub',
cbxDisableChecks: 'disableChecks',
selectRegistration: 'registration',
selectInsNSIB: 'insNSIB',
selectTechnicalCard: 'technicalCard',
radioRequirementTelematic: 'requirementTelematic',
selectTelematic: 'telematic',
selectTracker: 'tracker',
tbxMileage: 'mileage',
radioCalcType: 'calcType',
tbxTotalPayments: 'totalPayments',
radioObjectRegistration: 'objectRegistration',
selectObjectRegionRegistration: 'objectRegionRegistration',
tbxVehicleTaxInYear: 'vehicleTaxInYear',
tbxVehicleTaxInLeasingPeriod: 'vehicleTaxInLeasingPeriod',
selectObjectCategoryTax: 'objectCategoryTax',
selectObjectTypeTax: 'objectTypeTax',
radioTypePTS: 'typePTS',
selectLegalClientRegion: 'legalClientRegion',
selectLegalClientTown: 'legalClientTown',
selectSubsidy: 'subsidy',
labelSubsidySum: 'subsidySum',
selectFuelCard: 'fuelCard',
tbxMinPriceChange: 'minPriceChange',
/** Result Elements */
labelResultTotalGraphwithNDS: 'resultTotalGraphwithNDS',
labelResultPlPrice: 'resultPlPrice',
labelResultPriceUpPr: 'resultPriceUpPr',
labelResultIRRGraphPerc: 'resultIRRGraphPerc',
labelResultIRRNominalPerc: 'resultIRRNominalPerc',
labelResultInsKasko: 'resultInsKasko',
labelResultInsOsago: 'resultInsOsago',
labelResultDopProdSum: 'resultDopProdSum',
labelResultFirstPayment: 'resultFirstPayment',
labelResultLastPayment: 'resultLastPayment',
labelResultTerm: 'resultTerm',
labelResultAB_FL: 'resultAB_FL',
labelResultAB_UL: 'resultAB_UL',
labelResultBonusMPL: 'resultBonusMPL',
labelResultDopMPLLeasing: 'resultDopMPLLeasing',
labelResultBonusDopProd: 'resultBonusDopProd',
labelResultBonusSafeFinance: 'resultBonusSafeFinance',
labelResultFirstPaymentRiskPolicy: 'resultFirstPaymentRiskPolicy',
/** Link Elements */
linkDownloadKp: 'kpUrl',
});
export type ElementsValues = typeof elementsToValues;
export type ElementsTypes = {
[Key in keyof ElementsValues]: ValuesTypes[ElementsValues[Key]];
};
export function getValueName(elementName: Elements) {
return elementsToValues[elementName];
}

View File

@ -1,156 +1,3 @@
export type Elements =
| 'selectLead'
| 'selectOpportunity'
| 'selectQuote'
| 'cbxRecalcWithRevision'
| 'selectProduct'
| 'selectClientRisk'
| 'selectClientType'
| 'tbxLeaseObjectPrice'
| 'selectSupplierCurrency'
| 'tbxSupplierDiscountRub'
| 'tbxSupplierDiscountPerc'
| 'tbxLeasingPeriod'
| 'tbxFirstPaymentPerc'
| 'tbxFirstPaymentRub'
| 'tbxLastPaymentPerc'
| 'tbxLastPaymentRub'
| 'radioLastPaymentRule'
| 'tbxRedemptionPaymentSum'
| 'radioBalanceHolder'
| 'radioGraphType'
| 'tbxParmentsDecreasePercent'
| 'selectSeasonType'
| 'selectHighSeasonStart'
| 'tbxComissionPerc'
| 'tbxComissionRub'
| 'tbxSaleBonus'
| 'tbxIRR_Perc'
| 'selectLeaseObjectType'
| 'radioDeliveryTime'
| 'labelDepreciationGroup'
| 'tbxLeaseObjectCount'
| 'cbxWithTrailer'
| 'cbxLeaseObjectUsed'
| 'tbxMaxMass'
| 'tbxCountSeats'
| 'tbxMaxSpeed'
| 'selectBrand'
| 'selectModel'
| 'selectConfiguration'
| 'tbxLeaseObjectYear'
| 'selectEngineType'
| 'selectLeaseObjectCategory'
| 'tbxLeaseObjectMotorPower'
| 'tbxEngineVolume'
| 'selectLeaseObjectUseFor'
| 'selectDealer'
| 'selectDealerPerson'
| 'selectDealerRewardCondition'
| 'tbxDealerRewardSumm'
| 'selectDealerBroker'
| 'selectDealerBrokerRewardCondition'
| 'tbxDealerBrokerRewardSumm'
| 'selectIndAgent'
| 'selectIndAgentRewardCondition'
| 'tbxIndAgentRewardSumm'
| 'selectCalcDoubleAgent'
| 'selectCalcDoubleAgentRewardCondition'
| 'tbxCalcDoubleAgentRewardSumm'
| 'selectCalcBroker'
| 'selectCalcBrokerRewardCondition'
| 'tbxCalcBrokerRewardSum'
| 'selectCalcFinDepartment'
| 'selectFinDepartmentRewardCondtion'
| 'tbxFinDepartmentRewardSumm'
| 'selectGPSBrand'
| 'selectGPSModel'
| 'selectRegionRegistration'
| 'selectTownRegistration'
| 'radioInfuranceOPF'
| 'radioInsKaskoType'
| 'cbxInsDecentral'
| 'tbxInsFranchise'
| 'cbxInsUnlimitDrivers'
| 'tbxInsAgeDrivers'
| 'tbxInsExpDrivers'
| 'tbxINNForCalc'
| 'cbxLastPaymentRedemption'
| 'cbxPriceWithDiscount'
| 'cbxFullPriceWithDiscount'
| 'cbxCostIncrease'
| 'cbxInsurance'
| 'cbxRegistrationQuote'
| 'cbxTechnicalCardQuote'
| 'cbxNSIB'
| 'tbxQuoteName'
| 'radioQuoteContactGender'
| 'cbxQuoteRedemptionGraph'
| 'cbxShowFinGAP'
| 'selectTarif'
| 'tbxCreditRate'
| 'selectRate'
| 'radioRequirementTelematic'
| 'tbxMaxPriceChange'
| 'tbxImporterRewardPerc'
| 'tbxImporterRewardRub'
| 'cbxDisableChecks'
| 'selectRegistration'
| 'selectInsNSIB'
| 'selectTechnicalCard'
| 'selectTelematic'
| 'selectTracker'
| 'tbxMileage'
| 'radioCalcType'
| 'tbxTotalPayments'
| 'radioObjectRegistration'
| 'selectObjectRegionRegistration'
| 'tbxVehicleTaxInYear'
| 'tbxVehicleTaxInLeasingPeriod'
| 'selectObjectCategoryTax'
| 'selectObjectTypeTax'
| 'radioTypePTS'
| 'selectLegalClientRegion'
| 'selectLegalClientTown'
| 'selectSubsidy'
| 'labelSubsidySum'
| 'selectFuelCard'
| 'tbxMinPriceChange'
| 'tbxLeaseObjectPriceWthtVAT'
| 'tbxVATInLeaseObjectPrice'
| 'tbxEngineHours'
import { ElementsValues } from '../config/map';
/** Result Elements */
| 'labelResultTotalGraphwithNDS'
| 'labelResultPlPrice'
| 'labelResultPriceUpPr'
| 'labelResultIRRGraphPerc'
| 'labelResultIRRNominalPerc'
| 'labelResultInsKasko'
| 'labelResultInsOsago'
| 'labelResultDopProdSum'
| 'labelResultFirstPayment'
| 'labelResultLastPayment'
| 'labelResultTerm'
| 'labelResultAB_FL'
| 'labelResultAB_UL'
| 'labelResultBonusMPL'
| 'labelResultDopMPLLeasing'
| 'labelResultBonusDopProd'
| 'labelResultBonusSafeFinance'
| 'labelResultFirstPaymentRiskPolicy'
/** Button Elements */
| 'btnCreateKP'
| 'btnCalculate'
/** Link Elements */
| 'linkDownloadKp'
/** Computed Elements */
| 'labelLeaseObjectRisk'
| 'tbxInsKaskoPriceLeasePeriod'
| 'labelIrrInfo'
| 'labelRegistrationDescription';
export type CustomComponents = 'componentElt' | 'componentfinGAP';
export type Elements = keyof ElementsValues;

View File

@ -1,13 +1,16 @@
import { RootStore } from 'stores/root';
import { OptionsStore } from './options';
import { StatusStore } from './statuses';
import { ValuesStore } from './values';
export class CalculationStore {
$values: ValuesStore;
$status: StatusStore;
$options: OptionsStore;
constructor(rootStore: RootStore) {
this.$values = new ValuesStore(rootStore);
this.$status = new StatusStore(rootStore);
this.$options = new OptionsStore(rootStore);
}
}

View File

@ -0,0 +1,55 @@
import { Elements } from 'Components/Calculation/types/elements';
import { mergeWith } from 'lodash';
import { makeAutoObservable } from 'mobx';
import { RootStore } from 'stores/root';
import { BaseOption } from './types';
type Options = BaseOption[];
type CalculationOptions = Record<Elements, Options>;
type OptionsFilters = Record<Elements, (options: Options) => Options>;
export class OptionsStore {
root: RootStore;
#options: Partial<CalculationOptions> = {};
#filters: Partial<OptionsFilters> = {};
constructor(rootStore: RootStore) {
makeAutoObservable(this);
this.root = rootStore;
}
hydrate = (initialOptions: Partial<CalculationOptions>) => {
this.#options = initialOptions;
};
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 }) {
const options = this.#options[elementName];
if (!options) return;
if (!filtered) return options;
const filter = this.#filters[elementName];
return filter ? filter(options) : options;
}
setOptions(elementName: Elements, options: Options) {
this.#options[elementName] = options;
}
mergeOptions(options: Partial<CalculationOptions>) {
mergeWith(this.#options, options, function (objValue, srcValue) {
return objValue === undefined ? srcValue : objValue;
});
}
clearOptions(elementName: Elements) {
this.#options[elementName] = [];
this.root.$calculation.$values.clearValueOfElement(elementName);
}
}

View File

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

View File

@ -1,3 +1,5 @@
import { ElementsTypes, getValueName } from 'Components/Calculation/config/map';
import { Elements } from 'Components/Calculation/types/elements';
import { makeAutoObservable } from 'mobx';
import { RootStore } from '../../root';
import { Values, ValuesTypes } from './types';
@ -15,10 +17,15 @@ export class ValuesStore {
this.#values = initialValues;
};
getValue<K extends Values>(valueName: K): Partial<ValuesTypes>[K] {
getValue<V extends Values>(valueName: V) {
return this.#values[valueName];
}
getValueByElement<E extends Elements>(elementName: E) {
const valueName = getValueName(elementName);
return this.getValue(valueName) as ElementsTypes[E] | undefined;
}
getValues<K extends Values>(valuesNames: readonly K[]) {
return valuesNames.reduce((values, valueName) => {
values[valueName] = this.getValue(valueName);
@ -34,4 +41,13 @@ export class ValuesStore {
if (options.replace) this.#values = values;
this.#values = Object.assign(this.#values, values);
}
clearValue(valueName: Values) {
this.setValue(valueName, null);
}
clearValueOfElement(elementName: Elements) {
const valueName = getValueName(elementName);
this.clearValue(valueName);
}
}

View File

@ -253,3 +253,5 @@ export interface ValuesTypes {
}
export type Values = keyof ValuesTypes;
export type Value = ValuesTypes[Values] | undefined;

View File

@ -11,11 +11,13 @@ export function initializeStore(initialData) {
if (initialData) {
const {
user = null,
calculation: { values = {}, statuses = {} },
calculation: { values = {}, statuses = {}, options = {} },
} = initialData;
_store.$user.hydrate(user);
_store.$calculation.$values.hydrate(values);
_store.$calculation.$status.hydrate(statuses);
_store.$calculation.$options.hydrate(options);
}
if (typeof window === 'undefined') return _store;