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 RowOptions = {
[ValueName in Values]?: BaseOption[];
[ValueName in Values]?: BaseOption<RowValues[ValueName]>[];
} & { key: Keys };
export type RowStatuses = Record<Values, Status> & {

View File

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

View File

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

View File

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

View File

@ -1,6 +1,6 @@
/* eslint-disable object-curly-newline */
/* 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 type { BaseOption } from 'Elements/types';
import { makeAutoObservable } from 'mobx';
@ -34,7 +34,7 @@ export default class OptionsStore {
this.options = initialOptions;
};
getOption(elementName: Elements) {
getOption<T extends Elements>(elementName: T) {
const value = this.root.$calculation.$values.getElementValue(elementName);
return this.options[elementName]?.find((x) => x.value === value);
@ -46,8 +46,16 @@ export default class OptionsStore {
return options;
}
setElementOptions = (elementName: Elements, options: BaseOption[]) => {
this.options[elementName] = options;
setElementOptions = <T extends Elements>(
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})
*/
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];
};
@ -83,7 +95,11 @@ export default class OptionsStore {
}
(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';
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;
lastPaymentPerc: number;
lastPaymentRub: number;
lastPaymentRule: 100_000_000 | 100_000_001 | 100_000_002 | null;
lastPaymentRule: number | null;
importProgram: string | null;
importProgramSum: number;
addEquipmentPrice: number;
redemptionPaymentSum: number;
balanceHolder: 100_000_000 | 100_000_001 | null;
graphType: 100_000_000 | 100_000_001 | 100_000_002 | 100_000_003 | 100_000_004 | null;
balanceHolder: number | null;
graphType: number | null;
parmentsDecreasePercent: number;
seasonType:
| 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;
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;
seasonType: number | null;
highSeasonStart: number | null;
comissionPerc: number;
comissionRub: number;
saleBonus: number;
IRR_Perc: number;
leaseObjectType: string | null;
deliveryTime:
| 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;
deliveryTime: number | null;
leaseObjectCount: number;
withTrailer: boolean;
leaseObjectUsed: boolean;
@ -79,32 +41,11 @@ export type CalculationValues = {
model: string | null;
configuration: string | null;
leaseObjectYear: number;
engineType: 100_000_000 | 100_000_001 | 100_000_002 | 100_000_003 | 100_000_004 | null;
leaseObjectCategory: 100_000_000 | 100_000_001 | 100_000_002 | 100_000_003 | 100_000_004 | null;
engineType: number | null;
leaseObjectCategory: number | null;
leaseObjectMotorPower: number;
engineVolume: number;
leaseObjectUseFor:
| 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;
leaseObjectUseFor: number | null;
dealer: string | null;
dealerPerson: string | null;
dealerRewardCondition: string | null;
@ -128,8 +69,8 @@ export type CalculationValues = {
GPSModel: string | null;
regionRegistration: string | null;
townRegistration: string | null;
infuranceOPF: 100_000_000 | 100_000_001 | null;
insKaskoType: 100_000_000 | 100_000_001 | null;
infuranceOPF: number | null;
insKaskoType: number | null;
insDecentral: boolean;
insFranchise: number;
insUnlimitDrivers: boolean;
@ -145,22 +86,13 @@ export type CalculationValues = {
technicalCardQuote: boolean;
NSIB: boolean;
quoteName: string | null;
quoteContactGender: 100_000_000 | 100_000_001 | null;
quoteContactGender: number | null;
quoteRedemptionGraph: boolean;
showFinGAP: boolean;
tarif: string | null;
creditRate: number;
rate: string | null;
requirementTelematic:
| 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;
requirementTelematic: number | null;
minPriceChange: number;
maxPriceChange: number;
importerRewardPerc: number;
@ -172,44 +104,15 @@ export type CalculationValues = {
telematic: string | null;
tracker: string | null;
mileage: number;
calcType: 100_000_000 | 100_000_001 | null;
calcType: number | null;
totalPayments: number;
objectRegistration: 100_000_000 | 100_000_001 | null;
objectRegistration: number | null;
objectRegionRegistration: string | null;
vehicleTaxInYear: number;
vehicleTaxInLeasingPeriod: number;
objectCategoryTax:
| 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
| 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;
objectCategoryTax: number | null;
objectTypeTax: number | null;
typePTS: number | null;
legalClientRegion: string | null;
legalClientTown: string | null;
subsidy: string | null;