diff --git a/client/src/components/Filters/Services/Form.tsx b/client/src/components/Filters/Services/Form.tsx index 5d2be1ab..7df0cb18 100644 --- a/client/src/components/Filters/Services/Form.tsx +++ b/client/src/components/Filters/Services/Form.tsx @@ -1,30 +1,41 @@ import React from 'react'; -import { Field, reduxForm } from 'redux-form'; -import { Trans, withTranslation } from 'react-i18next'; -import flow from 'lodash/flow'; +import { Trans } from 'react-i18next'; -import { toggleAllServices } from '../../../helpers/helpers'; +import { Controller, useForm } from 'react-hook-form'; -import { renderServiceField } from '../../../helpers/form'; -import { FORM_NAME } from '../../../helpers/constants'; +import { ServiceField } from './ServiceField'; -interface FormProps { - blockedServices: unknown[]; - pristine: boolean; - handleSubmit: (...args: unknown[]) => string; - change: (...args: unknown[]) => unknown; - submitting: boolean; - processing: boolean; - processingSet: boolean; - t: (...args: unknown[]) => string; +type BlockedService = { + id: string; + name: string; + icon_svg: string; } -const Form = (props: FormProps) => { - const { blockedServices, handleSubmit, change, pristine, submitting, processing, processingSet } = props; +type FormValues = { + blocked_services: Record; +} + +interface FormProps { + initialValues: Record; + blockedServices: BlockedService[]; + onSubmit: (...args: unknown[]) => void; + processing: boolean; + processingSet: boolean; +} + +export const Form = ({ initialValues, blockedServices, processing, processingSet, onSubmit }: FormProps) => { + const { handleSubmit, control, setValue, formState: { isSubmitting, isDirty } } = useForm({ + mode: 'onChange', + defaultValues: initialValues, + }); + + const handleToggleAllServices = (isSelected: boolean) => { + blockedServices.forEach((service: BlockedService) => setValue(`blocked_services.${service.id}`, isSelected)); + }; return ( -
+
@@ -32,7 +43,8 @@ const Form = (props: FormProps) => { type="button" className="btn btn-secondary btn-block" disabled={processing || processingSet} - onClick={() => toggleAllServices(blockedServices, change, true)}> + onClick={() => handleToggleAllServices(true)} + > block_all
@@ -42,7 +54,8 @@ const Form = (props: FormProps) => { type="button" className="btn btn-secondary btn-block" disabled={processing || processingSet} - onClick={() => toggleAllServices(blockedServices, change, false)}> + onClick={() => handleToggleAllServices(false)} + > unblock_all
@@ -50,14 +63,18 @@ const Form = (props: FormProps) => {
{blockedServices.map((service: any) => ( - ( + + )} /> ))}
@@ -67,18 +84,10 @@ const Form = (props: FormProps) => {
); }; - -export default flow([ - withTranslation(), - reduxForm({ - form: FORM_NAME.SERVICES, - enableReinitialize: true, - }), -])(Form); diff --git a/client/src/components/Filters/Services/ServiceField.tsx b/client/src/components/Filters/Services/ServiceField.tsx new file mode 100644 index 00000000..e9a3d4be --- /dev/null +++ b/client/src/components/Filters/Services/ServiceField.tsx @@ -0,0 +1,53 @@ +import React from 'react'; +import cn from 'classnames'; +import { FieldValues, ControllerRenderProps } from 'react-hook-form'; + +interface ServiceFieldProps extends ControllerRenderProps { + placeholder: string; + disabled?: boolean; + className?: string; + icon?: string; + error?: string; +} + +export const ServiceField = React.forwardRef(({ + name, + value, + onChange, + onBlur, + placeholder, + disabled, + className, + icon, + error, +}, ref) => ( + <> +