Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support useStateAsInitial in getItemsParams #148

Merged
merged 3 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/shared/modules/state-and-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,37 @@ export interface GetItemsParamsArg {
config: Config;
itemsStateAndParams: ItemsStateAndParams;
plugins: PluginBase[];
useStateAsInitial?: boolean;
}

type GetItemsParamsReturn = Record<string, StringParams | Record<string, StringParams>>;

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<string, StringParams>)[item.id],
)
: Object.assign({}, stateAndParams[widgetId].params as StringParams);

return itemParams;
};

function getItemParams({
item,
itemsStateAndParams,
Expand All @@ -51,6 +78,8 @@ function getItemParams({
globalParams,
isFirstVersion,
queueData,
parentItemId,
useStateAsInitial,
}: {
item: ConfigItem | ConfigItemGroup;
itemsStateAndParams: ItemsStateAndParams;
Expand All @@ -61,6 +90,8 @@ function getItemParams({
globalParams: GlobalParams;
isFirstVersion: boolean;
queueData: FormedQueueData[];
parentItemId?: string;
useStateAsInitial?: boolean;
}) {
const {id, namespace} = item;

Expand Down Expand Up @@ -91,7 +122,11 @@ function getItemParams({
};
}, {}),
getMergedParams(globalParams),
useStateAsInitial
? getParamsFromStateAndParams({parentItemId, item, itemsStateAndParams})
: {},
);

if (isFirstVersion) {
itemParams = Object.assign(
itemParams,
Expand Down Expand Up @@ -133,6 +168,7 @@ export function getItemsParams({
config,
itemsStateAndParams,
plugins,
useStateAsInitial,
}: GetItemsParamsArg): GetItemsParamsReturn {
const {aliases, connections} = config;
const items = prerenderItems({items: config.items, plugins});
Expand Down Expand Up @@ -190,13 +226,15 @@ export function getItemsParams({
globalParams,
isFirstVersion,
queueData,
useStateAsInitial,
};

if (isItemWithGroup(item)) {
const groupParams = item.data.group.reduce(
(groupItemParams: Record<string, StringParams>, groupItem) => {
groupItemParams[groupItem.id] = getItemParams({
item: groupItem,
parentItemId: item.id,
...paramsOptions,
});
return groupItemParams;
Expand Down
77 changes: 46 additions & 31 deletions src/utils/update-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ function getAllowableChangedParams(

const stateParamsConf = stateAndParams.params;

// check if structure is StringParams or Record<string, StringParams>
// if it's Record<string, StringParams>, 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' &&
mournfulCoroner marked this conversation as resolved.
Show resolved Hide resolved
stateParamsConf?.[item.id] !== null &&
!Array.isArray(stateParamsConf?.[item.id]);

if (isItemWithTabs(item)) {
let tab;
if ('state' in stateAndParams && stateAndParams.state?.tabId) {
Expand All @@ -150,15 +158,14 @@ function getAllowableChangedParams(
}
allowedParams = pick(stateParamsConf, Object.keys(tab?.params || {})) as StringParams;
} else {
// check if structure is StringParams or Record<string, StringParams>
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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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})
Expand All @@ -577,10 +595,7 @@ export class UpdateManager {
[initiatorId]: {
$auto: {
params: {
[commandUpdateParams]: {
...allowableParams,
...allowableActionParams,
},
[commandUpdateParams]: changedParams,
},
...(hasState ? {state: {$set: stateAndParams.state}} : {}),
},
Expand Down
Loading