add callback to table cell & fix filters
This commit is contained in:
parent
a2eed1a032
commit
30b2458be7
@ -14,13 +14,11 @@ export const useTableOptions = ({ tableName, rowIndex, propName }) => {
|
||||
const { calculationStore } = useStores();
|
||||
const options =
|
||||
(calculationStore.tables[tableName].options &&
|
||||
calculationStore.tables[tableName].options &&
|
||||
calculationStore.tables[tableName].options[propName]) ||
|
||||
[];
|
||||
const filter =
|
||||
calculationStore.tables[tableName].filters &&
|
||||
calculationStore.tables[tableName].filters[rowIndex] &&
|
||||
calculationStore.tables[tableName].filters[rowIndex][propName];
|
||||
calculationStore.tables[tableName].filters[propName];
|
||||
return {
|
||||
options: filter ? filter(options) : options,
|
||||
};
|
||||
|
||||
@ -59,5 +59,15 @@ export const useTableValue = ({ tableName, rowIndex, propName }) => {
|
||||
);
|
||||
}, [calculationStore, debouncedValue, propName, rowIndex, tableName]);
|
||||
|
||||
// onset debounced value to cell callback
|
||||
const cellCallback = calculationStore.tables[tableName].callbacks
|
||||
? calculationStore.tables[tableName].callbacks[propName]
|
||||
: undefined;
|
||||
useEffect(() => {
|
||||
if (cellCallback) {
|
||||
cellCallback(calculationStore, rowIndex, propName);
|
||||
}
|
||||
}, [cellCallback, debouncedValue]);
|
||||
|
||||
return { value: currentValue, setCurrentValue, debouncedValue };
|
||||
};
|
||||
|
||||
@ -5,7 +5,7 @@ const tablesData = {
|
||||
};
|
||||
|
||||
const tablesActions = {
|
||||
setTableRow({ tableName, rowIndex }, { values, statuses, filters }) {
|
||||
setTableRow({ tableName, rowIndex }, { values, statuses }) {
|
||||
if (!this.tables[tableName]) {
|
||||
throw new Error(`Table ${tableName} doesn't exist in store`);
|
||||
}
|
||||
@ -13,7 +13,7 @@ const tablesActions = {
|
||||
rowIndex = this.tables[tableName].values.length;
|
||||
}
|
||||
|
||||
const applyParams = (targetName, values) => {
|
||||
const applyRowParams = (targetName, values) => {
|
||||
if (!this.tables[tableName][targetName]) {
|
||||
this.tables[tableName][targetName] = [];
|
||||
}
|
||||
@ -24,13 +24,37 @@ const tablesActions = {
|
||||
};
|
||||
|
||||
if (values && Object.keys(values).length > 0) {
|
||||
applyParams('values', values);
|
||||
applyRowParams('values', values);
|
||||
}
|
||||
if (statuses && Object.keys(statuses).length > 0) {
|
||||
applyParams('statuses', statuses);
|
||||
applyRowParams('statuses', statuses);
|
||||
}
|
||||
},
|
||||
|
||||
setTableColumn({ tableName }, { filters, options, callbacks }) {
|
||||
if (!this.tables[tableName]) {
|
||||
throw new Error(`Table ${tableName} doesn't exist in store`);
|
||||
}
|
||||
|
||||
const applyColumnParams = (targetName, values) => {
|
||||
if (!this.tables[tableName][targetName]) {
|
||||
this.tables[tableName][targetName] = {};
|
||||
}
|
||||
this.tables[tableName][targetName] = Object.assign(
|
||||
{},
|
||||
this.tables[tableName][targetName],
|
||||
values,
|
||||
);
|
||||
};
|
||||
|
||||
if (filters && Object.keys(filters).length > 0) {
|
||||
applyParams('filters', filters);
|
||||
applyColumnParams('filters', filters);
|
||||
}
|
||||
if (options && Object.keys(options).length > 0) {
|
||||
applyColumnParams('options', options);
|
||||
}
|
||||
if (callbacks && Object.keys(callbacks).length > 0) {
|
||||
applyColumnParams('callbacks', callbacks);
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import { Status } from 'core/types/statuses';
|
||||
import { IStoreTable } from './../types/tables';
|
||||
const initialTables: IStoreTable = {
|
||||
tableInsurance: {
|
||||
@ -17,6 +18,12 @@ const initialTables: IStoreTable = {
|
||||
insuranceTerm: 36,
|
||||
},
|
||||
],
|
||||
statuses: [
|
||||
{
|
||||
policyType: Status.Disabled,
|
||||
insuranceCompany: Status.Default,
|
||||
},
|
||||
],
|
||||
options: {
|
||||
policyType: [
|
||||
{ name: 'ОСАГО', value: 'osago' },
|
||||
@ -25,6 +32,20 @@ const initialTables: IStoreTable = {
|
||||
{ name: 'НС', value: 'nc' },
|
||||
],
|
||||
},
|
||||
filters: {
|
||||
policyType: options => options,
|
||||
insuranceCompany: options => options,
|
||||
},
|
||||
callbacks: {
|
||||
policyType: (calculationStore, rowIndex, propName) => {
|
||||
console.log('policyType Callback', rowIndex, propName);
|
||||
//action
|
||||
},
|
||||
insuranceCompany: (calculationStore, rowIndex, propName) => {
|
||||
console.log('insuranceCompany Callback', rowIndex, propName);
|
||||
//action
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@ -3,7 +3,13 @@ import { IBaseOption, IOption } from 'core/types/Calculation/options';
|
||||
import { ElementsNames, TElements } from './elements';
|
||||
import { StaticDataNames, TStaticData } from './staticData';
|
||||
import { Status } from './statuses';
|
||||
import { IStoreTable, TableNames, TableValuesNames } from './tables';
|
||||
import {
|
||||
IStoreTable,
|
||||
TableNames,
|
||||
TableValuesNames,
|
||||
TCellCallback,
|
||||
TTableValues,
|
||||
} from './tables';
|
||||
import { TValue, TValues, ValuesNames } from './values';
|
||||
|
||||
export interface ICalculationStore {
|
||||
@ -24,7 +30,7 @@ export interface ICalculationStore {
|
||||
) => void;
|
||||
// applyFilters: (filters: TElementFilter[]) => void;
|
||||
|
||||
values: TValues<TValue>;
|
||||
values: TValues<any>;
|
||||
getValue: (sourceValueName: ValuesNames) => TValue;
|
||||
setValue: (sourceValueName: ValuesNames, newValue: TValue) => void;
|
||||
|
||||
@ -45,11 +51,21 @@ export interface ICalculationStore {
|
||||
{
|
||||
values,
|
||||
statuses,
|
||||
filters,
|
||||
}: {
|
||||
values?: { [prop in TableValuesNames]?: TValue };
|
||||
statuses?: { [prop in TableValuesNames]?: Status };
|
||||
filters?: { [prop in TableValuesNames]?: TElementFilter };
|
||||
values?: TTableValues<any>;
|
||||
statuses?: TTableValues<Status>;
|
||||
},
|
||||
) => void;
|
||||
setTableColumn: (
|
||||
{ tableName }: { tableName: TableNames },
|
||||
{
|
||||
options,
|
||||
filters,
|
||||
callbacks,
|
||||
}: {
|
||||
options?: TTableValues<IOption[]>;
|
||||
filters?: TTableValues<TElementFilter>;
|
||||
callbacks?: TTableValues<TCellCallback>;
|
||||
},
|
||||
) => void;
|
||||
setRowOptions: (
|
||||
|
||||
@ -1,3 +1,6 @@
|
||||
import { ICalculationStore } from 'core/types/stores';
|
||||
import { TElementFilter } from 'core/types/Calculation/filters';
|
||||
import { IOption } from 'core/types/Calculation/options';
|
||||
import { Status } from './statuses';
|
||||
export type TableNames = 'tableInsurance';
|
||||
export type TableValuesNames =
|
||||
@ -11,11 +14,18 @@ export type TTableValues<T> = {
|
||||
[propName in TableValuesNames]?: T;
|
||||
};
|
||||
|
||||
export type TCellCallback = (
|
||||
calculationStore: ICalculationStore,
|
||||
rowIndex: number,
|
||||
propName: TableValuesNames,
|
||||
) => void;
|
||||
|
||||
export type IStoreTable = {
|
||||
[tableName in TableNames]?: {
|
||||
values?: TTableValues<any>[];
|
||||
options?: TTableValues<any>;
|
||||
statuses?: TTableValues<Status>[];
|
||||
filters?: TTableValues<any>[];
|
||||
options?: TTableValues<IOption[]>;
|
||||
filters?: TTableValues<TElementFilter>;
|
||||
callbacks?: TTableValues<TCellCallback>;
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user