import React, { useContext, useEffect, useState } from "react"; import { useHistory } from "react-router-dom"; import { useTranslation } from "react-i18next"; import { Controller, useForm } from "react-hook-form"; import { Form, FormGroup, PageSection, Select, SelectVariant, TextInput, SelectOption, ActionGroup, Button, SelectGroup, Split, SplitItem, Divider, } from "@patternfly/react-core"; import { useAlerts } from "../../components/alert/Alerts"; import { RealmContext } from "../../context/realm-context/RealmContext"; import { HttpClientContext } from "../../context/http-service/HttpClientContext"; import { ViewHeader } from "../../components/view-header/ViewHeader"; import { HelpItem } from "../../components/help-enabler/HelpItem"; import { ClientRepresentation, RoleRepresentation, } from "../../realm/models/Realm"; import { ProtocolMapperRepresentation } from "../models/client-scope"; export type RoleMappingFormProps = { clientScopeId: string; }; export const RoleMappingForm = ({ clientScopeId }: RoleMappingFormProps) => { const httpClient = useContext(HttpClientContext)!; const { realm } = useContext(RealmContext); const history = useHistory(); const { addAlert } = useAlerts(); const { t } = useTranslation("client-scopes"); const { register, handleSubmit, control, errors } = useForm(); const [roleOpen, setRoleOpen] = useState(false); const [roles, setRoles] = useState([]); const [clientsOpen, setClientsOpen] = useState(false); const [clients, setClients] = useState([]); const [selectedClient, setSelectedClient] = useState(); const [clientRoles, setClientRoles] = useState(); useEffect(() => { (async () => { const response = await httpClient.doGet( `/admin/realms/${realm}/roles` ); setRoles(response.data!); const clientResponse = await httpClient.doGet( `/admin/realms/${realm}/clients` ); const clients = clientResponse.data!; clients.map( (client) => (client.toString = function () { return this.name; }) ); setClients(clients); })(); }); useEffect(() => { (async () => { const client = selectedClient as ClientRepresentation; if (client && client.name !== "realmRoles") { const response = await httpClient.doGet( `/admin/realms/master/clients/${client.id}/roles` ); setClientRoles(response.data!); } })(); }, [selectedClient]); const save = async (mapping: ProtocolMapperRepresentation) => { try { await httpClient.doPost( `/admin/realms/${realm}/client-scopes/${clientScopeId}/protocol-mappers/models`, mapping ); addAlert(t("mapperCreateSuccess")); } catch (error) { addAlert(t("mapperCreateError", error)); } }; const createSelectGroup = (clients: ClientRepresentation[]) => { return [ t("realmRoles"), } as ClientRepresentation } > {realm} , , {clients.map((client) => ( {client.name} ))} , ]; }; const roleSelectOptions = () => { const createItem = (role: RoleRepresentation) => ( {role.name} ); if (clientRoles) { return clientRoles.map((role) => createItem(role)); } else { return roles.map((role) => createItem(role)); } }; return ( <>
} fieldId="protocolMapper" > } fieldId="name" isRequired validated={errors.name ? "error" : "default"} helperTextInvalid={t("common:required")} > } validated={errors["config.role"] ? "error" : "default"} helperTextInvalid={t("common:required")} fieldId="role" > ( )} /> } fieldId="newRoleName" >
); };