52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
import type { ApolloClient } from '@apollo/client';
|
|
import type { QueryClient } from '@tanstack/react-query';
|
|
import message from 'Elements/message';
|
|
import { reaction, toJS } from 'mobx';
|
|
import type RootStore from 'stores/root';
|
|
import type { TRPCPureClient } from 'trpc/types';
|
|
|
|
export default function loadKpReactions(
|
|
store: RootStore,
|
|
apolloClient: ApolloClient<object>,
|
|
queryClient: QueryClient,
|
|
trpcClient: TRPCPureClient
|
|
) {
|
|
const { $calculation, $process } = store;
|
|
|
|
reaction(
|
|
() => $calculation.element('selectQuote').getValue(),
|
|
(quoteId) => {
|
|
if (!quoteId) return;
|
|
|
|
$process.add('LoadKP');
|
|
|
|
const quoteName = $calculation.element('selectQuote').getOption()?.label;
|
|
const { quote } = toJS($calculation.$values.values);
|
|
|
|
trpcClient.quote.getValues
|
|
.query({
|
|
values: {
|
|
quote,
|
|
},
|
|
})
|
|
.then(({ values }) => {
|
|
$calculation.$values.setValues({
|
|
values,
|
|
exclude: ['lead', 'opportunity', 'quote', 'leadUrl', 'opportunityUrl', 'quoteUrl'],
|
|
});
|
|
message.success({
|
|
content: `КП ${quoteName} загружено`,
|
|
});
|
|
})
|
|
.catch(() => {
|
|
message.error({
|
|
content: `Ошибка во время загрузки КП ${quoteName}`,
|
|
});
|
|
})
|
|
.finally(() => {
|
|
$process.delete('LoadKP');
|
|
});
|
|
}
|
|
);
|
|
}
|