|
| 1 | +import React, { useEffect } from 'react'; |
| 2 | +import { Trans, useTranslation } from 'react-i18next'; |
| 3 | +import { Controller, useFormContext } from 'react-hook-form'; |
| 4 | +import { useDispatch, useSelector } from 'react-redux'; |
| 5 | +import { ClientForm } from '../types'; |
| 6 | +import { ServiceField } from '../../../../Filters/Services/ServiceField'; |
| 7 | +import { RootState } from '../../../../../initialState'; |
| 8 | +import { getFilteringStatus } from '../../../../../actions/filtering'; |
| 9 | +import { Filter } from '../../../../../helpers/helpers'; |
| 10 | + |
| 11 | +export const FilterLists = () => { |
| 12 | + const { t } = useTranslation(); |
| 13 | + const dispatch = useDispatch(); |
| 14 | + const { watch, setValue, control } = useFormContext<ClientForm>(); |
| 15 | + |
| 16 | + const useGlobalFilterLists = watch('use_global_filter_lists'); |
| 17 | + |
| 18 | + const filters: Filter[] = useSelector((state: RootState) => state?.filtering?.filters) || []; |
| 19 | + const whitelistFilters: Filter[] = |
| 20 | + useSelector((state: RootState) => state?.filtering?.whitelistFilters) || []; |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + dispatch(getFilteringStatus()); |
| 24 | + }, [dispatch]); |
| 25 | + |
| 26 | + const handleToggleAllBlocklists = (isSelected: boolean) => { |
| 27 | + filters.forEach((filter) => setValue(`filter_list_ids.${filter.id}`, isSelected)); |
| 28 | + }; |
| 29 | + |
| 30 | + const handleToggleAllAllowlists = (isSelected: boolean) => { |
| 31 | + whitelistFilters.forEach((filter) => setValue(`allow_filter_list_ids.${filter.id}`, isSelected)); |
| 32 | + }; |
| 33 | + |
| 34 | + return ( |
| 35 | + <div title={t('client_filter_lists')}> |
| 36 | + <div className="form__group"> |
| 37 | + <Controller |
| 38 | + name="use_global_filter_lists" |
| 39 | + control={control} |
| 40 | + render={({ field }) => ( |
| 41 | + <ServiceField |
| 42 | + {...field} |
| 43 | + data-testid="clients_use_global_filter_lists" |
| 44 | + placeholder={t('use_global_filter_lists')} |
| 45 | + className="service--global" |
| 46 | + /> |
| 47 | + )} |
| 48 | + /> |
| 49 | + |
| 50 | + <div className="form__desc mt-0 mb-2"> |
| 51 | + <Trans>client_filter_lists_desc</Trans> |
| 52 | + </div> |
| 53 | + |
| 54 | + {filters.length > 0 && ( |
| 55 | + <> |
| 56 | + <div className="form__label mt-3"> |
| 57 | + <strong> |
| 58 | + <Trans>dns_blocklists</Trans> |
| 59 | + </strong> |
| 60 | + </div> |
| 61 | + |
| 62 | + <div className="row mb-2"> |
| 63 | + <div className="col-6"> |
| 64 | + <button |
| 65 | + type="button" |
| 66 | + className="btn btn-secondary btn-block btn-sm" |
| 67 | + disabled={useGlobalFilterLists} |
| 68 | + onClick={() => handleToggleAllBlocklists(true)}> |
| 69 | + <Trans>select_all</Trans> |
| 70 | + </button> |
| 71 | + </div> |
| 72 | + |
| 73 | + <div className="col-6"> |
| 74 | + <button |
| 75 | + type="button" |
| 76 | + className="btn btn-secondary btn-block btn-sm" |
| 77 | + disabled={useGlobalFilterLists} |
| 78 | + onClick={() => handleToggleAllBlocklists(false)}> |
| 79 | + <Trans>deselect_all</Trans> |
| 80 | + </button> |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + |
| 84 | + <div className="services services--half"> |
| 85 | + {filters.map((filter: Filter) => ( |
| 86 | + <Controller |
| 87 | + key={filter.id} |
| 88 | + name={`filter_list_ids.${filter.id}`} |
| 89 | + control={control} |
| 90 | + render={({ field }) => ( |
| 91 | + <ServiceField |
| 92 | + {...field} |
| 93 | + data-testid={`clients_filter_${filter.id}`} |
| 94 | + placeholder={filter.name} |
| 95 | + disabled={useGlobalFilterLists} |
| 96 | + /> |
| 97 | + )} |
| 98 | + /> |
| 99 | + ))} |
| 100 | + </div> |
| 101 | + </> |
| 102 | + )} |
| 103 | + |
| 104 | + {whitelistFilters.length > 0 && ( |
| 105 | + <> |
| 106 | + <div className="form__label mt-3"> |
| 107 | + <strong> |
| 108 | + <Trans>dns_allowlists</Trans> |
| 109 | + </strong> |
| 110 | + </div> |
| 111 | + |
| 112 | + <div className="row mb-2"> |
| 113 | + <div className="col-6"> |
| 114 | + <button |
| 115 | + type="button" |
| 116 | + className="btn btn-secondary btn-block btn-sm" |
| 117 | + disabled={useGlobalFilterLists} |
| 118 | + onClick={() => handleToggleAllAllowlists(true)}> |
| 119 | + <Trans>select_all</Trans> |
| 120 | + </button> |
| 121 | + </div> |
| 122 | + |
| 123 | + <div className="col-6"> |
| 124 | + <button |
| 125 | + type="button" |
| 126 | + className="btn btn-secondary btn-block btn-sm" |
| 127 | + disabled={useGlobalFilterLists} |
| 128 | + onClick={() => handleToggleAllAllowlists(false)}> |
| 129 | + <Trans>deselect_all</Trans> |
| 130 | + </button> |
| 131 | + </div> |
| 132 | + </div> |
| 133 | + |
| 134 | + <div className="services services--half"> |
| 135 | + {whitelistFilters.map((filter: Filter) => ( |
| 136 | + <Controller |
| 137 | + key={filter.id} |
| 138 | + name={`allow_filter_list_ids.${filter.id}`} |
| 139 | + control={control} |
| 140 | + render={({ field }) => ( |
| 141 | + <ServiceField |
| 142 | + {...field} |
| 143 | + data-testid={`clients_allowfilter_${filter.id}`} |
| 144 | + placeholder={filter.name} |
| 145 | + disabled={useGlobalFilterLists} |
| 146 | + /> |
| 147 | + )} |
| 148 | + /> |
| 149 | + ))} |
| 150 | + </div> |
| 151 | + </> |
| 152 | + )} |
| 153 | + </div> |
| 154 | + </div> |
| 155 | + ); |
| 156 | +}; |
0 commit comments