|
| 1 | +import React, { FC, useContext } from 'react'; |
| 2 | +import { yupResolver } from '@hookform/resolvers/yup'; |
| 3 | +import { FormProvider, useForm } from 'react-hook-form'; |
| 4 | +import { ClusterName } from 'redux/interfaces'; |
| 5 | +import { useCreateConsumersAcl } from 'lib/hooks/api/acl'; |
| 6 | +import useAppParams from 'lib/hooks/useAppParams'; |
| 7 | +import ControlledMultiSelect from 'components/common/MultiSelect/ControlledMultiSelect'; |
| 8 | +import Input from 'components/common/Input/Input'; |
| 9 | +import * as S from 'components/ACLPage/Form/Form.styled'; |
| 10 | +import { AclDetailedFormProps, MatchType } from 'components/ACLPage/Form/types'; |
| 11 | +import useTopicsOptions from 'components/ACLPage/lib/useTopicsOptions'; |
| 12 | +import useConsumerGroupsOptions from 'components/ACLPage/lib/useConsumerGroupsOptions'; |
| 13 | +import ACLFormContext from 'components/ACLPage/Form/AclFormContext'; |
| 14 | +import MatchTypeSelector from 'components/ACLPage/Form/components/MatchTypeSelector'; |
| 15 | + |
| 16 | +import formSchema from './schema'; |
| 17 | +import { toRequest } from './lib'; |
| 18 | +import { FormValues } from './types'; |
| 19 | + |
| 20 | +const ForConsumersForm: FC<AclDetailedFormProps> = ({ formRef }) => { |
| 21 | + const context = useContext(ACLFormContext); |
| 22 | + const { clusterName } = useAppParams<{ clusterName: ClusterName }>(); |
| 23 | + const create = useCreateConsumersAcl(clusterName); |
| 24 | + const methods = useForm<FormValues>({ |
| 25 | + mode: 'all', |
| 26 | + resolver: yupResolver(formSchema), |
| 27 | + }); |
| 28 | + |
| 29 | + const { setValue } = methods; |
| 30 | + |
| 31 | + const onSubmit = async (data: FormValues) => { |
| 32 | + try { |
| 33 | + await create.createResource(toRequest(data)); |
| 34 | + context?.close(); |
| 35 | + } catch (e) { |
| 36 | + // no custom error |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + const topics = useTopicsOptions(clusterName); |
| 41 | + const consumerGroups = useConsumerGroupsOptions(clusterName); |
| 42 | + |
| 43 | + const onTopicTypeChange = (value: string) => { |
| 44 | + if (value === MatchType.EXACT) { |
| 45 | + setValue('topicsPrefix', undefined); |
| 46 | + } else { |
| 47 | + setValue('topics', undefined); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + const onConsumerGroupTypeChange = (value: string) => { |
| 52 | + if (value === MatchType.EXACT) { |
| 53 | + setValue('consumerGroupsPrefix', undefined); |
| 54 | + } else { |
| 55 | + setValue('consumerGroups', undefined); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + return ( |
| 60 | + <FormProvider {...methods}> |
| 61 | + <S.Form ref={formRef} onSubmit={methods.handleSubmit(onSubmit)}> |
| 62 | + <hr /> |
| 63 | + <S.Field> |
| 64 | + <S.Label htmlFor="principal">Principal</S.Label> |
| 65 | + <Input |
| 66 | + name="principal" |
| 67 | + id="principal" |
| 68 | + placeholder="Principal" |
| 69 | + withError |
| 70 | + /> |
| 71 | + </S.Field> |
| 72 | + |
| 73 | + <S.Field> |
| 74 | + <S.Label htmlFor="host">Host restriction</S.Label> |
| 75 | + <Input name="host" id="host" placeholder="Host" withError /> |
| 76 | + </S.Field> |
| 77 | + <hr /> |
| 78 | + |
| 79 | + <S.Field> |
| 80 | + <S.Label>From Topic(s)</S.Label> |
| 81 | + <S.ControlList> |
| 82 | + <MatchTypeSelector |
| 83 | + exact={<ControlledMultiSelect name="topics" options={topics} />} |
| 84 | + prefixed={<Input name="topicsPrefix" placeholder="Prefix..." />} |
| 85 | + onChange={onTopicTypeChange} |
| 86 | + /> |
| 87 | + </S.ControlList> |
| 88 | + </S.Field> |
| 89 | + |
| 90 | + <S.Field> |
| 91 | + <S.Field>Consumer group(s)</S.Field> |
| 92 | + <S.ControlList> |
| 93 | + <MatchTypeSelector |
| 94 | + exact={ |
| 95 | + <ControlledMultiSelect |
| 96 | + name="consumerGroups" |
| 97 | + options={consumerGroups} |
| 98 | + /> |
| 99 | + } |
| 100 | + prefixed={ |
| 101 | + <Input name="consumerGroupsPrefix" placeholder="Prefix..." /> |
| 102 | + } |
| 103 | + onChange={onConsumerGroupTypeChange} |
| 104 | + /> |
| 105 | + </S.ControlList> |
| 106 | + </S.Field> |
| 107 | + </S.Form> |
| 108 | + </FormProvider> |
| 109 | + ); |
| 110 | +}; |
| 111 | + |
| 112 | +export default React.memo(ForConsumersForm); |
0 commit comments