From b733da22cc3fadbfa7e76df470b8d56d27c241b9 Mon Sep 17 00:00:00 2001 From: Chika Date: Fri, 29 Apr 2022 12:50:28 +0300 Subject: [PATCH] store: add CalculationStore (values) --- next.config.js | 8 ++ package.json | 2 + stores/calculation/index.ts | 10 +++ stores/calculation/types.ts | 141 +++++++++++++++++++++++++++++++++++ stores/calculation/values.ts | 37 +++++++++ stores/index.js | 3 +- stores/root.ts | 4 + tsconfig.json | 2 +- yarn.lock | 5 ++ 9 files changed, 210 insertions(+), 2 deletions(-) create mode 100644 stores/calculation/index.ts create mode 100644 stores/calculation/types.ts create mode 100644 stores/calculation/values.ts diff --git a/next.config.js b/next.config.js index 14c9878..b56a6bd 100644 --- a/next.config.js +++ b/next.config.js @@ -16,6 +16,14 @@ const nextConfig = { }, ]; }, + experimental: { + modularizeImports: { + lodash: { + transform: 'lodash/{{member}}', + preventFullImport: true, + }, + }, + }, }; const plugins = [ diff --git a/package.json b/package.json index 5aabaa8..9b48920 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "graphql": "14.0.2 - 14.2.0 || ^14.3.1 || ^15.0.0", "less": "^4.1.2", "less-loader": "^10.2.0", + "lodash": "^4.17.21", "mobx": "^6.5.0", "mobx-react-lite": "^3.3.0", "next": "12.1.5", @@ -30,6 +31,7 @@ "styled-components": "^5.3.5" }, "devDependencies": { + "@types/lodash": "^4.14.182", "@types/node": "17.0.25", "@types/react": "18.0.5", "@types/react-dom": "18.0.1", diff --git a/stores/calculation/index.ts b/stores/calculation/index.ts new file mode 100644 index 0000000..98dcd32 --- /dev/null +++ b/stores/calculation/index.ts @@ -0,0 +1,10 @@ +import { RootStore } from 'stores/root'; +import { ValuesStore } from './values'; + +export class CalculationStore { + $values: ValuesStore; + + constructor(rootStore: RootStore) { + this.$values = new ValuesStore(rootStore); + } +} diff --git a/stores/calculation/types.ts b/stores/calculation/types.ts new file mode 100644 index 0000000..ea5262a --- /dev/null +++ b/stores/calculation/types.ts @@ -0,0 +1,141 @@ +export type Values = + | 'lead' + | 'opportunity' + | 'quote' + | 'recalcWithRevision' + | 'product' + | 'clientRisk' + | 'clientType' + | 'leaseObjectPrice' + | 'supplierCurrency' + | 'supplierDiscountRub' + | 'supplierDiscountPerc' + | 'leasingPeriod' + | 'firstPaymentPerc' + | 'firstPaymentRub' + | 'lastPaymentPerc' + | 'lastPaymentRub' + | 'lastPaymentRule' + | 'redemptionPaymentSum' + | 'balanceHolder' + | 'graphType' + | 'parmentsDecreasePercent' + | 'seasonType' + | 'highSeasonStart' + | 'comissionPerc' + | 'comissionRub' + | 'saleBonus' + | 'IRR_Perc' + | 'paymentGraph' + | 'leaseObjectType' + | 'deliveryTime' + | 'leaseObjectRisk' + | 'depreciationGroup' + | 'leaseObjectCount' + | 'withTrailer' + | 'leaseObjectUsed' + | 'maxMass' + | 'countSeats' + | 'maxSpeed' + | 'brand' + | 'model' + | 'configuration' + | 'leaseObjectYear' + | 'engineType' + | 'leaseObjectCategory' + | 'leaseObjectMotorPower' + | 'engineVolume' + | 'leaseObjectUseFor' + | 'dealer' + | 'dealerPerson' + | 'dealerRewardCondition' + | 'dealerRewardSumm' + | 'dealerBroker' + | 'dealerBrokerRewardCondition' + | 'dealerBrokerRewardSumm' + | 'indAgent' + | 'indAgentRewardCondition' + | 'indAgentRewardSumm' + | 'calcDoubleAgent' + | 'calcDoubleAgentRewardCondition' + | 'calcDoubleAgentRewardSumm' + | 'calcBroker' + | 'calcBrokerRewardCondition' + | 'calcBrokerRewardSum' + | 'calcFinDepartment' + | 'finDepartmentRewardCondtion' + | 'finDepartmentRewardSumm' + | 'GPSBrand' + | 'GPSModel' + | 'regionRegistration' + | 'townRegistration' + | 'infuranceOPF' + | 'insKaskoType' + | 'insDecentral' + | 'insFranchise' + | 'insUnlimitDrivers' + | 'insAgeDrivers' + | 'insExpDrivers' + | 'INNForCalc' + | 'lastPaymentRedemption' + | 'priceWithDiscount' + | 'fullPriceWithDiscount' + | 'costIncrease' + | 'insurance' + | 'registrationQuote' + | 'technicalCardQuote' + | 'NSIB' + | 'quoteName' + | 'quoteContactGender' + | 'quoteRedemptionGraph' + | 'showFinGAP' + | 'tarif' + | 'creditRate' + | 'rate' + | 'requirementTelematic' + | 'maxPriceChange' + | 'importerRewardPerc' + | 'importerRewardRub' + | 'disableChecks' + | 'registration' + | 'insNSIB' + | 'technicalCard' + | 'telematic' + | 'tracker' + | 'mileage' + | 'calcType' + | 'totalPayments' + | 'objectRegistration' + | 'objectRegionRegistration' + | 'vehicleTaxInYear' + | 'vehicleTaxInLeasingPeriod' + | 'objectCategoryTax' + | 'objectTypeTax' + | 'typePTS' + | 'legalClientRegion' + | 'legalClientTown' + | 'subsidy' + | 'subsidySum' + | 'fuelCard' + | 'minPriceChange' + | 'resultTotalGraphwithNDS' + | 'resultPlPrice' + | 'resultPriceUpPr' + | 'resultIRRGraphPerc' + | 'resultIRRNominalPerc' + | 'resultInsKasko' + | 'resultInsOsago' + | 'resultDopProdSum' + | 'resultFirstPayment' + | 'resultLastPayment' + | 'resultTerm' + | 'resultAB_FL' + | 'resultAB_UL' + | 'resultBonusMPL' + | 'resultDopMPLLeasing' + | 'resultBonusDopProd' + | 'resultBonusSafeFinance' + | 'resultFirstPaymentRiskPolicy' + | 'leaseObjectPriceWthtVAT' + | 'VATInLeaseObjectPrice' + | 'engineHours'; diff --git a/stores/calculation/values.ts b/stores/calculation/values.ts new file mode 100644 index 0000000..47fb25f --- /dev/null +++ b/stores/calculation/values.ts @@ -0,0 +1,37 @@ +import { pick } from 'lodash'; +import { makeAutoObservable } from 'mobx'; +import { RootStore } from '../root'; +import { Values } from './types'; + +export type CalculationValues = Partial>; + +export class ValuesStore { + root: RootStore; + #values: CalculationValues = {}; + + constructor(rootStore: RootStore) { + makeAutoObservable(this); + this.root = rootStore; + } + + hydrate = (initialValues: CalculationValues) => { + this.#values = initialValues; + }; + + getValue(valueName: Values) { + return this.#values[valueName]; + } + + getValues(valuesNames: Values[]) { + return pick(this.#values, valuesNames); + } + + setValue(valueName: Values, value: any) { + this.#values[valueName] = value; + } + + setValues(values: CalculationValues, options: { replace?: boolean }) { + if (options.replace) this.#values = values; + this.#values = Object.assign(this.#values, values); + } +} diff --git a/stores/index.js b/stores/index.js index e143d9d..6a13e83 100644 --- a/stores/index.js +++ b/stores/index.js @@ -9,8 +9,9 @@ export function initializeStore(initialData) { const _store = store ?? new RootStore(); if (initialData) { - const { user } = initialData; + const { user = null, calculationValues = {} } = initialData; _store.$user.hydrate(user); + _store.$calculation.$values.hydrate(calculationValues); } if (typeof window === 'undefined') return _store; diff --git a/stores/root.ts b/stores/root.ts index 9a35e6a..34030a6 100644 --- a/stores/root.ts +++ b/stores/root.ts @@ -1,11 +1,15 @@ import { enableStaticRendering } from 'mobx-react-lite'; +import { CalculationStore } from './calculation'; import { UserStore } from './user'; enableStaticRendering(typeof window === 'undefined'); export class RootStore { $user: UserStore; + $calculation: CalculationStore; + constructor() { this.$user = new UserStore(this); + this.$calculation = new CalculationStore(this); } } diff --git a/tsconfig.json b/tsconfig.json index 92ae294..8227501 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "baseUrl": ".", - "target": "es5", + "target": "ES6", "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "skipLibCheck": true, diff --git a/yarn.lock b/yarn.lock index db7e4c9..ec2aad1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -850,6 +850,11 @@ resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4= +"@types/lodash@^4.14.182": + version "4.14.182" + resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.182.tgz#05301a4d5e62963227eaafe0ce04dd77c54ea5c2" + integrity sha512-/THyiqyQAP9AfARo4pF+aCGcyiQ94tX/Is2I7HofNRqoYLgN1PBoOWu2/zTA5zMxzP5EFutMtWtGAFRKUe961Q== + "@types/node-fetch@^2.5.10": version "2.6.1" resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.1.tgz#8f127c50481db65886800ef496f20bbf15518975"