2023-05-12 12:38:35 +03:00

169 lines
4.3 KiB
TypeScript

import helper from '../lib/helper';
import type { Elements } from '@/Components/Calculation/config/map/values';
import * as CRMTypes from '@/graphql/crm.types';
import type { ProcessContext } from '@/process/types';
import { normalizeOptions } from '@/utils/entity';
import { disposableReaction } from '@/utils/mobx';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { comparer, reaction } from 'mobx';
import { first, sort } from 'radash';
dayjs.extend(utc);
export default function valuesReactions({ store, apolloClient }: ProcessContext) {
const { $calculation, $process } = store;
const { getTarifs } = helper({ apolloClient });
disposableReaction(
() => $process.has('LoadKP'),
() =>
$calculation.$values.getValues([
'product',
'leasingPeriod',
'leaseObjectUsed',
'deliveryTime',
'firstPaymentPerc',
'lastPaymentPerc',
'leaseObjectType',
]),
async (values) => {
const { evo_tarif, evo_tarifs } = await getTarifs(values);
$calculation.element('selectTarif').setOptions(normalizeOptions(evo_tarifs)).resetValue();
$calculation.element('selectTarif').setValue(evo_tarif?.evo_tarifid || null);
},
{
delay: 10,
equals: comparer.shallow,
fireImmediately: true,
}
);
disposableReaction(
() => $process.has('LoadKP'),
() => $calculation.element('selectTarif').getValue(),
async (tarifId) => {
if (!tarifId) {
$calculation.element('tbxIRR_Perc').resetValue();
return;
}
const {
data: { evo_tarif },
} = await apolloClient.query({
fetchPolicy: 'network-only',
query: CRMTypes.GetTarifDocument,
variables: {
tarifId,
},
});
if (evo_tarif?.evo_irr) {
$calculation.element('tbxIRR_Perc').setValue(evo_tarif?.evo_irr);
} else {
$calculation.element('tbxIRR_Perc').resetValue();
}
}
);
reaction(
() => $calculation.element('selectTarif').getValue(),
async (tarifId) => {
if (!tarifId) {
$calculation.element('selectRate').resetValue();
return;
}
const currentDate = dayjs().utc(false).toISOString();
const {
data: { evo_rates },
} = await apolloClient.query({
fetchPolicy: 'network-only',
query: CRMTypes.GetRatesDocument,
variables: {
currentDate,
},
});
$calculation.element('selectRate').setOptions(normalizeOptions(evo_rates));
const {
data: { evo_tarif },
} = await apolloClient.query({
query: CRMTypes.GetTarifDocument,
variables: { tarifId },
});
const targetRate =
evo_tarif?.evo_rates &&
first(sort(evo_tarif?.evo_rates, (x) => dayjs(x?.evo_datefrom).date()));
if (targetRate) {
$calculation.element('selectRate').setValue(targetRate?.evo_rateid);
} else {
const baseRate = evo_rates?.find((x) => x?.evo_id === 'BASE');
if (baseRate) $calculation.element('selectRate').setValue(baseRate?.value);
}
}
);
reaction(
() => $calculation.element('selectRate').getValue(),
async (rateId) => {
if (!rateId) {
$calculation.element('tbxCreditRate').resetValue();
return;
}
const {
data: { evo_rate },
} = await apolloClient.query({
fetchPolicy: 'network-only',
query: CRMTypes.GetRateDocument,
variables: {
rateId,
},
});
if (evo_rate?.evo_base_rate === null || evo_rate?.evo_base_rate === undefined) {
$calculation.element('tbxCreditRate').resetValue();
return;
}
$calculation.element('tbxCreditRate').setValue(evo_rate?.evo_base_rate);
}
);
(
[
'selectProduct',
'selectLeaseObjectType',
'selectBrand',
'selectModel',
'selectConfiguration',
'selectTracker',
'selectTelematic',
'selectTechnicalCard',
'selectFuelCard',
'selectRegistration',
'selectTownRegistration',
'radioCalcType',
] as Elements[]
).forEach((elementName) => {
reaction(
() => $calculation.element(elementName).getOptions(),
(options) => {
if (options.length === 1) {
$calculation.element(elementName).setValue(options[0]?.value);
}
}
);
});
}