Edit.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import React from 'react';
  2. import {
  3. ClusterName,
  4. TopicFormDataRaw,
  5. TopicName,
  6. TopicConfigByName,
  7. TopicWithDetailedInfo,
  8. TopicFormData,
  9. } from 'redux/interfaces';
  10. import { useForm, FormProvider } from 'react-hook-form';
  11. import TopicForm from 'components/Topics/shared/Form/TopicForm';
  12. import { clusterTopicPath } from 'lib/paths';
  13. import { useHistory } from 'react-router-dom';
  14. import { yupResolver } from '@hookform/resolvers/yup';
  15. import { topicFormValidationSchema } from 'lib/yupExtended';
  16. import { TOPIC_CUSTOM_PARAMS_PREFIX, TOPIC_CUSTOM_PARAMS } from 'lib/constants';
  17. import styled from 'styled-components';
  18. import PageHeading from 'components/common/PageHeading/PageHeading';
  19. import DangerZoneContainer from './DangerZone/DangerZoneContainer';
  20. export interface Props {
  21. clusterName: ClusterName;
  22. topicName: TopicName;
  23. topic?: TopicWithDetailedInfo;
  24. isFetched: boolean;
  25. isTopicUpdated: boolean;
  26. fetchTopicConfig: (clusterName: ClusterName, topicName: TopicName) => void;
  27. updateTopic: (
  28. clusterName: ClusterName,
  29. topicName: TopicName,
  30. form: TopicFormDataRaw
  31. ) => void;
  32. updateTopicPartitionsCount: (
  33. clusterName: string,
  34. topicname: string,
  35. partitions: number
  36. ) => void;
  37. }
  38. const EditWrapperStyled = styled.div`
  39. display: flex;
  40. justify-content: center;
  41. & > * {
  42. width: 800px;
  43. }
  44. `;
  45. export const DEFAULTS = {
  46. partitions: 1,
  47. replicationFactor: 1,
  48. minInSyncReplicas: 1,
  49. cleanupPolicy: 'delete',
  50. retentionBytes: -1,
  51. maxMessageBytes: 1000012,
  52. };
  53. const topicParams = (topic: TopicWithDetailedInfo | undefined) => {
  54. if (!topic) {
  55. return DEFAULTS;
  56. }
  57. const { name, replicationFactor } = topic;
  58. return {
  59. ...DEFAULTS,
  60. name,
  61. partitions: topic.partitionCount || DEFAULTS.partitions,
  62. replicationFactor,
  63. [TOPIC_CUSTOM_PARAMS_PREFIX]: topic.config
  64. ?.filter(
  65. (el) =>
  66. el.value !== el.defaultValue &&
  67. Object.keys(TOPIC_CUSTOM_PARAMS).includes(el.name)
  68. )
  69. .map((el) => ({ name: el.name, value: el.value })),
  70. };
  71. };
  72. let formInit = false;
  73. const Edit: React.FC<Props> = ({
  74. clusterName,
  75. topicName,
  76. topic,
  77. isFetched,
  78. isTopicUpdated,
  79. fetchTopicConfig,
  80. updateTopic,
  81. }) => {
  82. const defaultValues = React.useMemo(() => topicParams(topic), [topic]);
  83. const methods = useForm<TopicFormData>({
  84. defaultValues,
  85. resolver: yupResolver(topicFormValidationSchema),
  86. });
  87. const [isSubmitting, setIsSubmitting] = React.useState<boolean>(false);
  88. const history = useHistory();
  89. React.useEffect(() => {
  90. fetchTopicConfig(clusterName, topicName);
  91. }, [fetchTopicConfig, clusterName, topicName]);
  92. React.useEffect(() => {
  93. if (isSubmitting && isTopicUpdated) {
  94. const { name } = methods.getValues();
  95. history.push(clusterTopicPath(clusterName, name));
  96. }
  97. }, [isSubmitting, isTopicUpdated, clusterName, methods, history]);
  98. if (!isFetched || !topic || !topic.config) {
  99. return null;
  100. }
  101. if (!formInit) {
  102. methods.reset(defaultValues);
  103. formInit = true;
  104. }
  105. const config: TopicConfigByName = {
  106. byName: {},
  107. };
  108. topic.config.forEach((param) => {
  109. config.byName[param.name] = param;
  110. });
  111. const onSubmit = async (data: TopicFormDataRaw) => {
  112. updateTopic(clusterName, topicName, data);
  113. setIsSubmitting(true); // Keep this action after updateTopic to prevent redirect before update.
  114. };
  115. return (
  116. <>
  117. <PageHeading text={`Edit ${topicName}`} />
  118. <EditWrapperStyled>
  119. <div>
  120. <FormProvider {...methods}>
  121. <TopicForm
  122. topicName={topicName}
  123. isSubmitting={isSubmitting}
  124. isEditing
  125. onSubmit={methods.handleSubmit(onSubmit)}
  126. />
  127. </FormProvider>
  128. {topic && (
  129. <DangerZoneContainer
  130. defaultPartitions={defaultValues.partitions}
  131. defaultReplicationFactor={
  132. defaultValues.replicationFactor || DEFAULTS.replicationFactor
  133. }
  134. />
  135. )}
  136. </div>
  137. </EditWrapperStyled>
  138. </>
  139. );
  140. };
  141. export default Edit;