kafka-ui/kafka-ui-react-app/src/components/Topics/Topic/Edit/topicParamsTransformer.ts
Mgrdich a0a464d455
[FE] Fix cleanup policy submit/display (#3067)
* TopicForm CleanupPolicy default value clash in the edit page

* minor code import modification in topicsParamsTransformer

Co-authored-by: Roman Zabaluev <rzabaluev@provectus.com>
2022-12-23 15:46:03 +04:00

45 lines
1.6 KiB
TypeScript

import {
MILLISECONDS_IN_WEEK,
TOPIC_CUSTOM_PARAMS,
TOPIC_CUSTOM_PARAMS_PREFIX,
} from 'lib/constants';
import { TOPIC_EDIT_FORM_DEFAULT_PROPS } from 'components/Topics/Topic/Edit/Edit';
import { getCleanUpPolicyValue } from 'components/Topics/shared/Form/TopicForm';
import { Topic, TopicConfig } from 'generated-sources';
export const getValue = (
config: TopicConfig[],
fieldName: string,
defaultValue?: number
) =>
Number(config.find(({ name }) => name === fieldName)?.value) || defaultValue;
const topicParamsTransformer = (topic?: Topic, config?: TopicConfig[]) => {
if (!config || !topic) {
return TOPIC_EDIT_FORM_DEFAULT_PROPS;
}
const customParams = config.reduce((acc, { name, value, defaultValue }) => {
if (value === defaultValue) return acc;
if (!TOPIC_CUSTOM_PARAMS[name]) return acc;
return [...acc, { name, value }];
}, [] as { name: string; value?: string }[]);
return {
...TOPIC_EDIT_FORM_DEFAULT_PROPS,
name: topic.name,
replicationFactor: topic.replicationFactor,
partitions:
topic.partitionCount || TOPIC_EDIT_FORM_DEFAULT_PROPS.partitions,
cleanupPolicy:
getCleanUpPolicyValue(topic.cleanUpPolicy) ||
TOPIC_EDIT_FORM_DEFAULT_PROPS.cleanupPolicy,
maxMessageBytes: getValue(config, 'max.message.bytes', 1000012),
minInSyncReplicas: getValue(config, 'min.insync.replicas', 1),
retentionBytes: getValue(config, 'retention.bytes', -1),
retentionMs: getValue(config, 'retention.ms', MILLISECONDS_IN_WEEK),
[TOPIC_CUSTOM_PARAMS_PREFIX]: customParams,
};
};
export default topicParamsTransformer;