types: BaseOption value type

This commit is contained in:
Chika 2022-07-09 12:56:31 +03:00
parent 24947cc40b
commit d01e725875
7 changed files with 58 additions and 141 deletions

View File

@ -14,7 +14,7 @@ export type RowValues = {
export type Values = Exclude<keyof RowValues, 'key'>; export type Values = Exclude<keyof RowValues, 'key'>;
export type RowOptions = { export type RowOptions = {
[ValueName in Values]?: BaseOption[]; [ValueName in Values]?: BaseOption<RowValues[ValueName]>[];
} & { key: Keys }; } & { key: Keys };
export type RowStatuses = Record<Values, Status> & { export type RowStatuses = Record<Values, Status> & {

View File

@ -151,7 +151,7 @@ export type ElementsTypes = {
[Key in keyof ElementsValues]: CalculationValues[ElementsValues[Key]]; [Key in keyof ElementsValues]: CalculationValues[ElementsValues[Key]];
}; };
export type Elements = keyof ElementsValues; export type Elements = keyof ElementsTypes;
export function getValueName(elementName: Elements) { export function getValueName(elementName: Elements) {
return elementsToValues[elementName]; return elementsToValues[elementName];

View File

@ -1,14 +1,14 @@
export type Status = 'Default' | 'Disabled' | 'Loading' | 'Hidden'; export type Status = 'Default' | 'Disabled' | 'Loading' | 'Hidden';
export type BaseElementProps<ValueType> = { export type BaseElementProps<Value> = {
value: ValueType; value: Value;
setValue: (value: ValueType) => void; setValue: (value: Value) => void;
status?: Status; status?: Status;
isValid?: boolean; isValid?: boolean;
help?: string; help?: string;
}; };
export type BaseOption = { export type BaseOption<Value = any> = {
label: string; label: string;
value: any; value: Value;
}; };

View File

@ -366,29 +366,25 @@ const defaultOptions: CalculationOptions = {
selectObjectTypeTax: [ selectObjectTypeTax: [
{ {
label: 'Автобус', label: 'Автобус',
type: 'D', value: 100_000_000,
}, },
{ {
label: 'Легковой', label: 'Легковой',
type: 'B', value: 100_000_001,
}, },
{ {
label: 'Грузовой', label: 'Грузовой',
type: 'C', value: 100_000_002,
}, },
{ {
label: 'Спецтехника', label: 'Спецтехника',
type: 'T', value: 100_000_003,
}, },
{ {
label: 'Мотоцикл', label: 'Мотоцикл',
type: 'A', value: 100_000_004,
}, },
].map((v, i) => ({ ],
...v,
value: 100_000_000 + i,
})),
selectLead: [], selectLead: [],
selectOpportunity: [], selectOpportunity: [],
selectQuote: [], selectQuote: [],

View File

@ -1,6 +1,6 @@
/* eslint-disable object-curly-newline */ /* eslint-disable object-curly-newline */
/* eslint-disable unicorn/prefer-set-has */ /* eslint-disable unicorn/prefer-set-has */
import type { Elements } from 'Components/Calculation/config/map/values'; import type { Elements, ElementsTypes } from 'Components/Calculation/config/map/values';
import defaultOptions from 'config/default-options'; import defaultOptions from 'config/default-options';
import type { BaseOption } from 'Elements/types'; import type { BaseOption } from 'Elements/types';
import { makeAutoObservable } from 'mobx'; import { makeAutoObservable } from 'mobx';
@ -34,7 +34,7 @@ export default class OptionsStore {
this.options = initialOptions; this.options = initialOptions;
}; };
getOption(elementName: Elements) { getOption<T extends Elements>(elementName: T) {
const value = this.root.$calculation.$values.getElementValue(elementName); const value = this.root.$calculation.$values.getElementValue(elementName);
return this.options[elementName]?.find((x) => x.value === value); return this.options[elementName]?.find((x) => x.value === value);
@ -46,8 +46,16 @@ export default class OptionsStore {
return options; return options;
} }
setElementOptions = (elementName: Elements, options: BaseOption[]) => { setElementOptions = <T extends Elements>(
this.options[elementName] = options; elementName: T,
options: BaseOption<ElementsTypes[T]>[]
) => {
/**
* TODO: use T istead of any in BaseOption type
* at this moment T causes typescript error
* but infer works just perfect
*/
this.options[elementName] = options as BaseOption<any>[];
/** /**
* Проверяем, что значение есть в новом списке, иначе сбрасываем значение * Проверяем, что значение есть в новом списке, иначе сбрасываем значение
@ -62,11 +70,15 @@ export default class OptionsStore {
* (для элементов из списка {@link AUTO_SET_VALUE_ELEMENTS}) * (для элементов из списка {@link AUTO_SET_VALUE_ELEMENTS})
*/ */
if (options?.length === 1 && AUTO_SET_VALUE_ELEMENTS.includes(elementName)) { if (options?.length === 1 && AUTO_SET_VALUE_ELEMENTS.includes(elementName)) {
this.root.$calculation.$values.setElementValue(elementName, options.at(0)?.value); const optionValue = options.at(0)?.value;
if (optionValue) {
this.root.$calculation.$values.setElementValue(elementName, optionValue);
}
} }
}; };
resetOption = (elementName: Elements) => { resetOption = <T extends Elements>(elementName: T) => {
this.options[elementName] = defaultOptions[elementName]; this.options[elementName] = defaultOptions[elementName];
}; };
@ -83,7 +95,11 @@ export default class OptionsStore {
} }
(Object.keys(options) as Elements[]).forEach((elementName) => { (Object.keys(options) as Elements[]).forEach((elementName) => {
this.setElementOptions(elementName, options[elementName]!); const elementOptions = options[elementName];
if (elementOptions) {
this.setElementOptions(elementName, elementOptions);
}
}); });
}; };
} }

View File

@ -1,4 +1,6 @@
import type { Elements } from 'Components/Calculation/config/map/values'; import type { Elements, ElementsTypes } from 'Components/Calculation/config/map/values';
import type { BaseOption } from 'Elements/types'; import type { BaseOption } from 'Elements/types';
export type CalculationOptions = Record<Elements, BaseOption[]>; export type CalculationOptions = {
[ElementName in Elements]: BaseOption<ElementsTypes[ElementName]>[];
};

View File

@ -15,60 +15,22 @@ export type CalculationValues = {
firstPaymentRub: number; firstPaymentRub: number;
lastPaymentPerc: number; lastPaymentPerc: number;
lastPaymentRub: number; lastPaymentRub: number;
lastPaymentRule: 100_000_000 | 100_000_001 | 100_000_002 | null; lastPaymentRule: number | null;
importProgram: string | null; importProgram: string | null;
importProgramSum: number; importProgramSum: number;
addEquipmentPrice: number; addEquipmentPrice: number;
redemptionPaymentSum: number; redemptionPaymentSum: number;
balanceHolder: 100_000_000 | 100_000_001 | null; balanceHolder: number | null;
graphType: 100_000_000 | 100_000_001 | 100_000_002 | 100_000_003 | 100_000_004 | null; graphType: number | null;
parmentsDecreasePercent: number; parmentsDecreasePercent: number;
seasonType: seasonType: number | null;
| 100_000_000 highSeasonStart: number | null;
| 100_000_001
| 100_000_002
| 100_000_003
| 100_000_004
| 100_000_005
| 100_000_006
| 100_000_007
| null;
highSeasonStart:
| 100_000_000
| 100_000_001
| 100_000_002
| 100_000_003
| 100_000_004
| 100_000_005
| 100_000_006
| 100_000_007
| 100_000_008
| 100_000_009
| 100_000_010
| 100_000_011
| 100_000_012
| null;
comissionPerc: number; comissionPerc: number;
comissionRub: number; comissionRub: number;
saleBonus: number; saleBonus: number;
IRR_Perc: number; IRR_Perc: number;
leaseObjectType: string | null; leaseObjectType: string | null;
deliveryTime: deliveryTime: number | null;
| 100_000_000
| 100_000_001
| 100_000_002
| 100_000_003
| 100_000_004
| 100_000_005
| 100_000_006
| 100_000_007
| 100_000_008
| 100_000_009
| 100_000_010
| 100_000_011
| 100_000_012
| null;
leaseObjectCount: number; leaseObjectCount: number;
withTrailer: boolean; withTrailer: boolean;
leaseObjectUsed: boolean; leaseObjectUsed: boolean;
@ -79,32 +41,11 @@ export type CalculationValues = {
model: string | null; model: string | null;
configuration: string | null; configuration: string | null;
leaseObjectYear: number; leaseObjectYear: number;
engineType: 100_000_000 | 100_000_001 | 100_000_002 | 100_000_003 | 100_000_004 | null; engineType: number | null;
leaseObjectCategory: 100_000_000 | 100_000_001 | 100_000_002 | 100_000_003 | 100_000_004 | null; leaseObjectCategory: number | null;
leaseObjectMotorPower: number; leaseObjectMotorPower: number;
engineVolume: number; engineVolume: number;
leaseObjectUseFor: leaseObjectUseFor: number | null;
| 100_000_000
| 100_000_001
| 100_000_002
| 100_000_003
| 100_000_004
| 100_000_005
| 100_000_006
| 100_000_007
| 100_000_008
| 100_000_009
| 100_000_010
| 100_000_011
| 100_000_012
| 100_000_013
| 100_000_014
| 100_000_015
| 100_000_016
| 100_000_017
| 100_000_018
| 100_000_019
| null;
dealer: string | null; dealer: string | null;
dealerPerson: string | null; dealerPerson: string | null;
dealerRewardCondition: string | null; dealerRewardCondition: string | null;
@ -128,8 +69,8 @@ export type CalculationValues = {
GPSModel: string | null; GPSModel: string | null;
regionRegistration: string | null; regionRegistration: string | null;
townRegistration: string | null; townRegistration: string | null;
infuranceOPF: 100_000_000 | 100_000_001 | null; infuranceOPF: number | null;
insKaskoType: 100_000_000 | 100_000_001 | null; insKaskoType: number | null;
insDecentral: boolean; insDecentral: boolean;
insFranchise: number; insFranchise: number;
insUnlimitDrivers: boolean; insUnlimitDrivers: boolean;
@ -145,22 +86,13 @@ export type CalculationValues = {
technicalCardQuote: boolean; technicalCardQuote: boolean;
NSIB: boolean; NSIB: boolean;
quoteName: string | null; quoteName: string | null;
quoteContactGender: 100_000_000 | 100_000_001 | null; quoteContactGender: number | null;
quoteRedemptionGraph: boolean; quoteRedemptionGraph: boolean;
showFinGAP: boolean; showFinGAP: boolean;
tarif: string | null; tarif: string | null;
creditRate: number; creditRate: number;
rate: string | null; rate: string | null;
requirementTelematic: requirementTelematic: number | null;
| 100_000_000
| 100_000_001
| 100_000_002
| 100_000_003
| 100_000_004
| 100_000_005
| 100_000_006
| 100_000_007
| null;
minPriceChange: number; minPriceChange: number;
maxPriceChange: number; maxPriceChange: number;
importerRewardPerc: number; importerRewardPerc: number;
@ -172,44 +104,15 @@ export type CalculationValues = {
telematic: string | null; telematic: string | null;
tracker: string | null; tracker: string | null;
mileage: number; mileage: number;
calcType: 100_000_000 | 100_000_001 | null; calcType: number | null;
totalPayments: number; totalPayments: number;
objectRegistration: 100_000_000 | 100_000_001 | null; objectRegistration: number | null;
objectRegionRegistration: string | null; objectRegionRegistration: string | null;
vehicleTaxInYear: number; vehicleTaxInYear: number;
vehicleTaxInLeasingPeriod: number; vehicleTaxInLeasingPeriod: number;
objectCategoryTax: objectCategoryTax: number | null;
| 100_000_000 objectTypeTax: number | null;
| 100_000_001 typePTS: number | null;
| 100_000_002
| 100_000_003
| 100_000_004
| 100_000_005
| 100_000_006
| 100_000_007
| 100_000_008
| 100_000_009
| 100_000_010
| 100_000_011
| 100_000_012
| 100_000_013
| 100_000_014
| 100_000_015
| 100_000_016
| 100_000_017
| 100_000_018
| 100_000_019
| 100_000_020
| null;
objectTypeTax:
| 100_000_000
| 100_000_001
| 100_000_002
| 100_000_003
| 100_000_004
| 100_000_005
| null;
typePTS: 100_000_000 | 100_000_001 | null;
legalClientRegion: string | null; legalClientRegion: string | null;
legalClientTown: string | null; legalClientTown: string | null;
subsidy: string | null; subsidy: string | null;