New.tsx 2.3 KB

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