import type UserProfileConfig from "@keycloak/keycloak-admin-client/lib/defs/userProfileConfig"; import { ActionGroup, Alert, AlertVariant, Button, ButtonVariant, InputGroup, Select, SelectOption, SelectVariant, Text, TextContent, TextVariants, } from "@patternfly/react-core"; import { Form } from "react-router-dom"; import { KeycloakTextInput } from "../keycloak-text-input/KeycloakTextInput"; import { useTranslation } from "react-i18next"; import { useForm } from "react-hook-form"; import { isBundleKey, unWrap } from "../../user/utils"; import { CheckIcon } from "@patternfly/react-icons"; import { useAlerts } from "../alert/Alerts"; import { ReactNode, useState } from "react"; import { UserAttribute } from "./UserDataTable"; type UserDataTableAttributeSearchFormProps = { activeFilters: UserAttribute[]; setActiveFilters: (filters: UserAttribute[]) => void; profile: UserProfileConfig; createAttributeSearchChips: () => ReactNode; searchUserWithAttributes: () => void; }; export function UserDataTableAttributeSearchForm({ activeFilters, setActiveFilters, profile, createAttributeSearchChips, searchUserWithAttributes, }: UserDataTableAttributeSearchFormProps) { const { t } = useTranslation("users"); const { addAlert } = useAlerts(); const [selectAttributeKeyOpen, setSelectAttributeKeyOpen] = useState(false); const defaultValues: UserAttribute = { name: "", displayName: "", value: "", }; const { getValues, register, reset, formState: { errors }, setValue, setError, clearErrors, } = useForm({ mode: "onChange", defaultValues, }); const isAttributeKeyDuplicate = () => { return activeFilters.some((filter) => filter.name === getValues().name); }; const isAttributeNameValid = () => { let valid = false; if (!getValues().name.length) { setError("name", { type: "empty", message: t("searchUserByAttributeMissingKeyError"), }); } else if ( activeFilters.some((filter) => filter.name === getValues().name) ) { setError("name", { type: "conflict", message: t("searchUserByAttributeKeyAlreadyInUseError"), }); } else { valid = true; } return valid; }; const isAttributeValueValid = () => { let valid = false; if (!getValues().value.length) { setError("value", { type: "empty", message: t("searchUserByAttributeMissingValueError"), }); } else { valid = true; } return valid; }; const isAttributeValid = () => isAttributeNameValid() && isAttributeValueValid(); const addToFilter = () => { if (isAttributeValid()) { setActiveFilters([ ...activeFilters, { ...getValues(), }, ]); reset(); } else { errors.name?.message && addAlert(errors.name.message, AlertVariant.danger); errors.value?.message && addAlert(errors.value.message, AlertVariant.danger); } }; const clearActiveFilters = () => { const filtered = [...activeFilters].filter( (chip) => chip.name !== chip.name ); setActiveFilters(filtered); }; const createAttributeKeyInputField = () => { if (profile) { return ( ); } else { return ( e.key === "Enter" && addToFilter()} {...register("name", { required: true, validate: isAttributeNameValid, })} /> ); } }; return (
{t("selectAttributes")}
{t("common:key")}
{t("common:value")}
{createAttributeKeyInputField()}
{ if (e.key === "Enter") { e.preventDefault(); addToFilter(); } }} {...register("value", { required: true, validate: isAttributeValueValid, })} />
{createAttributeSearchChips()} ); }