From deb01652350a71e3eade232966f2f7a7b421305f Mon Sep 17 00:00:00 2001 From: Chika Date: Thu, 7 Jul 2022 19:22:27 +0300 Subject: [PATCH] process/lead-opportuniy: add link reactions for opportunity and quote --- .../reactions/__generated__/GetLeadUrl.ts | 24 ----- process/lead-opportunity/reactions/urls.ts | 90 ++++++++++++------- 2 files changed, 56 insertions(+), 58 deletions(-) delete mode 100644 process/lead-opportunity/reactions/__generated__/GetLeadUrl.ts diff --git a/process/lead-opportunity/reactions/__generated__/GetLeadUrl.ts b/process/lead-opportunity/reactions/__generated__/GetLeadUrl.ts deleted file mode 100644 index 51184fc..0000000 --- a/process/lead-opportunity/reactions/__generated__/GetLeadUrl.ts +++ /dev/null @@ -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; -} diff --git a/process/lead-opportunity/reactions/urls.ts b/process/lead-opportunity/reactions/urls.ts index b025d1f..9e135b2 100644 --- a/process/lead-opportunity/reactions/urls.ts +++ b/process/lead-opportunity/reactions/urls.ts @@ -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({ - 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); }