New.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import React from 'react';
  2. import { NewSchemaSubjectRaw } from 'redux/interfaces';
  3. import { FormProvider, useForm, Controller } from 'react-hook-form';
  4. import { ErrorMessage } from '@hookform/error-message';
  5. import { clusterSchemaPath } from 'lib/paths';
  6. import { SchemaType } from 'generated-sources';
  7. import { SCHEMA_NAME_VALIDATION_PATTERN } from 'lib/constants';
  8. import { useHistory, useParams } from 'react-router';
  9. import { InputLabel } from 'components/common/Input/InputLabel.styled';
  10. import Input from 'components/common/Input/Input';
  11. import { FormError } from 'components/common/Input/Input.styled';
  12. import Select, { SelectOption } from 'components/common/Select/Select';
  13. import { Button } from 'components/common/Button/Button';
  14. import { Textarea } from 'components/common/Textbox/Textarea.styled';
  15. import PageHeading from 'components/common/PageHeading/PageHeading';
  16. import {
  17. schemaAdded,
  18. schemasApiClient,
  19. } from 'redux/reducers/schemas/schemasSlice';
  20. import { useAppDispatch } from 'lib/hooks/redux';
  21. import { serverErrorAlertAdded } from 'redux/reducers/alerts/alertsSlice';
  22. import { getResponse } from 'lib/errorHandling';
  23. import * as S from './New.styled';
  24. const SchemaTypeOptions: Array<SelectOption> = [
  25. { value: SchemaType.AVRO, label: 'AVRO' },
  26. { value: SchemaType.JSON, label: 'JSON' },
  27. { value: SchemaType.PROTOBUF, label: 'PROTOBUF' },
  28. ];
  29. const New: React.FC = () => {
  30. const { clusterName } = useParams<{ clusterName: string }>();
  31. const history = useHistory();
  32. const dispatch = useAppDispatch();
  33. const methods = useForm<NewSchemaSubjectRaw>();
  34. const {
  35. register,
  36. handleSubmit,
  37. control,
  38. formState: { isDirty, isSubmitting, errors },
  39. } = methods;
  40. const onSubmit = React.useCallback(
  41. async ({ subject, schema, schemaType }: NewSchemaSubjectRaw) => {
  42. try {
  43. const resp = await schemasApiClient.createNewSchema({
  44. clusterName,
  45. newSchemaSubject: { subject, schema, schemaType },
  46. });
  47. dispatch(schemaAdded(resp));
  48. history.push(clusterSchemaPath(clusterName, subject));
  49. } catch (e) {
  50. const err = await getResponse(e as Response);
  51. dispatch(serverErrorAlertAdded(err));
  52. }
  53. },
  54. [clusterName, dispatch, history]
  55. );
  56. return (
  57. <FormProvider {...methods}>
  58. <PageHeading text="Create new schema" />
  59. <S.Form onSubmit={handleSubmit(onSubmit)}>
  60. <div>
  61. <InputLabel>Subject *</InputLabel>
  62. <Input
  63. inputSize="M"
  64. placeholder="Schema Name"
  65. name="subject"
  66. hookFormOptions={{
  67. required: 'Schema Name is required.',
  68. pattern: {
  69. value: SCHEMA_NAME_VALIDATION_PATTERN,
  70. message: 'Only alphanumeric, _, -, and . allowed',
  71. },
  72. }}
  73. autoComplete="off"
  74. disabled={isSubmitting}
  75. />
  76. <FormError>
  77. <ErrorMessage errors={errors} name="subject" />
  78. </FormError>
  79. </div>
  80. <div>
  81. <InputLabel>Schema *</InputLabel>
  82. <Textarea
  83. {...register('schema', {
  84. required: 'Schema is required.',
  85. })}
  86. disabled={isSubmitting}
  87. />
  88. <FormError>
  89. <ErrorMessage errors={errors} name="schema" />
  90. </FormError>
  91. </div>
  92. <div>
  93. <InputLabel>Schema Type *</InputLabel>
  94. <Controller
  95. defaultValue={SchemaTypeOptions[0].value as SchemaType}
  96. control={control}
  97. rules={{ required: 'Schema Type is required.' }}
  98. name="schemaType"
  99. render={({ field: { name, onChange } }) => (
  100. <Select
  101. selectSize="M"
  102. name={name}
  103. value={SchemaTypeOptions[0].value}
  104. onChange={onChange}
  105. minWidth="50%"
  106. disabled={isSubmitting}
  107. options={SchemaTypeOptions}
  108. />
  109. )}
  110. />
  111. <FormError>
  112. <ErrorMessage errors={errors} name="schemaType" />
  113. </FormError>
  114. </div>
  115. <Button
  116. buttonSize="M"
  117. buttonType="primary"
  118. type="submit"
  119. disabled={isSubmitting || !isDirty}
  120. >
  121. Submit
  122. </Button>
  123. </S.Form>
  124. </FormProvider>
  125. );
  126. };
  127. export default New;