From ede2db0b219697016aafd4fed9a210f2b9c52fb4 Mon Sep 17 00:00:00 2001 From: Taya Leutina Date: Tue, 11 Jun 2024 18:21:02 +0300 Subject: [PATCH] feat: support useStateAsInitial in getItemsParams (#148) * feat: support useStateAsInitial in getItemsParams * fix: usage of useStateAsInitial * fix: type checking --- src/shared/modules/state-and-params.ts | 38 +++++++++++++ src/utils/update-manager.ts | 77 +++++++++++++++----------- 2 files changed, 84 insertions(+), 31 deletions(-) diff --git a/src/shared/modules/state-and-params.ts b/src/shared/modules/state-and-params.ts index 47dd7f6..a4d4ac9 100644 --- a/src/shared/modules/state-and-params.ts +++ b/src/shared/modules/state-and-params.ts @@ -37,10 +37,37 @@ export interface GetItemsParamsArg { config: Config; itemsStateAndParams: ItemsStateAndParams; plugins: PluginBase[]; + useStateAsInitial?: boolean; } type GetItemsParamsReturn = Record>; +const getParamsFromStateAndParams = ({ + itemsStateAndParams, + parentItemId, + item, +}: { + item: ConfigItem | ConfigItemGroup; + itemsStateAndParams: ItemsStateAndParams; + parentItemId?: string; +}) => { + const widgetId = parentItemId || item.id; + + if (!(widgetId in itemsStateAndParams)) { + return {}; + } + + const stateAndParams = itemsStateAndParams as ItemsStateAndParamsBase; + const itemParams = parentItemId + ? Object.assign( + {}, + (stateAndParams[widgetId].params as Record)[item.id], + ) + : Object.assign({}, stateAndParams[widgetId].params as StringParams); + + return itemParams; +}; + function getItemParams({ item, itemsStateAndParams, @@ -51,6 +78,8 @@ function getItemParams({ globalParams, isFirstVersion, queueData, + parentItemId, + useStateAsInitial, }: { item: ConfigItem | ConfigItemGroup; itemsStateAndParams: ItemsStateAndParams; @@ -61,6 +90,8 @@ function getItemParams({ globalParams: GlobalParams; isFirstVersion: boolean; queueData: FormedQueueData[]; + parentItemId?: string; + useStateAsInitial?: boolean; }) { const {id, namespace} = item; @@ -91,7 +122,11 @@ function getItemParams({ }; }, {}), getMergedParams(globalParams), + useStateAsInitial + ? getParamsFromStateAndParams({parentItemId, item, itemsStateAndParams}) + : {}, ); + if (isFirstVersion) { itemParams = Object.assign( itemParams, @@ -133,6 +168,7 @@ export function getItemsParams({ config, itemsStateAndParams, plugins, + useStateAsInitial, }: GetItemsParamsArg): GetItemsParamsReturn { const {aliases, connections} = config; const items = prerenderItems({items: config.items, plugins}); @@ -190,6 +226,7 @@ export function getItemsParams({ globalParams, isFirstVersion, queueData, + useStateAsInitial, }; if (isItemWithGroup(item)) { @@ -197,6 +234,7 @@ export function getItemsParams({ (groupItemParams: Record, groupItem) => { groupItemParams[groupItem.id] = getItemParams({ item: groupItem, + parentItemId: item.id, ...paramsOptions, }); return groupItemParams; diff --git a/src/utils/update-manager.ts b/src/utils/update-manager.ts index 8872164..726b11c 100644 --- a/src/utils/update-manager.ts +++ b/src/utils/update-manager.ts @@ -139,6 +139,14 @@ function getAllowableChangedParams( const stateParamsConf = stateAndParams.params; + // check if structure is StringParams or Record + // if it's Record, then this is a group application of params + // and checking for comparison of allowedParams and stateParamsConf is not necessary + const isGroupParamsApply = + typeof stateParamsConf?.[item.id] === 'object' && + stateParamsConf?.[item.id] !== null && + !Array.isArray(stateParamsConf?.[item.id]); + if (isItemWithTabs(item)) { let tab; if ('state' in stateAndParams && stateAndParams.state?.tabId) { @@ -150,15 +158,14 @@ function getAllowableChangedParams( } allowedParams = pick(stateParamsConf, Object.keys(tab?.params || {})) as StringParams; } else { - // check if structure is StringParams or Record - const paramsConf = - typeof stateParamsConf?.[item.id] === 'object' && - !Array.isArray(stateParamsConf?.[item.id]) - ? stateParamsConf[item.id] - : stateParamsConf; + const paramsConf = isGroupParamsApply ? stateParamsConf[item.id] : stateParamsConf; allowedParams = pick(paramsConf, Object.keys(item.defaults || {})) as StringParams; } - if (Object.keys(allowedParams || {}).length !== Object.keys(stateParamsConf || {}).length) { + + if ( + !isGroupParamsApply && + Object.keys(allowedParams || {}).length !== Object.keys(stateParamsConf || {}).length + ) { console.warn('Параметры, которых нет в defaults, будут проигнорированы!'); } return allowedParams; @@ -293,6 +300,31 @@ function getNewItemData({item, config, counter: argsCounter, salt, options}: Get return {data, counter, excludeIds}; } +export const getChangedParams = ({ + initiatorItem, + stateAndParams, + itemsStateAndParams, +}: { + initiatorItem: ConfigItem | ConfigItemGroup; + stateAndParams: ItemStateAndParams; + itemsStateAndParams: ItemsStateAndParams; +}) => { + const allowableParams = getAllowableChangedParams( + initiatorItem, + stateAndParams, + itemsStateAndParams, + ); + + const allowableActionParams = getAllowableChangedParams( + initiatorItem, + stateAndParams, + itemsStateAndParams, + {type: 'actionParams', returnPrefix: true}, + ); + + return {...allowableParams, ...allowableActionParams}; +}; + function changeGroupParams({ groupItemIds, initiatorId, @@ -322,20 +354,13 @@ function changeGroupParams({ for (const groupItem of initiatorItem.data.group) { if (groupItemIds.includes(groupItem.id)) { - const allowableParams = getAllowableChangedParams( - groupItem, - stateAndParams, - itemsStateAndParams, - ); - - const allowableActionParams = getAllowableChangedParams( - groupItem, + const changedParams = getChangedParams({ + initiatorItem: groupItem, stateAndParams, itemsStateAndParams, - {type: 'actionParams', returnPrefix: true}, - ); + }); - updatedItems[groupItem.id] = {...allowableParams, ...allowableActionParams}; + updatedItems[groupItem.id] = changedParams; continue; } @@ -546,18 +571,11 @@ export class UpdateManager { } if ('params' in stateAndParams) { - const allowableParams = getAllowableChangedParams( + const changedParams = getChangedParams({ initiatorItem, stateAndParams, itemsStateAndParams, - ); - - const allowableActionParams = getAllowableChangedParams( - initiatorItem, - stateAndParams, - itemsStateAndParams, - {type: 'actionParams', returnPrefix: true}, - ); + }); const tabId: string | undefined = isItemWithTabs(initiatorItem) ? newTabId || resolveItemInnerId({item: initiatorItem, itemsStateAndParams}) @@ -577,10 +595,7 @@ export class UpdateManager { [initiatorId]: { $auto: { params: { - [commandUpdateParams]: { - ...allowableParams, - ...allowableActionParams, - }, + [commandUpdateParams]: changedParams, }, ...(hasState ? {state: {$set: stateAndParams.state}} : {}), },