New.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import React from 'react';
  2. import { ClusterName, TopicName, TopicFormData } from 'redux/interfaces';
  3. import { useForm, FormProvider } from 'react-hook-form';
  4. import Breadcrumb from 'components/common/Breadcrumb/Breadcrumb';
  5. import { clusterTopicsPath } from 'lib/paths';
  6. import TopicForm from 'components/Topics/shared/Form/TopicForm';
  7. interface Props {
  8. clusterName: ClusterName;
  9. isTopicCreated: boolean;
  10. createTopic: (clusterName: ClusterName, form: TopicFormData) => void;
  11. redirectToTopicPath: (clusterName: ClusterName, topicName: TopicName) => void;
  12. resetUploadedState: () => void;
  13. }
  14. const New: React.FC<Props> = ({
  15. clusterName,
  16. isTopicCreated,
  17. createTopic,
  18. redirectToTopicPath,
  19. }) => {
  20. const methods = useForm<TopicFormData>();
  21. const [isSubmitting, setIsSubmitting] = React.useState<boolean>(false);
  22. React.useEffect(() => {
  23. if (isSubmitting && isTopicCreated) {
  24. const { name } = methods.getValues();
  25. redirectToTopicPath(clusterName, name);
  26. }
  27. }, [isSubmitting, isTopicCreated, redirectToTopicPath, clusterName, methods]);
  28. const onSubmit = async (data: TopicFormData) => {
  29. // TODO: need to fix loader. After success loading the first time, we won't wait for creation any more, because state is
  30. // loaded, and we will try to get entity immediately after pressing the button, and we will receive null
  31. // going to object page on the second creation. Setting of isSubmitting after createTopic is a workaround, need to tweak loader logic
  32. createTopic(clusterName, data);
  33. setIsSubmitting(true); // Keep this action after createTopic to prevent redirect before create.
  34. };
  35. return (
  36. <div className="section">
  37. <div className="level">
  38. <div className="level-item level-left">
  39. <Breadcrumb
  40. links={[
  41. { href: clusterTopicsPath(clusterName), label: 'All Topics' },
  42. ]}
  43. >
  44. New Topic
  45. </Breadcrumb>
  46. </div>
  47. </div>
  48. <div className="box">
  49. {/* eslint-disable react/jsx-props-no-spreading */}
  50. <FormProvider {...methods}>
  51. <TopicForm
  52. isSubmitting={isSubmitting}
  53. onSubmit={methods.handleSubmit(onSubmit)}
  54. />
  55. </FormProvider>
  56. </div>
  57. </div>
  58. );
  59. };
  60. export default New;