64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
import type { ProcessContext } from '../types';
|
|
import { toJS } from 'mobx';
|
|
import notification from 'ui/elements/notification';
|
|
|
|
const key = 'ACTION_CALCULATE';
|
|
const errorMessage = 'Ошибка во время расчета графика!';
|
|
const successMessage = 'Расчет графика завершен успешно!';
|
|
|
|
export async function action({ store, trpcClient }: ProcessContext) {
|
|
const { $calculation, $tables, $results } = store;
|
|
|
|
$calculation.$status.setStatus('btnCalculate', 'Loading');
|
|
$calculation.$status.setStatus('btnCreateKP', 'Loading');
|
|
$calculation.$status.setStatus('btnCreateKPMini', 'Loading');
|
|
$results.clear();
|
|
|
|
const values = $calculation.$values.getValues();
|
|
|
|
const insurance = {
|
|
fingap: toJS($tables.insurance.row('fingap').getValues()),
|
|
kasko: toJS($tables.insurance.row('kasko').getValues()),
|
|
osago: toJS($tables.insurance.row('osago').getValues()),
|
|
};
|
|
|
|
const payments = toJS($tables.payments.values);
|
|
|
|
trpcClient.calculate
|
|
.mutate({
|
|
insurance: { values: insurance },
|
|
payments: { values: payments },
|
|
values,
|
|
})
|
|
.then((res) => {
|
|
if (res.success === false) {
|
|
notification.error({
|
|
description: res.error,
|
|
key,
|
|
message: errorMessage,
|
|
});
|
|
} else {
|
|
$results.setPayments(res.data.resultPayments);
|
|
$results.setValues(res.data.resultValues);
|
|
$calculation.$values.setValues(res.data.values);
|
|
|
|
notification.success({
|
|
key,
|
|
message: successMessage,
|
|
});
|
|
}
|
|
})
|
|
.catch((error) => {
|
|
notification.error({
|
|
description: JSON.stringify(error),
|
|
key,
|
|
message: errorMessage,
|
|
});
|
|
})
|
|
.finally(() => {
|
|
$calculation.$status.setStatus('btnCalculate', 'Default');
|
|
$calculation.$status.setStatus('btnCreateKP', 'Default');
|
|
$calculation.$status.setStatus('btnCreateKPMini', 'Default');
|
|
});
|
|
}
|