diff --git a/src/client/hooks/useOptions.js b/src/client/hooks/useOptions.js index e426b16..ecd1e71 100644 --- a/src/client/hooks/useOptions.js +++ b/src/client/hooks/useOptions.js @@ -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, }; diff --git a/src/client/hooks/useValue.js b/src/client/hooks/useValue.js index 6d734b7..30fded4 100644 --- a/src/client/hooks/useValue.js +++ b/src/client/hooks/useValue.js @@ -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 }; }; diff --git a/src/client/stores/CalculationStore/Data/tables.js b/src/client/stores/CalculationStore/Data/tables.js index 9d49a7c..923163e 100644 --- a/src/client/stores/CalculationStore/Data/tables.js +++ b/src/client/stores/CalculationStore/Data/tables.js @@ -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); } }, diff --git a/src/core/config/initialTables.ts b/src/core/config/initialTables.ts index f75291d..90f9418 100644 --- a/src/core/config/initialTables.ts +++ b/src/core/config/initialTables.ts @@ -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 + }, + }, }, }; diff --git a/src/core/types/stores.ts b/src/core/types/stores.ts index 362df16..024ef30 100644 --- a/src/core/types/stores.ts +++ b/src/core/types/stores.ts @@ -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; + values: TValues; 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; + statuses?: TTableValues; + }, + ) => void; + setTableColumn: ( + { tableName }: { tableName: TableNames }, + { + options, + filters, + callbacks, + }: { + options?: TTableValues; + filters?: TTableValues; + callbacks?: TTableValues; }, ) => void; setRowOptions: ( diff --git a/src/core/types/tables.ts b/src/core/types/tables.ts index 6410eb8..4324bb5 100644 --- a/src/core/types/tables.ts +++ b/src/core/types/tables.ts @@ -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 = { [propName in TableValuesNames]?: T; }; +export type TCellCallback = ( + calculationStore: ICalculationStore, + rowIndex: number, + propName: TableValuesNames, +) => void; + export type IStoreTable = { [tableName in TableNames]?: { values?: TTableValues[]; - options?: TTableValues; statuses?: TTableValues[]; - filters?: TTableValues[]; + options?: TTableValues; + filters?: TTableValues; + callbacks?: TTableValues; }; };