283 lines
6.9 KiB
TypeScript
283 lines
6.9 KiB
TypeScript
/* eslint-disable @typescript-eslint/naming-convention */
|
|
import * as CRMTypes from 'graphql/crm.types';
|
|
import { reaction } from 'mobx';
|
|
import type { ReactionsContext } from 'process/types';
|
|
import { intersects } from 'radash';
|
|
import { normalizeOptions } from 'tools';
|
|
|
|
export default function commonReactions({ store, apolloClient }: ReactionsContext) {
|
|
const { $calculation } = store;
|
|
|
|
reaction(
|
|
() => {
|
|
const { brand, subsidy, importProgram, leaseObjectType } = $calculation.$values.values;
|
|
|
|
return {
|
|
brandId: brand,
|
|
subsidyId: subsidy,
|
|
importProgramId: importProgram,
|
|
leaseObjectTypeId: leaseObjectType,
|
|
};
|
|
},
|
|
async ({ brandId, subsidyId, importProgramId, leaseObjectTypeId }) => {
|
|
if (!brandId) {
|
|
$calculation.element('selectModel').reset();
|
|
|
|
return;
|
|
}
|
|
|
|
const {
|
|
data: { evo_models },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetModelsDocument,
|
|
variables: {
|
|
brandId,
|
|
},
|
|
});
|
|
|
|
let models = evo_models;
|
|
|
|
if (!models) {
|
|
$calculation.element('selectModel').reset();
|
|
|
|
return;
|
|
}
|
|
|
|
if (leaseObjectTypeId) {
|
|
const {
|
|
data: { leaseObjectType },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetLeaseObjectTypeDocument,
|
|
variables: {
|
|
leaseObjectTypeId,
|
|
},
|
|
});
|
|
|
|
models = models?.filter((model) => {
|
|
if (
|
|
model?.evo_vehicle_type &&
|
|
leaseObjectType?.evo_vehicle_type?.includes(model.evo_vehicle_type)
|
|
) {
|
|
return model;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
}
|
|
|
|
if (subsidyId) {
|
|
const {
|
|
data: { evo_subsidy: subsidy },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetSubsidyDocument,
|
|
variables: {
|
|
subsidyId,
|
|
},
|
|
});
|
|
|
|
models = models?.filter((model) => {
|
|
if (
|
|
model &&
|
|
(!subsidy?.evo_models?.length ||
|
|
subsidy?.evo_models?.filter(
|
|
(subsidy_evo_model) => subsidy_evo_model?.evo_modelid === model.evo_modelid
|
|
)?.length)
|
|
) {
|
|
return model;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
}
|
|
|
|
if (importProgramId) {
|
|
const {
|
|
data: { importProgram },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetImportProgramDocument,
|
|
variables: {
|
|
importProgramId,
|
|
},
|
|
});
|
|
|
|
models = models?.filter((model) => {
|
|
if (
|
|
model &&
|
|
(!importProgram?.evo_models?.length ||
|
|
importProgram?.evo_models?.filter(
|
|
(importProgram_evo_model) =>
|
|
importProgram_evo_model?.evo_modelid === model.evo_modelid
|
|
)?.length)
|
|
) {
|
|
return model;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
}
|
|
|
|
$calculation.element('selectModel').setOptions(normalizeOptions(models));
|
|
}
|
|
);
|
|
|
|
reaction(
|
|
() => {
|
|
const { product, subsidy, importProgram, leaseObjectType } = $calculation.$values.values;
|
|
|
|
return {
|
|
productId: product,
|
|
subsidyId: subsidy,
|
|
importProgramId: importProgram,
|
|
leaseObjectTypeId: leaseObjectType,
|
|
};
|
|
},
|
|
async ({ productId, subsidyId, importProgramId, leaseObjectTypeId }) => {
|
|
const {
|
|
data: { evo_brands },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetBrandsDocument,
|
|
});
|
|
|
|
let brands = evo_brands;
|
|
|
|
if (!brands) {
|
|
$calculation.element('selectBrand').reset();
|
|
|
|
return;
|
|
}
|
|
|
|
if (productId) {
|
|
const {
|
|
data: { evo_baseproduct },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetProductDocument,
|
|
variables: {
|
|
productId,
|
|
},
|
|
});
|
|
|
|
brands = brands?.filter((brand) => {
|
|
if (
|
|
!evo_baseproduct?.evo_brands?.length ||
|
|
evo_baseproduct.evo_brands.filter(
|
|
(baseproduct_evo_brand) => baseproduct_evo_brand?.evo_brandid === brand?.evo_brandid
|
|
)?.length
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
}
|
|
|
|
if (leaseObjectTypeId) {
|
|
const {
|
|
data: { leaseObjectType },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetLeaseObjectTypeDocument,
|
|
variables: {
|
|
leaseObjectTypeId,
|
|
},
|
|
});
|
|
|
|
brands = brands?.filter((brand) => {
|
|
if (
|
|
brand?.evo_vehicle_type?.length &&
|
|
leaseObjectType?.evo_vehicle_type &&
|
|
intersects(
|
|
brand.evo_vehicle_type?.filter((x) => x > 0),
|
|
leaseObjectType?.evo_vehicle_type
|
|
)
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
}
|
|
|
|
if (subsidyId) {
|
|
const {
|
|
data: { evo_subsidy: subsidy },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetSubsidyDocument,
|
|
variables: {
|
|
subsidyId,
|
|
},
|
|
});
|
|
|
|
brands = brands?.filter((brand) => {
|
|
if (
|
|
!subsidy?.evo_brands?.length ||
|
|
subsidy?.evo_brands?.filter(
|
|
(subsidy_evo_brand) => subsidy_evo_brand?.evo_brandid === brand?.evo_brandid
|
|
)?.length
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
}
|
|
|
|
if (importProgramId) {
|
|
const {
|
|
data: { importProgram },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetImportProgramDocument,
|
|
variables: {
|
|
importProgramId,
|
|
},
|
|
});
|
|
|
|
brands = brands?.filter((brand) => {
|
|
if (
|
|
!importProgram?.evo_brands?.length ||
|
|
importProgram?.evo_brands?.filter(
|
|
(importProgram_evo_brand) =>
|
|
importProgram_evo_brand?.evo_brandid === brand?.evo_brandid
|
|
)?.length
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
}
|
|
|
|
$calculation.element('selectBrand').setOptions(normalizeOptions(brands));
|
|
}
|
|
);
|
|
|
|
reaction(
|
|
() => $calculation.element('selectLeaseObjectType').getValue(),
|
|
(leaseObjectTypeId) => {
|
|
if (!leaseObjectTypeId) {
|
|
$calculation.element('selectBrand').resetValue();
|
|
}
|
|
}
|
|
);
|
|
|
|
reaction(
|
|
() => $calculation.element('selectModel').getValue(),
|
|
async (modelId) => {
|
|
if (!modelId) {
|
|
$calculation.element('selectConfiguration').reset();
|
|
|
|
return;
|
|
}
|
|
|
|
const {
|
|
data: { evo_equipments },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetConfigurationsDocument,
|
|
variables: {
|
|
modelId,
|
|
},
|
|
});
|
|
|
|
$calculation.element('selectConfiguration').setOptions(normalizeOptions(evo_equipments));
|
|
}
|
|
);
|
|
}
|