From 340203399a86331e53bde050d9f0eb06b99a8938 Mon Sep 17 00:00:00 2001 From: Chika Date: Fri, 6 May 2022 17:10:46 +0300 Subject: [PATCH] stores: add options store --- Components/Calculation/config/map.ts | 162 +++++++++++++++++++++++ Components/Calculation/types/elements.ts | 157 +--------------------- stores/calculation/index.ts | 3 + stores/calculation/options/index.ts | 55 ++++++++ stores/calculation/options/types.ts | 6 + stores/calculation/values/index.ts | 18 ++- stores/calculation/values/types.ts | 2 + stores/index.js | 4 +- 8 files changed, 250 insertions(+), 157 deletions(-) create mode 100644 Components/Calculation/config/map.ts create mode 100644 stores/calculation/options/index.ts create mode 100644 stores/calculation/options/types.ts diff --git a/Components/Calculation/config/map.ts b/Components/Calculation/config/map.ts new file mode 100644 index 0000000..3738b7c --- /dev/null +++ b/Components/Calculation/config/map.ts @@ -0,0 +1,162 @@ +import { Values, ValuesTypes } from 'stores/calculation/values/types'; +import { Elements } from '../types/elements'; + +function wrapElementsMap>(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]; +} diff --git a/Components/Calculation/types/elements.ts b/Components/Calculation/types/elements.ts index 5d3d781..66d4e86 100644 --- a/Components/Calculation/types/elements.ts +++ b/Components/Calculation/types/elements.ts @@ -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; diff --git a/stores/calculation/index.ts b/stores/calculation/index.ts index 4f47c61..6d0b3c6 100644 --- a/stores/calculation/index.ts +++ b/stores/calculation/index.ts @@ -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); } } diff --git a/stores/calculation/options/index.ts b/stores/calculation/options/index.ts new file mode 100644 index 0000000..40cea46 --- /dev/null +++ b/stores/calculation/options/index.ts @@ -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; +type OptionsFilters = Record Options>; + +export class OptionsStore { + root: RootStore; + #options: Partial = {}; + #filters: Partial = {}; + + constructor(rootStore: RootStore) { + makeAutoObservable(this); + this.root = rootStore; + } + + hydrate = (initialOptions: Partial) => { + 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) { + mergeWith(this.#options, options, function (objValue, srcValue) { + return objValue === undefined ? srcValue : objValue; + }); + } + + clearOptions(elementName: Elements) { + this.#options[elementName] = []; + + this.root.$calculation.$values.clearValueOfElement(elementName); + } +} diff --git a/stores/calculation/options/types.ts b/stores/calculation/options/types.ts new file mode 100644 index 0000000..794f318 --- /dev/null +++ b/stores/calculation/options/types.ts @@ -0,0 +1,6 @@ +import { Value } from '../values/types'; + +export type BaseOption = { + name: string; + value: Value; +}; diff --git a/stores/calculation/values/index.ts b/stores/calculation/values/index.ts index 71518b4..6ee12ad 100644 --- a/stores/calculation/values/index.ts +++ b/stores/calculation/values/index.ts @@ -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(valueName: K): Partial[K] { + getValue(valueName: V) { return this.#values[valueName]; } + getValueByElement(elementName: E) { + const valueName = getValueName(elementName); + return this.getValue(valueName) as ElementsTypes[E] | undefined; + } + getValues(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); + } } diff --git a/stores/calculation/values/types.ts b/stores/calculation/values/types.ts index cb3774f..cd59c36 100644 --- a/stores/calculation/values/types.ts +++ b/stores/calculation/values/types.ts @@ -253,3 +253,5 @@ export interface ValuesTypes { } export type Values = keyof ValuesTypes; + +export type Value = ValuesTypes[Values] | undefined; diff --git a/stores/index.js b/stores/index.js index 1e18760..3e042e9 100644 --- a/stores/index.js +++ b/stores/index.js @@ -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;