From 99f82ec2f9fffb1f1f637915d7ddb915e425c35a Mon Sep 17 00:00:00 2001 From: Chika Date: Wed, 26 Oct 2022 15:21:01 +0300 Subject: [PATCH] stores/calculation: support chain methods --- .../Form/Payments/PaymentsParams.jsx | 2 +- .../Calculation/addons/currency-addon.tsx | 2 +- .../config/elements-render/override.tsx | 2 +- process/calculate/reactions/validation.ts | 8 +- process/fingap/reactions/common.ts | 6 +- process/init/get-data/index.js | 2 +- process/init/set-values/reactions.ts | 7 +- process/lead-opportunity/reactions/common.ts | 22 ++-- process/lead-opportunity/reactions/urls.ts | 14 +-- process/payments/reactions.ts | 102 ++++++++--------- process/payments/validation.ts | 18 +-- process/price/reactions/computed.ts | 6 +- .../supplier-agent/lib/create-reactions.ts | 19 ++-- .../lib/fill-agents-from-lead.ts | 10 +- process/supplier-agent/reactions/agents.ts | 107 +++++++++--------- process/supplier-agent/reactions/supplier.ts | 33 +++--- stores/calculation/index.ts | 91 ++++++++------- stores/calculation/options/index.ts | 4 +- 18 files changed, 233 insertions(+), 222 deletions(-) diff --git a/Components/Calculation/Form/Payments/PaymentsParams.jsx b/Components/Calculation/Form/Payments/PaymentsParams.jsx index 4f3174f..97fd0d3 100644 --- a/Components/Calculation/Form/Payments/PaymentsParams.jsx +++ b/Components/Calculation/Form/Payments/PaymentsParams.jsx @@ -14,7 +14,7 @@ function PaymentsParams() { const [selectSeasonType, tbxParmentsDecreasePercent, selectHighSeasonStart] = renderedElements; const { $calculation } = useStore(); - const graphType = $calculation.getElementValue('radioGraphType'); + const graphType = $calculation.element('radioGraphType').getValue(); switch (graphType) { case 100_000_000: diff --git a/Components/Calculation/addons/currency-addon.tsx b/Components/Calculation/addons/currency-addon.tsx index b2499b7..368f804 100644 --- a/Components/Calculation/addons/currency-addon.tsx +++ b/Components/Calculation/addons/currency-addon.tsx @@ -14,7 +14,7 @@ const QUERY_GET_CURRENCY_SYMBOL = gql` const CurrencyAddon = observer(() => { const { $calculation } = useStore(); - const currencyid = $calculation.$values.getValue('supplierCurrency'); + const currencyid = $calculation.element('selectSupplierCurrency').getValue(); const { data } = useQuery< CRMTypes.GetCurrencySymbolQuery, diff --git a/Components/Calculation/config/elements-render/override.tsx b/Components/Calculation/config/elements-render/override.tsx index 2e08e79..cd9b208 100644 --- a/Components/Calculation/config/elements-render/override.tsx +++ b/Components/Calculation/config/elements-render/override.tsx @@ -180,7 +180,7 @@ const overrideRender: Partial> = { const Title = observer(() => { const { $calculation } = useStore(); - const graphType = $calculation.getElementValue('radioGraphType'); + const graphType = $calculation.element('radioGraphType').getValue(); switch (graphType) { case 100_000_001: diff --git a/process/calculate/reactions/validation.ts b/process/calculate/reactions/validation.ts index e6ed9f7..c13cc6f 100644 --- a/process/calculate/reactions/validation.ts +++ b/process/calculate/reactions/validation.ts @@ -15,11 +15,11 @@ export default function validationReactions(store: RootStore, apolloClient: Apol }, (hasErrors) => { if (hasErrors) { - $calculation.blockElement('btnCalculate'); - $calculation.blockElement('btnCreateKP'); + $calculation.$status.setStatus('btnCalculate', 'Disabled'); + $calculation.$status.setStatus('btnCreateKP', 'Disabled'); } else { - $calculation.unblockElement('btnCalculate'); - $calculation.unblockElement('btnCreateKP'); + $calculation.$status.setStatus('btnCalculate', 'Default'); + $calculation.$status.setStatus('btnCreateKP', 'Default'); } }, { diff --git a/process/fingap/reactions/common.ts b/process/fingap/reactions/common.ts index d63e2d6..5f3daca 100644 --- a/process/fingap/reactions/common.ts +++ b/process/fingap/reactions/common.ts @@ -53,7 +53,7 @@ export default function commonReactions( reaction( () => { const finGAPInsuranceCompany = $tables.insurance.getRowValue('fingap', 'insuranceCompany'); - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); return { finGAPInsuranceCompany, @@ -127,8 +127,8 @@ export default function commonReactions( const paymentsValues = toJS($tables.payments.values); const plPriceRub = $calculation.$values.getValue('plPriceRub'); const discountRub = $calculation.$values.getValue('discountRub'); - const firstPaymentRub = $calculation.getElementValue('tbxFirstPaymentRub'); - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + const firstPaymentRub = $calculation.element('tbxFirstPaymentRub').getValue(); + const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); return { finGAPInsuranceCompany, diff --git a/process/init/get-data/index.js b/process/init/get-data/index.js index a967552..ec6fcfd 100644 --- a/process/init/get-data/index.js +++ b/process/init/get-data/index.js @@ -10,7 +10,7 @@ export default function getData(apolloClient, store) { function setManyOptions(options) { Object.keys(options).forEach((elementName) => { const elementOptions = options[elementName]; - $calculation.$options.setOptions(elementName, elementOptions); + $calculation.element(elementName).setOptions(elementOptions); }); } diff --git a/process/init/set-values/reactions.ts b/process/init/set-values/reactions.ts index 83d04de..11c25c7 100644 --- a/process/init/set-values/reactions.ts +++ b/process/init/set-values/reactions.ts @@ -35,10 +35,9 @@ export default function setInitialValuesReactions( (x) => x?.isocurrencycode === 'RUB' ); - $calculation.setElementValue( - 'selectSupplierCurrency', - transactioncurrency_rub?.transactioncurrencyid - ); + $calculation + .element('selectSupplierCurrency') + .setValue(transactioncurrency_rub?.transactioncurrencyid); } ); } diff --git a/process/lead-opportunity/reactions/common.ts b/process/lead-opportunity/reactions/common.ts index 88fe70f..ab8df2d 100644 --- a/process/lead-opportunity/reactions/common.ts +++ b/process/lead-opportunity/reactions/common.ts @@ -31,10 +31,10 @@ export default function commonReactions(store: RootStore, apolloClient: ApolloCl makeDisposable( () => reaction( - () => $calculation.getElementValue('selectLead'), + () => $calculation.element('selectLead').getValue(), async (leadid) => { if (!leadid) { - $calculation.resetElementValue('selectOpportunity'); + $calculation.element('selectOpportunity').resetValue(); return; } @@ -52,9 +52,9 @@ export default function commonReactions(store: RootStore, apolloClient: ApolloCl }); if (lead?.evo_opportunityidData?.value) { - $calculation.setElementValue('selectOpportunity', lead?.evo_opportunityidData?.value); + $calculation.element('selectOpportunity').setValue(lead?.evo_opportunityidData?.value); } else { - $calculation.resetElementValue('selectOpportunity'); + $calculation.element('selectOpportunity').resetValue(); } } ), @@ -76,9 +76,9 @@ export default function commonReactions(store: RootStore, apolloClient: ApolloCl `; reaction( - () => $calculation.getElementValue('selectOpportunity'), + () => $calculation.element('selectOpportunity').getValue(), async (opportunityid) => { - const leadid = $calculation.getElementValue('selectLead'); + const leadid = $calculation.element('selectLead').getValue(); if (leadid) { const { @@ -94,7 +94,7 @@ export default function commonReactions(store: RootStore, apolloClient: ApolloCl }); if (!opportunityid && lead?.evo_opportunityidData?.value) { - $calculation.resetElementValue('selectLead'); + $calculation.element('selectLead').resetValue(); } } @@ -109,7 +109,7 @@ export default function commonReactions(store: RootStore, apolloClient: ApolloCl }, }); - $calculation.setElementValue('selectLead', data.opportunity?.evo_leadid); + $calculation.element('selectLead').setValue(data.opportunity?.evo_leadid); } } ); @@ -131,7 +131,7 @@ export default function commonReactions(store: RootStore, apolloClient: ApolloCl `; reaction( - () => $calculation.getElementValue('selectLead'), + () => $calculation.element('selectLead').getValue(), async (leadid) => { if (leadid) { const { @@ -146,9 +146,9 @@ export default function commonReactions(store: RootStore, apolloClient: ApolloCl }, }); - $calculation.setElementOptions('selectQuote', normalizeOptions(quotes)); + $calculation.element('selectQuote').setOptions(normalizeOptions(quotes)); } else { - $calculation.resetElement('selectQuote'); + $calculation.element('selectQuote').reset(); } } ); diff --git a/process/lead-opportunity/reactions/urls.ts b/process/lead-opportunity/reactions/urls.ts index cef94c6..477f719 100644 --- a/process/lead-opportunity/reactions/urls.ts +++ b/process/lead-opportunity/reactions/urls.ts @@ -12,10 +12,10 @@ export default function urlsReactions(store: RootStore, apolloClient: ApolloClie */ function makeLinkReaction(elementName: Elements, linkElementName: Elements, query: DocumentNode) { reaction( - () => $calculation.getElementValue(elementName), + () => $calculation.element(elementName).getValue(), (id) => { if (!id) { - $calculation.resetElementValue(linkElementName); + $calculation.element(linkElementName).resetValue(); return; } @@ -34,22 +34,22 @@ export default function urlsReactions(store: RootStore, apolloClient: ApolloClie .then(({ data }) => { clearTimeout(timeoutId); if (data.entity?.link) { - $calculation.setElementValue(linkElementName, data.entity?.link); + $calculation.element(linkElementName).setValue(data.entity?.link); } }) .catch(() => { clearTimeout(timeoutId); - $calculation.resetElementValue(linkElementName); + $calculation.element(linkElementName).resetValue(); }); } ); reaction( - () => $calculation.getElementValue(linkElementName), + () => $calculation.element(linkElementName).getValue(), (url) => { if (!url) { - $calculation.blockElement(linkElementName); + $calculation.element(linkElementName).block(); } else { - $calculation.unblockElement(linkElementName); + $calculation.element(linkElementName).unblock(); } } ); diff --git a/process/payments/reactions.ts b/process/payments/reactions.ts index db4d627..c48f993 100644 --- a/process/payments/reactions.ts +++ b/process/payments/reactions.ts @@ -22,14 +22,14 @@ export default function paymentsReactions( const { $calculation, $tables, $process } = store; reaction( - () => $calculation.getElementValue('tbxFirstPaymentPerc'), + () => $calculation.element('tbxFirstPaymentPerc').getValue(), (firstPaymentPerc) => { $tables.payments.setValue(0, firstPaymentPerc); } ); reaction( - () => $calculation.getElementValue('tbxLastPaymentPerc'), + () => $calculation.element('tbxLastPaymentPerc').getValue(), (lastPaymentPerc) => { const paymentsLength = $tables.payments.values.length; $tables.payments.setValue(paymentsLength - 1, lastPaymentPerc); @@ -43,8 +43,8 @@ export default function paymentsReactions( () => reaction( () => { - const graphType = $calculation.getElementValue('radioGraphType'); - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + const graphType = $calculation.element('radioGraphType').getValue(); + const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); return { graphType, @@ -63,8 +63,8 @@ export default function paymentsReactions( }) ); - const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); - const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); + const firstPaymentPerc = $calculation.element('tbxFirstPaymentPerc').getValue(); + const lastPaymentPerc = $calculation.element('tbxLastPaymentPerc').getValue(); $tables.payments.setRows([ { @@ -93,11 +93,11 @@ export default function paymentsReactions( () => reaction( () => { - const graphType = $calculation.getElementValue('radioGraphType'); - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); - const parmentsDecreasePercent = $calculation.getElementValue( - 'tbxParmentsDecreasePercent' - ); + const graphType = $calculation.element('radioGraphType').getValue(); + const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); + const parmentsDecreasePercent = $calculation + .element('tbxParmentsDecreasePercent') + .getValue(); return { graphType, @@ -121,8 +121,8 @@ export default function paymentsReactions( } ); - const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); - const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); + const firstPaymentPerc = $calculation.element('tbxFirstPaymentPerc').getValue(); + const lastPaymentPerc = $calculation.element('tbxLastPaymentPerc').getValue(); $tables.payments.setRows([ { @@ -148,8 +148,8 @@ export default function paymentsReactions( () => reaction( () => { - const graphType = $calculation.getElementValue('radioGraphType'); - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + const graphType = $calculation.element('radioGraphType').getValue(); + const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); return { graphType, @@ -183,8 +183,8 @@ export default function paymentsReactions( }) ); - const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); - const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); + const firstPaymentPerc = $calculation.element('tbxFirstPaymentPerc').getValue(); + const lastPaymentPerc = $calculation.element('tbxLastPaymentPerc').getValue(); $tables.payments.setRows([ { @@ -215,7 +215,7 @@ export default function paymentsReactions( reaction( () => { - const graphType = $calculation.getElementValue('radioGraphType'); + const graphType = $calculation.element('radioGraphType').getValue(); return graphType; }, @@ -231,7 +231,7 @@ export default function paymentsReactions( seasonSeasonTypes.has(option.value) ); } - $calculation.setElementOptions('selectSeasonType', selectSeasonTypeOptions); + $calculation.element('selectSeasonType').setOptions(selectSeasonTypeOptions); } ); @@ -246,9 +246,9 @@ export default function paymentsReactions( () => reaction( () => { - const degressionType = $calculation.getElementValue('selectSeasonType'); - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); - const graphType = $calculation.getElementValue('radioGraphType'); + const degressionType = $calculation.element('selectSeasonType').getValue(); + const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); + const graphType = $calculation.element('radioGraphType').getValue(); return { degressionType, @@ -317,8 +317,8 @@ export default function paymentsReactions( } } - const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); - const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); + const firstPaymentPerc = $calculation.element('tbxFirstPaymentPerc').getValue(); + const lastPaymentPerc = $calculation.element('tbxLastPaymentPerc').getValue(); $tables.payments.setRows([ { @@ -341,9 +341,9 @@ export default function paymentsReactions( () => reaction( () => { - const graphType = $calculation.getElementValue('radioGraphType'); + const graphType = $calculation.element('radioGraphType').getValue(); const payments = toJS($tables.payments.values); - const degressionType = $calculation.getElementValue('selectSeasonType'); + const degressionType = $calculation.element('selectSeasonType').getValue(); return { graphType, @@ -365,8 +365,8 @@ export default function paymentsReactions( return value; }); - const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); - const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); + const firstPaymentPerc = $calculation.element('tbxFirstPaymentPerc').getValue(); + const lastPaymentPerc = $calculation.element('tbxLastPaymentPerc').getValue(); $tables.payments.setValues([firstPaymentPerc, ...payments, lastPaymentPerc]); } @@ -385,7 +385,7 @@ export default function paymentsReactions( reaction( () => { - const graphType = $calculation.getElementValue('radioGraphType'); + const graphType = $calculation.element('radioGraphType').getValue(); return { graphType, @@ -394,8 +394,8 @@ export default function paymentsReactions( ({ graphType }) => { if (graphType !== 100_000_003) return; - const seasonType = $calculation.getElementValue('selectSeasonType'); - const highSeasonStart = $calculation.getElementValue('selectHighSeasonStart'); + const seasonType = $calculation.element('selectSeasonType').getValue(); + const highSeasonStart = $calculation.element('selectHighSeasonStart').getValue(); if (!seasonType || !highSeasonStart) { $tables.payments.setValues([]); @@ -405,16 +405,16 @@ export default function paymentsReactions( reaction( () => { - const seasonType = $calculation.getElementValue('selectSeasonType'); + const seasonType = $calculation.element('selectSeasonType').getValue(); return seasonType; }, (seasonType) => { - const graphType = $calculation.getElementValue('radioGraphType'); + const graphType = $calculation.element('radioGraphType').getValue(); if (graphType !== 100_000_003) return; if (!seasonType) { - $calculation.resetElement('selectHighSeasonStart'); + $calculation.element('selectHighSeasonStart').reset(); return; } @@ -423,7 +423,7 @@ export default function paymentsReactions( (option) => !seasonsConstants.FORBIDDEN_HIGH_SEASON_START[seasonType].includes(option.label) ); - $calculation.setElementOptions('selectHighSeasonStart', highSeasonStartOptions); + $calculation.element('selectHighSeasonStart').setOptions(highSeasonStartOptions); } ); @@ -445,9 +445,9 @@ export default function paymentsReactions( () => reaction( () => { - const seasonType = $calculation.getElementValue('selectSeasonType'); - const highSeasonStartOption = $calculation.getElementOption('selectHighSeasonStart'); - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + const seasonType = $calculation.element('selectSeasonType').getValue(); + const highSeasonStartOption = $calculation.element('selectHighSeasonStart').getOption(); + const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); return { seasonType, @@ -456,7 +456,7 @@ export default function paymentsReactions( }; }, ({ seasonType, highSeasonStartOption, leasingPeriod }) => { - const graphType = $calculation.getElementValue('radioGraphType'); + const graphType = $calculation.element('radioGraphType').getValue(); if (graphType !== 100_000_003) return; if (!seasonType || !highSeasonStartOption) { @@ -475,8 +475,8 @@ export default function paymentsReactions( shift(seasons, shiftNumber) ); const rows: Row[] = generateSeasonPaymentsRows(seasonType, shiftNumber, payments); - const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); - const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); + const firstPaymentPerc = $calculation.element('tbxFirstPaymentPerc').getValue(); + const lastPaymentPerc = $calculation.element('tbxLastPaymentPerc').getValue(); $tables.payments.setRows([ { @@ -504,11 +504,11 @@ export default function paymentsReactions( return seasons; }, (nextSeasons, prevSeasons) => { - const graphType = $calculation.getElementValue('radioGraphType'); + const graphType = $calculation.element('radioGraphType').getValue(); if (graphType !== 100_000_003) return; - const seasonType = $calculation.getElementValue('selectSeasonType'); - const highSeasonStartOption = $calculation.getElementOption('selectHighSeasonStart'); + const seasonType = $calculation.element('selectSeasonType').getValue(); + const highSeasonStartOption = $calculation.element('selectHighSeasonStart').getOption(); if (!seasonType || !highSeasonStartOption) return; const shiftNumber = Number.parseInt(highSeasonStartOption.label, 10) - 2; @@ -525,14 +525,14 @@ export default function paymentsReactions( values[positionIndex] = unshiftedNextSeasons[changeIndex]; const seasons = seasonsTools.generateSeasons(seasonType, values); - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); const payments = seasonsTools.generateSeasonsPayments( leasingPeriod, shift(seasons, shiftNumber) ); const rows: Row[] = generateSeasonPaymentsRows(seasonType, shiftNumber, payments); - const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); - const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); + const firstPaymentPerc = $calculation.element('tbxFirstPaymentPerc').getValue(); + const lastPaymentPerc = $calculation.element('tbxLastPaymentPerc').getValue(); $tables.payments.setRows([ { @@ -562,10 +562,10 @@ export default function paymentsReactions( reaction( () => { const payments = toJS($tables.payments.values); - const graphType = $calculation.getElementValue('radioGraphType'); - const seasonType = $calculation.getElementValue('selectSeasonType'); - const highSeasonStart = $calculation.getElementValue('selectHighSeasonStart'); - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + const graphType = $calculation.element('radioGraphType').getValue(); + const seasonType = $calculation.element('selectSeasonType').getValue(); + const highSeasonStart = $calculation.element('selectHighSeasonStart').getValue(); + const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); return { payments, diff --git a/process/payments/validation.ts b/process/payments/validation.ts index eb75d22..3dd671c 100644 --- a/process/payments/validation.ts +++ b/process/payments/validation.ts @@ -7,10 +7,10 @@ import { areEqual, isSorted } from 'tools/array'; import { SEASONS_PERIODS, SEASONS_PERIOD_NUMBER } from './lib/seasons-constants'; export default function validatePaymentsTable({ $calculation, $tables }: RootStore) { - switch ($calculation.getElementValue('radioGraphType')) { + switch ($calculation.element('radioGraphType').getValue()) { // Дегрессия case 100_000_001: - if (!$calculation.getElementValue('selectSeasonType')) { + if (!$calculation.element('selectSeasonType').getValue()) { return 'Не выбран тип дегрессии'; } /** @@ -18,7 +18,7 @@ export default function validatePaymentsTable({ $calculation, $tables }: RootSto */ { const MIN_PAYMENT = 3; - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); const targetPayments = $tables.payments.values.slice(1, leasingPeriod - 1); if (!targetPayments.every((payment) => payment >= MIN_PAYMENT)) { @@ -30,7 +30,7 @@ export default function validatePaymentsTable({ $calculation, $tables }: RootSto * при Дегрессии все значения не должны быть равны друг другу + что при Легком старте 2,3 и 4 платежи не должны быть равны 100 */ { - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); const targetPayments = $tables.payments.values.slice(1, leasingPeriod - 1); if (new Set(targetPayments).size === 1) { @@ -42,7 +42,7 @@ export default function validatePaymentsTable({ $calculation, $tables }: RootSto * Проверка на возрастание */ { - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); const targetPayments = $tables.payments.values.slice(1, leasingPeriod - 1); for (let i = 2; i < targetPayments.length - 1; i += 1) { @@ -73,7 +73,7 @@ export default function validatePaymentsTable({ $calculation, $tables }: RootSto * иначе не осуществлять Расчет графика и выводить ошибку "Нельзя осуществить расчет - указана очень жетская дегрессия. Не менее чем у 4х платежей "Соотношение платежа" должно не отличаться между самой", */ { - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); const targetPayments = $tables.payments.values.slice(1, leasingPeriod - 1); const counts = counting(targetPayments, (v) => v); if (Object.values(counts).filter((count) => count > 1).length < 2) { @@ -84,17 +84,17 @@ export default function validatePaymentsTable({ $calculation, $tables }: RootSto break; case 100_000_003: { - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); if (leasingPeriod < 14) { return 'При сезонном виде графика срок лизинга должен быть больше 14 месяцев'; } - const seasonType = $calculation.getElementValue('selectSeasonType'); + const seasonType = $calculation.element('selectSeasonType').getValue(); if (!seasonType) { return 'Не выбран тип сезонности'; } - const highSeasonStartOption = $calculation.getElementOption('selectHighSeasonStart'); + const highSeasonStartOption = $calculation.element('selectHighSeasonStart').getOption(); if (!highSeasonStartOption) { return 'Не выбрано смещение сезонности'; } diff --git a/process/price/reactions/computed.ts b/process/price/reactions/computed.ts index d05d088..c2c20be 100644 --- a/process/price/reactions/computed.ts +++ b/process/price/reactions/computed.ts @@ -31,9 +31,9 @@ export default function computedReactions(store: RootStore, apolloClient: Apollo autorun( async () => { - const supplierCurrencyId = $calculation.getElementValue('selectSupplierCurrency'); - const leaseObjectPrice = $calculation.getElementValue('tbxLeaseObjectPrice'); - const supplierDiscountRub = $calculation.getElementValue('tbxSupplierDiscountRub'); + const supplierCurrencyId = $calculation.element('selectSupplierCurrency').getValue(); + const leaseObjectPrice = $calculation.element('tbxLeaseObjectPrice').getValue(); + const supplierDiscountRub = $calculation.element('tbxSupplierDiscountRub').getValue(); if (!supplierCurrencyId) { $calculation.$values.setValue('plPriceRub', leaseObjectPrice); diff --git a/process/supplier-agent/lib/create-reactions.ts b/process/supplier-agent/lib/create-reactions.ts index 94c209d..6a683a4 100644 --- a/process/supplier-agent/lib/create-reactions.ts +++ b/process/supplier-agent/lib/create-reactions.ts @@ -26,10 +26,10 @@ export function fillAgentRewardReaction( makeDisposable( () => reaction( - () => $calculation.getElementValue(agentField), + () => $calculation.element(agentField).getValue(), async (agentId) => { if (!agentId) { - $calculation.resetElement(rewardConditionField); + $calculation.element(rewardConditionField).reset(); return; } @@ -47,10 +47,9 @@ export function fillAgentRewardReaction( }); if (evo_reward_conditions?.length) { - $calculation.setElementOptions( - rewardConditionField, - normalizeOptions(evo_reward_conditions) - ); + $calculation + .element(rewardConditionField) + .setOptions(normalizeOptions(evo_reward_conditions)); } } ), @@ -72,10 +71,10 @@ export function fillAgentRewardSummReaction( makeDisposable( () => reaction( - () => $calculation.getElementValue(rewardConditionField), + () => $calculation.element(rewardConditionField).getValue(), async (rewardConditionId) => { if (!rewardConditionId) { - $calculation.resetElement(rewardSummField); + $calculation.element(rewardSummField).reset(); return; } @@ -92,9 +91,9 @@ export function fillAgentRewardSummReaction( }, }); - $calculation.setElementValue(rewardSummField, evo_reward_condition?.evo_reward_summ); + $calculation.element(rewardSummField).setValue(evo_reward_condition?.evo_reward_summ); - $calculation.unblockElement(rewardSummField); + $calculation.element(rewardSummField).unblock(); } ), () => $process.has('LoadKP') diff --git a/process/supplier-agent/lib/fill-agents-from-lead.ts b/process/supplier-agent/lib/fill-agents-from-lead.ts index a308318..d92aebd 100644 --- a/process/supplier-agent/lib/fill-agents-from-lead.ts +++ b/process/supplier-agent/lib/fill-agents-from-lead.ts @@ -18,7 +18,7 @@ function makeFillAgentFromLead( leadid: string | null ) { if (!leadid) { - $calculation.resetElement(elementName); + $calculation.element(elementName).reset(); return; } @@ -43,11 +43,13 @@ function makeFillAgentFromLead( }); if (agent) { - $calculation.setElementOptions(elementName, normalizeOptions([agent])); - $calculation.setElementValue(elementName, agent.value); + $calculation + .element(elementName) + .setOptions(normalizeOptions([agent])) + .setValue(agent.value); } } else { - $calculation.resetElement(elementName); + $calculation.element(elementName).reset(); } }; } diff --git a/process/supplier-agent/reactions/agents.ts b/process/supplier-agent/reactions/agents.ts index e45decf..1dbcddd 100644 --- a/process/supplier-agent/reactions/agents.ts +++ b/process/supplier-agent/reactions/agents.ts @@ -22,7 +22,7 @@ export function fillReactions(store: RootStore, apolloClient: ApolloClient reaction( - () => $calculation.getElementValue('selectLead'), + () => $calculation.element('selectLead').getValue(), (leadid) => { fillAgentsFromLead.fillIndAgent(store, apolloClient, leadid); fillAgentsFromLead.fillCalcDoubleAgent(store, apolloClient, leadid); @@ -122,10 +122,10 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient $calculation.getElementValue('selectDealerRewardCondition'), + () => $calculation.element('selectDealerRewardCondition').getValue(), async (rewardConditionId) => { function fillAgents() { - const leadid = $calculation.getElementValue('selectLead'); + const leadid = $calculation.element('selectLead').getValue(); fillAgentsFromLead.fillIndAgent(store, apolloClient, leadid); fillAgentsFromLead.fillCalcDoubleAgent(store, apolloClient, leadid); fillAgentsFromLead.fillCalcBroker(store, apolloClient, leadid); @@ -151,11 +151,11 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient $calculation.getElementValue('selectDealerBrokerRewardCondition'), + () => $calculation.element('selectDealerBrokerRewardCondition').getValue(), async (rewardConditionId) => { function fillAgents() { - const leadid = $calculation.getElementValue('selectLead'); + const leadid = $calculation.element('selectLead').getValue(); fillAgentsFromLead.fillIndAgent(store, apolloClient, leadid); fillAgentsFromLead.fillCalcDoubleAgent(store, apolloClient, leadid); fillAgentsFromLead.fillCalcBroker(store, apolloClient, leadid); @@ -213,14 +213,13 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient $calculation.getElementValue('selectIndAgentRewardCondition'), + () => $calculation.element('selectIndAgentRewardCondition').getValue(), async (rewardConditionId) => { function fillAgents() { - const leadid = $calculation.getElementValue('selectLead'); + const leadid = $calculation.element('selectLead').getValue(); fillAgentsFromLead.fillCalcDoubleAgent(store, apolloClient, leadid); fillAgentsFromLead.fillCalcBroker(store, apolloClient, leadid); fillAgentsFromLead.fillFinDepartment(store, apolloClient, leadid); @@ -277,14 +276,13 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient $calculation.getElementValue('selectCalcDoubleAgentRewardCondition'), + () => $calculation.element('selectCalcDoubleAgentRewardCondition').getValue(), async (rewardConditionId) => { function fillAgents() { - const leadid = $calculation.getElementValue('selectLead'); + const leadid = $calculation.element('selectLead').getValue(); fillAgentsFromLead.fillIndAgent(store, apolloClient, leadid); fillAgentsFromLead.fillCalcBroker(store, apolloClient, leadid); fillAgentsFromLead.fillFinDepartment(store, apolloClient, leadid); @@ -341,15 +339,14 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient $calculation.getElementValue('selectCalcBrokerRewardCondition'), + () => $calculation.element('selectCalcBrokerRewardCondition').getValue(), async (rewardConditionId) => { function fillAgents() { - const leadid = $calculation.getElementValue('selectLead'); + const leadid = $calculation.element('selectLead').getValue(); fillAgentsFromLead.fillIndAgent(store, apolloClient, leadid); fillAgentsFromLead.fillCalcDoubleAgent(store, apolloClient, leadid); fillAgentsFromLead.fillFinDepartment(store, apolloClient, leadid); @@ -406,15 +403,14 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient $calculation.getElementValue('selectFinDepartmentRewardCondtion'), + () => $calculation.element('selectFinDepartmentRewardCondtion').getValue(), async (rewardConditionId) => { function fillAgents() { - const leadid = $calculation.getElementValue('selectLead'); + const leadid = $calculation.element('selectLead').getValue(); fillAgentsFromLead.fillIndAgent(store, apolloClient, leadid); fillAgentsFromLead.fillCalcDoubleAgent(store, apolloClient, leadid); fillAgentsFromLead.fillFinDepartment(store, apolloClient, leadid); @@ -471,15 +467,14 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient reaction( - () => $calculation.getElementValue('selectDealer'), + () => $calculation.element('selectDealer').getValue(), async (dealerId) => { if (!dealerId) { - $calculation.resetElement('selectDealerPerson'); - $calculation.resetElement('selectDealerBroker'); + $calculation.element('selectDealerPerson').reset(); + $calculation.element('selectDealerBroker').reset(); return; } @@ -57,8 +57,10 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient reaction( - () => $calculation.getElementValue('selectDealerPerson'), + () => $calculation.element('selectDealerPerson').getValue(), async (dealerPersonId) => { if (!dealerPersonId) { return; @@ -109,14 +111,13 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient $calculation.getElementValue('selectDealerRewardCondition'), + () => $calculation.element('selectDealerRewardCondition').getValue(), (dealerRewardConditionId) => { if (dealerRewardConditionId) { - $calculation.resetElementValue('selectDealerBroker'); + $calculation.element('selectDealerBroker').resetValue(); } } ); reaction( - () => $calculation.getElementValue('selectDealerBrokerRewardCondition'), + () => $calculation.element('selectDealerBrokerRewardCondition').getValue(), (dealerBrokerRewardConditionId) => { if (dealerBrokerRewardConditionId) { - $calculation.resetElementValue('selectDealerPerson'); + $calculation.element('selectDealerPerson').resetValue(); } } ); diff --git a/stores/calculation/index.ts b/stores/calculation/index.ts index 101e45e..7278248 100644 --- a/stores/calculation/index.ts +++ b/stores/calculation/index.ts @@ -1,5 +1,4 @@ /* eslint-disable import/no-cycle */ -import type { Elements as ElementsActions } from 'Components/Calculation/config/map/actions'; import type * as Values from 'Components/Calculation/config/map/values'; import { getValueName } from 'Components/Calculation/config/map/values'; import type { BaseOption } from 'Elements/types'; @@ -22,51 +21,67 @@ export default class CalculationStore { this.$validation = new ValidationStore(rootStore); } - resetElement = (elementName: Values.Elements) => { - this.resetElementValue(elementName); - this.$options.resetOptions(elementName); - this.$status.resetStatus(elementName); - this.$validation.clearErrors(elementName); - }; + element = (elementName: E) => ({ + reset: () => { + const valueName = getValueName(elementName); + this.$values.resetValue(valueName); - blockElement = (elementName: Values.Elements | ElementsActions) => { - this.$status.setStatus(elementName, 'Disabled'); - }; + this.$options.resetOptions(elementName); + this.$status.resetStatus(elementName); + this.$validation.clearErrors(elementName); - unblockElement = (elementName: Values.Elements | ElementsActions) => { - this.$status.setStatus(elementName, 'Default'); - }; + return this.element(elementName); + }, - getElementValue(elementName: E) { - const valueName = getValueName(elementName); + block: () => { + this.$status.setStatus(elementName, 'Disabled'); - return this.$values.getValue(valueName) as Values.ElementsTypes[E]; - } + return this.element(elementName); + }, - setElementValue = (elementName: E, value: Values.ElementsTypes[E]) => { - const valueName = getValueName(elementName); - this.$values.setValue(valueName, value); - }; + unblock: () => { + this.$status.setStatus(elementName, 'Default'); - resetElementValue = (elementName: Values.Elements) => { - const valueName = getValueName(elementName); - this.$values.resetValue(valueName); - }; + return this.element(elementName); + }, - setElementOptions = ( - elementName: T, - options: BaseOption[] - ) => { - this.$options.setOptions(elementName, options); - }; + getValue: () => { + const valueName = getValueName(elementName); - resetElementOptions = (elementName: T) => { - this.$options.resetOptions(elementName); - }; + return this.$values.getValue(valueName) as Values.ElementsTypes[E]; + }, - getElementOption(elementName: T) { - const value = this.getElementValue(elementName); + setValue: (value: Values.ElementsTypes[E]) => { + const valueName = getValueName(elementName); + this.$values.setValue(valueName, value); - return this.$options.getOptions(elementName)?.find((x) => x.value === value); - } + return this.element(elementName); + }, + + resetValue: () => { + const valueName = getValueName(elementName); + this.$values.resetValue(valueName); + + return this.element(elementName); + }, + + getOption: () => { + const valueName = getValueName(elementName); + const value = this.$values.getValue(valueName) as Values.ElementsTypes[E]; + + return this.$options.getOptions(elementName)?.find((x) => x.value === value); + }, + + setOptions: (options: BaseOption[]) => { + this.$options.setOptions(elementName, options); + + return this.element(elementName); + }, + + resetOptions: () => { + this.$options.resetOptions(elementName); + + return this.element(elementName); + }, + }); } diff --git a/stores/calculation/options/index.ts b/stores/calculation/options/index.ts index 3b2811c..2bbe7e1 100644 --- a/stores/calculation/options/index.ts +++ b/stores/calculation/options/index.ts @@ -30,13 +30,13 @@ export default class OptionsStore { /** * Проверяем, что значение есть в новом списке, иначе сбрасываем значение */ - const value = this.root.$calculation.getElementValue(elementName); + const value = this.root.$calculation.element(elementName).getValue(); if ( // eslint-disable-next-line operator-linebreak !this.options[elementName]?.length || !this.options[elementName].some((x) => x.value === value) ) { - this.root.$calculation.resetElementValue(elementName); + this.root.$calculation.element(elementName).resetValue(); } };