From 0246916fcaeaae8d7863d1d47d2d56ba602a7bf7 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Wed, 24 May 2023 12:55:53 +0300 Subject: [PATCH] process/calculate: reset results on change form values --- apps/web/process/calculate/action.ts | 6 ++++- .../web/process/calculate/reactions/common.ts | 25 +++++++++++++++++-- apps/web/stores/process/index.ts | 2 +- apps/web/stores/results/index.ts | 14 +++++++++++ apps/web/stores/tables/payments/index.ts | 6 ++++- 5 files changed, 48 insertions(+), 5 deletions(-) diff --git a/apps/web/process/calculate/action.ts b/apps/web/process/calculate/action.ts index 0cbb5a0..c833784 100644 --- a/apps/web/process/calculate/action.ts +++ b/apps/web/process/calculate/action.ts @@ -7,7 +7,9 @@ const errorMessage = 'Ошибка во время расчета графика const successMessage = 'Расчет графика завершен успешно!'; export async function action({ store, trpcClient }: ProcessContext) { - const { $calculation, $tables, $results } = store; + const { $calculation, $tables, $results, $process } = store; + + $process.add('Calculate'); $calculation.$status.setStatus('btnCalculate', 'Loading'); $calculation.$status.setStatus('btnCreateKP', 'Loading'); @@ -60,5 +62,7 @@ export async function action({ store, trpcClient }: ProcessContext) { $calculation.$status.setStatus('btnCalculate', 'Default'); $calculation.$status.setStatus('btnCreateKP', 'Default'); $calculation.$status.setStatus('btnCreateKPMini', 'Default'); + + $process.delete('Calculate'); }); } diff --git a/apps/web/process/calculate/reactions/common.ts b/apps/web/process/calculate/reactions/common.ts index 5d1e5d6..a700f91 100644 --- a/apps/web/process/calculate/reactions/common.ts +++ b/apps/web/process/calculate/reactions/common.ts @@ -1,9 +1,10 @@ import helper from '../lib/helper'; import type { ProcessContext } from '@/process/types'; -import { reaction } from 'mobx'; +import { disposableReaction } from '@/utils/mobx'; +import { comparer, reaction } from 'mobx'; export default function reactions({ store, apolloClient }: ProcessContext) { - const { $calculation } = store; + const { $calculation, $tables, $results, $process } = store; reaction( () => $calculation.element('radioCalcType').getValue(), @@ -37,4 +38,24 @@ export default function reactions({ store, apolloClient }: ProcessContext) { $calculation.element('labelIrrInfo').setValue({ max, min }); } ); + + disposableReaction( + () => $process.has('Calculate'), + () => ({ + insurance: { + fingap: $tables.insurance.row('fingap').getValues(), + kasko: $tables.insurance.row('kasko').getValues(), + osago: $tables.insurance.row('osago').getValues(), + }, + payments: $tables.payments.getValues, + values: $calculation.$values.getValues(), + }), + () => { + $results.clear(); + }, + { + delay: 10, + equals: comparer.structural, + } + ); } diff --git a/apps/web/stores/process/index.ts b/apps/web/stores/process/index.ts index 3a97210..23e4b15 100644 --- a/apps/web/stores/process/index.ts +++ b/apps/web/stores/process/index.ts @@ -1,7 +1,7 @@ import type { ObservableSet } from 'mobx'; import { observable } from 'mobx'; -export type Process = 'ELT' | 'LoadKP' | 'Unlimited'; +export type Process = 'Calculate' | 'ELT' | 'LoadKP' | 'Unlimited'; export type ProcessStore = ObservableSet; export default function createProcessStore() { diff --git a/apps/web/stores/results/index.ts b/apps/web/stores/results/index.ts index c553ae8..4f8c920 100644 --- a/apps/web/stores/results/index.ts +++ b/apps/web/stores/results/index.ts @@ -3,6 +3,7 @@ import type { ResultPayment, ResultValues } from './types'; import type RootStore from '@/stores/root'; import type { IObservableArray } from 'mobx'; import { makeAutoObservable, observable } from 'mobx'; +import { notification } from 'ui/elements'; export default class Results { private root: RootStore; @@ -17,6 +18,10 @@ export default class Results { this.root = rootStore; } + private get clean() { + return !this.payments.length; + } + public setPayments = (payments: ResultPayment[]) => { this.payments.replace(payments); }; @@ -26,7 +31,16 @@ export default class Results { }; public clear = () => { + if (this.clean === true) return; + this.payments.clear(); this.values = defaultResultsValues; + + notification.open({ + description: 'Результаты расчета были сброшены', + key: 'ACTION_CALCULATE', + message: 'Внимание', + type: 'warning', + }); }; } diff --git a/apps/web/stores/tables/payments/index.ts b/apps/web/stores/tables/payments/index.ts index 19a58e0..e11a103 100644 --- a/apps/web/stores/tables/payments/index.ts +++ b/apps/web/stores/tables/payments/index.ts @@ -4,7 +4,7 @@ import type { Row } from './types'; import type { Status } from '@/stores/calculation/statuses/types'; import type RootStore from '@/stores/root'; import type { IObservableArray, IObservableValue } from 'mobx'; -import { makeAutoObservable, observable, reaction } from 'mobx'; +import { makeAutoObservable, observable, reaction, toJS } from 'mobx'; export default class PaymentsTable { private root: RootStore; @@ -51,6 +51,10 @@ export default class PaymentsTable { return this.values[index]; } + public get getValues() { + return toJS(this.values); + } + public setValue = (index: number, value: number) => { this.values[index] = value; };