process/agents: make reactions disposable

This commit is contained in:
Chika 2022-10-25 17:20:28 +03:00
parent 202eb6e304
commit 5b923f9de7

View File

@ -1,3 +1,4 @@
/* eslint-disable implicit-arrow-linebreak */
/* eslint-disable @typescript-eslint/naming-convention */
import type { ApolloClient } from '@apollo/client';
import { gql } from '@apollo/client';
@ -7,25 +8,30 @@ import type * as CRMTypes from 'graphql/crm.types';
import { reaction } from 'mobx';
import type RootStore from 'stores/root';
import { normalizeOptions } from 'tools/entity';
import { makeDisposable } from 'tools/mobx';
import * as fillAgentsFromLead from '../lib/fill-agents-from-lead';
import { QUERY_GET_AGENT } from '../lib/query';
dayjs.extend(utc);
export default function commonReactions(store: RootStore, apolloClient: ApolloClient<object>) {
const { $calculation } = store;
const { $calculation, $process } = store;
/**
* Заполняем агентов из Интереса
*/
reaction(
() => $calculation.getElementValue('selectLead'),
(leadid) => {
fillAgentsFromLead.fillIndAgent(store, apolloClient, leadid);
fillAgentsFromLead.fillCalcDoubleAgent(store, apolloClient, leadid);
fillAgentsFromLead.fillCalcBroker(store, apolloClient, leadid);
fillAgentsFromLead.fillFinDepartment(store, apolloClient, leadid);
}
makeDisposable(
() =>
reaction(
() => $calculation.getElementValue('selectLead'),
(leadid) => {
fillAgentsFromLead.fillIndAgent(store, apolloClient, leadid);
fillAgentsFromLead.fillCalcDoubleAgent(store, apolloClient, leadid);
fillAgentsFromLead.fillCalcBroker(store, apolloClient, leadid);
fillAgentsFromLead.fillFinDepartment(store, apolloClient, leadid);
}
),
() => $process.has('LoadKP')
);
/**
@ -44,33 +50,37 @@ export default function commonReactions(store: RootStore, apolloClient: ApolloCl
}
`;
reaction(
() => $calculation.getElementValue('selectDealer'),
async (dealerId) => {
if (!dealerId) {
$calculation.resetElement('selectDealerPerson');
$calculation.resetElement('selectDealerBroker');
makeDisposable(
() =>
reaction(
() => $calculation.getElementValue('selectDealer'),
async (dealerId) => {
if (!dealerId) {
$calculation.resetElement('selectDealerPerson');
$calculation.resetElement('selectDealerBroker');
return;
}
return;
}
const {
data: { salon_providers },
} = await apolloClient.query<
CRMTypes.GetDealerPersonQuery,
CRMTypes.GetDealerPersonQueryVariables
>({
query: QUERY_GET_DEALER_PERSON,
variables: {
dealerId,
},
});
const {
data: { salon_providers },
} = await apolloClient.query<
CRMTypes.GetDealerPersonQuery,
CRMTypes.GetDealerPersonQueryVariables
>({
query: QUERY_GET_DEALER_PERSON,
variables: {
dealerId,
},
});
if (salon_providers?.length) {
$calculation.setElementOptions('selectDealerPerson', normalizeOptions(salon_providers));
$calculation.setElementValue('selectDealerPerson', salon_providers[0]?.value);
}
}
if (salon_providers?.length) {
$calculation.setElementOptions('selectDealerPerson', normalizeOptions(salon_providers));
$calculation.setElementValue('selectDealerPerson', salon_providers[0]?.value);
}
}
),
() => $process.has('LoadKP')
);
/**
@ -85,43 +95,50 @@ export default function commonReactions(store: RootStore, apolloClient: ApolloCl
}
`;
reaction(
() => $calculation.getElementValue('selectDealerPerson'),
async (dealerPersonId) => {
if (!dealerPersonId) {
return;
}
makeDisposable(
() =>
reaction(
() => $calculation.getElementValue('selectDealerPerson'),
async (dealerPersonId) => {
if (!dealerPersonId) {
return;
}
const {
data: { dealer },
} = await apolloClient.query<
CRMTypes.GetBrokerAccountIdFromDealerQuery,
CRMTypes.GetBrokerAccountIdFromDealerQueryVariables
>({
query: QUERY_GET_BROKER_ACCOUNTID_FROM_DEALER,
variables: {
dealerId: dealerPersonId,
},
});
const {
data: { dealer },
} = await apolloClient.query<
CRMTypes.GetBrokerAccountIdFromDealerQuery,
CRMTypes.GetBrokerAccountIdFromDealerQueryVariables
>({
query: QUERY_GET_BROKER_ACCOUNTID_FROM_DEALER,
variables: {
dealerId: dealerPersonId,
},
});
if (dealer?.evo_broker_accountid) {
const {
data: { agent: dealerBroker },
} = await apolloClient.query<CRMTypes.GetAgentQuery, CRMTypes.GetAgentQueryVariables>({
query: QUERY_GET_AGENT,
variables: {
agentid: dealer?.evo_broker_accountid,
},
});
if (dealer?.evo_broker_accountid) {
const {
data: { agent: dealerBroker },
} = await apolloClient.query<CRMTypes.GetAgentQuery, CRMTypes.GetAgentQueryVariables>({
query: QUERY_GET_AGENT,
variables: {
agentid: dealer?.evo_broker_accountid,
},
});
if (dealerBroker) {
$calculation.setElementOptions('selectDealerBroker', normalizeOptions([dealerBroker]));
$calculation.setElementValue('selectDealerBroker', dealerBroker.value);
if (dealerBroker) {
$calculation.setElementOptions(
'selectDealerBroker',
normalizeOptions([dealerBroker])
);
$calculation.setElementValue('selectDealerBroker', dealerBroker.value);
}
} else {
$calculation.resetElement('selectDealerBroker');
}
}
} else {
$calculation.resetElement('selectDealerBroker');
}
}
),
() => $process.has('LoadKP')
);
/**
@ -144,34 +161,38 @@ export default function commonReactions(store: RootStore, apolloClient: ApolloCl
}
`;
reaction(
() => $calculation.getElementValue('selectDealerPerson'),
async (dealerPersonId) => {
if (!dealerPersonId) {
$calculation.resetElement('selectDealerRewardCondition');
makeDisposable(
() =>
reaction(
() => $calculation.getElementValue('selectDealerPerson'),
async (dealerPersonId) => {
if (!dealerPersonId) {
$calculation.resetElement('selectDealerRewardCondition');
return;
}
const {
data: { evo_reward_conditions },
} = await apolloClient.query<
CRMTypes.GetRewardConditionsQuery,
CRMTypes.GetRewardConditionsQueryVariables
>({
query: QUERY_GET_REWARD_CONDITIONS,
variables: {
agentid: dealerPersonId,
currentDate: dayjs().toISOString(),
},
});
return;
}
const {
data: { evo_reward_conditions },
} = await apolloClient.query<
CRMTypes.GetRewardConditionsQuery,
CRMTypes.GetRewardConditionsQueryVariables
>({
query: QUERY_GET_REWARD_CONDITIONS,
variables: {
agentid: dealerPersonId,
currentDate: dayjs().toISOString(),
},
});
if (evo_reward_conditions?.length) {
$calculation.setElementOptions(
'selectDealerRewardCondition',
normalizeOptions(evo_reward_conditions)
);
}
}
if (evo_reward_conditions?.length) {
$calculation.setElementOptions(
'selectDealerRewardCondition',
normalizeOptions(evo_reward_conditions)
);
}
}
),
() => $process.has('LoadKP')
);
const QUERY_GET_REWARD_SUMM = gql`
@ -182,94 +203,109 @@ export default function commonReactions(store: RootStore, apolloClient: ApolloCl
}
`;
reaction(
() => $calculation.getElementValue('selectDealerRewardCondition'),
async (rewardConditionId) => {
if (!rewardConditionId) {
$calculation.resetElement('tbxDealerRewardSumm');
makeDisposable(
() =>
reaction(
() => $calculation.getElementValue('selectDealerRewardCondition'),
async (rewardConditionId) => {
if (!rewardConditionId) {
$calculation.resetElement('tbxDealerRewardSumm');
return;
}
return;
}
const {
data: { evo_reward_condition },
} = await apolloClient.query<
CRMTypes.GetRewardSummQuery,
CRMTypes.GetRewardSummQueryVariables
>({
query: QUERY_GET_REWARD_SUMM,
variables: {
conditionId: rewardConditionId,
},
});
const {
data: { evo_reward_condition },
} = await apolloClient.query<
CRMTypes.GetRewardSummQuery,
CRMTypes.GetRewardSummQueryVariables
>({
query: QUERY_GET_REWARD_SUMM,
variables: {
conditionId: rewardConditionId,
},
});
$calculation.setElementValue('tbxDealerRewardSumm', evo_reward_condition?.evo_reward_summ);
$calculation.unblockElement('tbxDealerRewardSumm');
}
$calculation.setElementValue(
'tbxDealerRewardSumm',
evo_reward_condition?.evo_reward_summ
);
$calculation.unblockElement('tbxDealerRewardSumm');
}
),
() => $process.has('LoadKP')
);
/**
* Заполняем selectDealerBrokerRewardCondition
*/
reaction(
() => $calculation.getElementValue('selectDealerBroker'),
async (dealerBrokerId) => {
if (!dealerBrokerId) {
$calculation.resetElement('selectDealerBrokerRewardCondition');
makeDisposable(
() =>
reaction(
() => $calculation.getElementValue('selectDealerBroker'),
async (dealerBrokerId) => {
if (!dealerBrokerId) {
$calculation.resetElement('selectDealerBrokerRewardCondition');
return;
}
const {
data: { evo_reward_conditions },
} = await apolloClient.query<
CRMTypes.GetRewardConditionsQuery,
CRMTypes.GetRewardConditionsQueryVariables
>({
query: QUERY_GET_REWARD_CONDITIONS,
variables: {
agentid: dealerBrokerId,
currentDate: dayjs().toISOString(),
},
});
return;
}
const {
data: { evo_reward_conditions },
} = await apolloClient.query<
CRMTypes.GetRewardConditionsQuery,
CRMTypes.GetRewardConditionsQueryVariables
>({
query: QUERY_GET_REWARD_CONDITIONS,
variables: {
agentid: dealerBrokerId,
currentDate: dayjs().toISOString(),
},
});
if (evo_reward_conditions?.length) {
$calculation.setElementOptions(
'selectDealerBrokerRewardCondition',
normalizeOptions(evo_reward_conditions)
);
}
}
if (evo_reward_conditions?.length) {
$calculation.setElementOptions(
'selectDealerBrokerRewardCondition',
normalizeOptions(evo_reward_conditions)
);
}
}
),
() => $process.has('LoadKP')
);
reaction(
() => $calculation.getElementValue('selectDealerBrokerRewardCondition'),
async (rewardConditionId) => {
if (!rewardConditionId) {
$calculation.resetElement('tbxDealerBrokerRewardSumm');
makeDisposable(
() =>
reaction(
() => $calculation.getElementValue('selectDealerBrokerRewardCondition'),
async (rewardConditionId) => {
if (!rewardConditionId) {
$calculation.resetElement('tbxDealerBrokerRewardSumm');
return;
}
return;
}
const {
data: { evo_reward_condition },
} = await apolloClient.query<
CRMTypes.GetRewardSummQuery,
CRMTypes.GetRewardSummQueryVariables
>({
query: QUERY_GET_REWARD_SUMM,
variables: {
conditionId: rewardConditionId,
},
});
const {
data: { evo_reward_condition },
} = await apolloClient.query<
CRMTypes.GetRewardSummQuery,
CRMTypes.GetRewardSummQueryVariables
>({
query: QUERY_GET_REWARD_SUMM,
variables: {
conditionId: rewardConditionId,
},
});
$calculation.setElementValue(
'tbxDealerBrokerRewardSumm',
evo_reward_condition?.evo_reward_summ
);
$calculation.setElementValue(
'tbxDealerBrokerRewardSumm',
evo_reward_condition?.evo_reward_summ
);
$calculation.unblockElement('tbxDealerBrokerRewardSumm');
}
$calculation.unblockElement('tbxDealerBrokerRewardSumm');
}
),
() => $process.has('LoadKP')
);
reaction(