process/lead-opportuniy: add link reactions for opportunity and quote

This commit is contained in:
Chika 2022-07-07 19:22:27 +03:00
parent 0382e8429d
commit deb0165235
2 changed files with 56 additions and 58 deletions

View File

@ -1,24 +0,0 @@
/* tslint:disable */
/* eslint-disable */
// @generated
// This file was automatically generated and should not be edited.
// ====================================================
// GraphQL query operation: GetLeadUrl
// ====================================================
export interface GetLeadUrl_lead {
__typename: 'lead';
link: string | null;
}
export interface GetLeadUrl {
/**
* Лизинговые сделки. statecode по умолчанию 0, можно отфильтровать по ownerid и/или domainName
*/
lead: GetLeadUrl_lead | null;
}
export interface GetLeadUrlVariables {
leadid: any;
}

View File

@ -1,8 +1,8 @@
import type { ApolloClient } from '@apollo/client';
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';
import type { GetLeadUrl, GetLeadUrlVariables } from './__generated__/GetLeadUrl';
export default function leadOpportunityUrlsReactions(
store: RootStore,
@ -11,46 +11,68 @@ export default function leadOpportunityUrlsReactions(
const { $calculation } = store;
/**
* При выборе Интереса скачиваем ссылку в CRM на него и записываем в linkLeadUrl
* При выборе Интереса, ЛС и Предложения скачиваем ссылку CRM
*/
function makeLinkReaction(elementName: Elements, linkElementName: Elements, query: DocumentNode) {
reaction(
() => $calculation.$values.getElementValue(elementName),
(id) => {
const timeoutId = setTimeout(() => {
$calculation.$status.setStatus(linkElementName, 'Loading');
}, 1200);
apolloClient
.query({
query,
variables: {
id,
},
})
.then(({ data }) => {
clearTimeout(timeoutId);
if (data.entity?.link) {
$calculation.$values.setElementValue(linkElementName, data.entity?.link);
}
})
.catch(() => {
clearTimeout(timeoutId);
$calculation.$values.setElementValue(linkElementName, null);
});
}
);
reaction(
() => $calculation.$values.getElementValue(linkElementName),
(url) => {
const status = url ? 'Default' : 'Disabled';
$calculation.$status.setStatus(linkElementName, status);
}
);
}
const QUERY_GET_LEAD_URL = gql`
query GetLeadUrl($leadid: Uuid!) {
lead(leadid: $leadid) {
query GetLeadUrl($id: Uuid!) {
entity: lead(leadid: $id) {
link
}
}
`;
makeLinkReaction('selectLead', 'linkLeadUrl', QUERY_GET_LEAD_URL);
reaction(
() => $calculation.$values.getElementValue('selectLead'),
(leadid) => {
const timeoutId = setTimeout(() => {
$calculation.$status.setStatus('linkLeadUrl', 'Loading');
}, 1200);
const QUERY_GET_OPPORTUNITY_URL = gql`
query GetOpportunityUrl($id: Uuid!) {
entity: opportunity(opportunityid: $id) {
link
}
}
`;
makeLinkReaction('selectOpportunity', 'linkOpportunityUrl', QUERY_GET_OPPORTUNITY_URL);
apolloClient
.query<GetLeadUrl, GetLeadUrlVariables>({
query: QUERY_GET_LEAD_URL,
variables: {
leadid,
},
})
.then(({ data }) => {
clearTimeout(timeoutId);
if (data.lead?.link) $calculation.$values.setValue('leadUrl', data.lead?.link);
})
.catch(() => {
clearTimeout(timeoutId);
$calculation.$values.setValue('leadUrl', null);
});
const QUERY_GET_QUOTE_URL = gql`
query GetQuoteUrl($id: Uuid!) {
entity: quote(quoteId: $id) {
link
}
}
);
reaction(
() => $calculation.$values.getElementValue('linkLeadUrl'),
(leadUrl) => {
const status = leadUrl ? 'Default' : 'Disabled';
$calculation.$status.setStatus('linkLeadUrl', status);
}
);
`;
makeLinkReaction('selectQuote', 'linkQuoteUrl', QUERY_GET_QUOTE_URL);
}