2022-07-13 14:44:30 +03:00

85 lines
2.3 KiB
TypeScript

import type { ApolloClient, DocumentNode } from '@apollo/client';
import { gql } from '@apollo/client';
import type { Elements } from 'Components/Calculation/config/map/values';
import { reaction } from 'mobx';
import type RootStore from 'stores/root';
export default function urlsReactions(store: RootStore, apolloClient: ApolloClient<object>) {
const { $calculation } = store;
/**
* При выборе Интереса, ЛС и Предложения скачиваем ссылку CRM
*/
function makeLinkReaction(elementName: Elements, linkElementName: Elements, query: DocumentNode) {
reaction(
() => $calculation.getElementValue(elementName),
(id) => {
if (!id) {
$calculation.resetElementValue(linkElementName);
return;
}
const timeoutId = setTimeout(() => {
$calculation.$status.setStatus(linkElementName, 'Loading');
}, 1200);
apolloClient
.query({
query,
variables: {
id,
},
})
.then(({ data }) => {
clearTimeout(timeoutId);
if (data.entity?.link) {
$calculation.setElementValue(linkElementName, data.entity?.link);
}
})
.catch(() => {
clearTimeout(timeoutId);
$calculation.resetElementValue(linkElementName);
});
}
);
reaction(
() => $calculation.getElementValue(linkElementName),
(url) => {
if (!url) {
$calculation.blockElement(linkElementName);
} else {
$calculation.unblockElement(linkElementName);
}
}
);
}
const QUERY_GET_LEAD_URL = gql`
query GetLeadUrl($id: Uuid!) {
entity: lead(leadid: $id) {
link
}
}
`;
makeLinkReaction('selectLead', 'linkLeadUrl', QUERY_GET_LEAD_URL);
const QUERY_GET_OPPORTUNITY_URL = gql`
query GetOpportunityUrl($id: Uuid!) {
entity: opportunity(opportunityid: $id) {
link
}
}
`;
makeLinkReaction('selectOpportunity', 'linkOpportunityUrl', QUERY_GET_OPPORTUNITY_URL);
const QUERY_GET_QUOTE_URL = gql`
query GetQuoteUrl($id: Uuid!) {
entity: quote(quoteId: $id) {
link
}
}
`;
makeLinkReaction('selectQuote', 'linkQuoteUrl', QUERY_GET_QUOTE_URL);
}