156 lines
3.9 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';
dayjs.extend(utc);
export default function valuesReactions({ store, apolloClient }: ProcessContext) {
const { $calculation, $process } = store;
const { getTarifs, getRates } = 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 (tarif) => {
if (!tarif) {
$calculation.element('selectRate').resetOptions();
return;
}
const { evo_rates } = await getRates({ tarif });
$calculation.element('selectRate').setOptions(normalizeOptions(evo_rates));
}
);
disposableReaction(
() => $process.has('LoadKP'),
() => $calculation.element('selectTarif').getValue(),
async (tarif) => {
if (!tarif) {
$calculation.element('selectRate').resetValue();
return;
}
const { evo_rate } = await getRates({ tarif });
$calculation.element('selectRate').setValue(evo_rate?.evo_rateid || null);
}
);
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);
}
}
);
});
}