From 0b76b1251859f7acc4daf4afd746b81945b7c720 Mon Sep 17 00:00:00 2001 From: Kirill Morozov Date: Tue, 12 Jul 2022 11:40:52 +0300 Subject: [PATCH] Added key-value form for stream parameters (#2191) * Added key-value form for stream parameters * Removed unused variable * fixing some test cases and fixing width of stream props * adding key value validation and tests * fixing placeholder padding and font size * remove unnecessary code Co-authored-by: rAzizbekyan Co-authored-by: Robert Azizbekyan <103438454+rAzizbekyan@users.noreply.github.com> --- .../src/components/KsqlDb/Query/Query.tsx | 14 +- .../Query/QueryForm/QueryForm.styled.ts | 41 ++++- .../KsqlDb/Query/QueryForm/QueryForm.tsx | 135 +++++++++------ .../QueryForm/__test__/QueryForm.spec.tsx | 156 +++--------------- .../KsqlDb/Query/__test__/Query.spec.tsx | 62 ++----- 5 files changed, 172 insertions(+), 236 deletions(-) diff --git a/kafka-ui-react-app/src/components/KsqlDb/Query/Query.tsx b/kafka-ui-react-app/src/components/KsqlDb/Query/Query.tsx index 4982807ac2..c5757e2cae 100644 --- a/kafka-ui-react-app/src/components/KsqlDb/Query/Query.tsx +++ b/kafka-ui-react-app/src/components/KsqlDb/Query/Query.tsx @@ -198,15 +198,23 @@ const Query: FC = () => { const submitHandler = useCallback( (values: FormValues) => { + const streamsProperties = values.streamsProperties.reduce( + (acc, current) => ({ + ...acc, + [current.key as keyof string]: current.value, + }), + {} as { [key: string]: string } + ); setFetching(true); dispatch( executeKsql({ clusterName, ksqlCommandV2: { ...values, - streamsProperties: values.streamsProperties - ? JSON.parse(values.streamsProperties) - : undefined, + streamsProperties: + values.streamsProperties[0].key !== '' + ? JSON.parse(JSON.stringify(streamsProperties)) + : undefined, }, }) ); diff --git a/kafka-ui-react-app/src/components/KsqlDb/Query/QueryForm/QueryForm.styled.ts b/kafka-ui-react-app/src/components/KsqlDb/Query/QueryForm/QueryForm.styled.ts index 980fa0c216..202e586290 100644 --- a/kafka-ui-react-app/src/components/KsqlDb/Query/QueryForm/QueryForm.styled.ts +++ b/kafka-ui-react-app/src/components/KsqlDb/Query/QueryForm/QueryForm.styled.ts @@ -27,8 +27,47 @@ export const KSQLButtons = styled.div` gap: 16px; `; +export const StreamPropertiesContainer = styled.label` + display: flex; + flex-direction: column; + gap: 10px; + width: 50%; +`; + +export const InputsContainer = styled.div` + display: flex; + justify-content: center; + gap: 10px; +`; + +export const StreamPropertiesInputWrapper = styled.div` + & > input { + height: 40px; + border: 1px solid grey; + border-radius: 4px; + min-width: 300px; + font-size: 16px; + padding-left: 15px; + } +`; + +export const DeleteButtonWrapper = styled.div` + min-height: 32px; + display: flex; + flex-direction: column; + align-items: center; + justify-self: flex-start; + margin-top: 10px; +`; + +export const LabelContainer = styled.div` + display: flex; + align-items: center; + justify-content: space-between; +`; + export const Fieldset = styled.fieldset` - width: 100%; + width: 50%; `; export const Editor = styled(BaseEditor)( diff --git a/kafka-ui-react-app/src/components/KsqlDb/Query/QueryForm/QueryForm.tsx b/kafka-ui-react-app/src/components/KsqlDb/Query/QueryForm/QueryForm.tsx index f824dd256a..a4d2a275be 100644 --- a/kafka-ui-react-app/src/components/KsqlDb/Query/QueryForm/QueryForm.tsx +++ b/kafka-ui-react-app/src/components/KsqlDb/Query/QueryForm/QueryForm.tsx @@ -1,11 +1,12 @@ import React from 'react'; import { FormError } from 'components/common/Input/Input.styled'; import { ErrorMessage } from '@hookform/error-message'; +import { useForm, Controller, useFieldArray } from 'react-hook-form'; +import { Button } from 'components/common/Button/Button'; +import IconButtonWrapper from 'components/common/Icons/IconButtonWrapper'; +import CloseIcon from 'components/common/Icons/CloseIcon'; import { yupResolver } from '@hookform/resolvers/yup'; import yup from 'lib/yupExtended'; -import { useForm, Controller } from 'react-hook-form'; -import { Button } from 'components/common/Button/Button'; -import { SchemaType } from 'generated-sources'; import * as S from './QueryForm.styled'; @@ -17,16 +18,22 @@ export interface Props { submitHandler: (values: FormValues) => void; } +export type StreamsPropertiesType = { + key: string; + value: string; +}; export type FormValues = { ksql: string; - streamsProperties: string; + streamsProperties: StreamsPropertiesType[]; }; +const streamsPropertiesSchema = yup.object().shape({ + key: yup.string().trim(), + value: yup.string().trim(), +}); const validationSchema = yup.object({ ksql: yup.string().trim().required(), - streamsProperties: yup.lazy((value) => - value === '' ? yup.string().trim() : yup.string().trim().isJsonObject() - ), + streamsProperties: yup.array().of(streamsPropertiesSchema), }); const QueryForm: React.FC = ({ @@ -46,9 +53,16 @@ const QueryForm: React.FC = ({ resolver: yupResolver(validationSchema), defaultValues: { ksql: '', - streamsProperties: '', + streamsProperties: [{ key: '', value: '' }], }, }); + const { fields, append, remove } = useFieldArray< + FormValues, + 'streamsProperties' + >({ + control, + name: 'streamsProperties', + }); return ( @@ -93,48 +107,69 @@ const QueryForm: React.FC = ({ - - - - - - ( - { - handleSubmit(submitHandler)(); - }, - }, - ]} - schemaType={SchemaType.JSON} - readOnly={fetching} - /> - )} - /> - - - - + + + Stream properties: + {fields.map((item, index) => ( + + + ( + + )} + /> + + + + + + ( + + )} + /> + + + + + + remove(index)}> + + + + + + ))} + +