From f264e2366af6155cb5440d0511dde7a0a6d04ef7 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Tue, 7 Feb 2023 14:57:52 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=D0=B8=20=D1=84=D0=B8=D0=BB=D1=8C=D1=82=D1=80=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8E=20selectLeasingWithoutKasko?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leasing-without-kasko/reactions/common.ts | 94 +++++++++++-------- 1 file changed, 54 insertions(+), 40 deletions(-) diff --git a/apps/web/process/leasing-without-kasko/reactions/common.ts b/apps/web/process/leasing-without-kasko/reactions/common.ts index c81842d..5e7cdd2 100644 --- a/apps/web/process/leasing-without-kasko/reactions/common.ts +++ b/apps/web/process/leasing-without-kasko/reactions/common.ts @@ -6,7 +6,7 @@ import { gql } from '@apollo/client'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; import { autorun, reaction } from 'mobx'; -import { uid } from 'radash'; +import { get, pick, uid } from 'radash'; import { normalizeOptions } from 'tools'; import notification from 'ui/elements/notification'; @@ -73,22 +73,22 @@ export default function commonReactions({ store, apolloClient }: ReactionsContex } ); - autorun(async () => { - const currentDate = dayjs().utc(false).toISOString(); + reaction( + () => { + const { values } = $calculation.$values; - const { - data: { evo_addproduct_types }, - } = await apolloClient.query< - CRMTypes.GetLeasingWithoutKaskoOptionsQuery, - CRMTypes.GetLeasingWithoutKaskoOptionsQueryVariables - >({ - query: QUERY_GET_LEASING_WITHOUT_KASKO_OPTIONS, - variables: { - currentDate, - }, - }); - - const { + return pick(values, [ + 'plPriceRub', + 'discountRub', + 'importProgramSum', + 'leasingPeriod', + 'addEquipmentPrice', + 'leaseObjectType', + 'firstPaymentPerc', + 'model', + ]); + }, + async ({ plPriceRub, discountRub, importProgramSum, @@ -97,30 +97,44 @@ export default function commonReactions({ store, apolloClient }: ReactionsContex leaseObjectType, firstPaymentPerc, model: modelId, - } = $calculation.$values.values; + }) => { + const currentDate = dayjs().utc(false).toISOString(); - const options = evo_addproduct_types?.filter( - (x) => - x?.evo_max_period && - x.evo_max_period >= leasingPeriod && - x?.evo_min_period && - x.evo_min_period <= leasingPeriod && - x?.evo_max_price && - x.evo_max_price >= plPriceRub - discountRub - importProgramSum + addEquipmentPrice && - x?.evo_min_price && - x.evo_min_price <= plPriceRub - discountRub - importProgramSum + addEquipmentPrice && - x.evo_leasingobject_types?.find( - (evo_leasingobject_type) => - evo_leasingobject_type?.evo_leasingobject_typeid === leaseObjectType - ) && - x.evo_visible_calc && - x.evo_min_first_payment_perc && - x.evo_min_first_payment_perc <= firstPaymentPerc && - x.evo_max_first_payment_perc && - x.evo_max_first_payment_perc >= firstPaymentPerc && - !x.evo_models?.map((evo_model) => evo_model?.evo_modelid).includes(modelId) - ); + const { + data: { evo_addproduct_types }, + } = await apolloClient.query< + CRMTypes.GetLeasingWithoutKaskoOptionsQuery, + CRMTypes.GetLeasingWithoutKaskoOptionsQueryVariables + >({ + query: QUERY_GET_LEASING_WITHOUT_KASKO_OPTIONS, + variables: { + currentDate, + }, + }); - $calculation.element('selectLeasingWithoutKasko').setOptions(normalizeOptions(options)); - }); + const options = evo_addproduct_types?.filter( + (x) => + // eslint-disable-next-line @typescript-eslint/prefer-optional-chain + x && + Boolean(x.evo_max_period !== null && x.evo_max_period >= leasingPeriod) && + Boolean(x.evo_min_period !== null && x.evo_min_period <= leasingPeriod) && + x.evo_max_price !== null && + x.evo_max_price >= plPriceRub - discountRub - importProgramSum + addEquipmentPrice && + x.evo_min_price !== null && + x.evo_min_price <= plPriceRub - discountRub - importProgramSum + addEquipmentPrice && + x.evo_leasingobject_types?.find( + (evo_leasingobject_type) => + evo_leasingobject_type?.evo_leasingobject_typeid === leaseObjectType + ) && + x.evo_visible_calc && + x.evo_min_first_payment_perc !== null && + x.evo_min_first_payment_perc <= firstPaymentPerc && + x.evo_max_first_payment_perc !== null && + x.evo_max_first_payment_perc >= firstPaymentPerc && + !x.evo_models?.map((evo_model) => evo_model?.evo_modelid).includes(modelId) + ); + + $calculation.element('selectLeasingWithoutKasko').setOptions(normalizeOptions(options)); + } + ); }