import { useState } from "react"; import { useParams } from "react-router-dom"; import { Link, useNavigate } from "react-router-dom-v5-compat"; import { Controller, FormProvider, useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { ActionGroup, Alert, AlertVariant, Button, ButtonVariant, DropdownItem, FormGroup, PageSection, Switch, ValidatedOptions, } from "@patternfly/react-core"; import type ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientRepresentation"; import type ResourceRepresentation from "@keycloak/keycloak-admin-client/lib/defs/resourceRepresentation"; import type ResourceServerRepresentation from "@keycloak/keycloak-admin-client/lib/defs/resourceServerRepresentation"; import { ResourceDetailsParams, toResourceDetails } from "../routes/Resource"; import { KeycloakSpinner } from "../../components/keycloak-spinner/KeycloakSpinner"; import { useAdminClient, useFetch } from "../../context/auth/AdminClient"; import { HelpItem } from "../../components/help-enabler/HelpItem"; import { ViewHeader } from "../../components/view-header/ViewHeader"; import { useConfirmDialog } from "../../components/confirm-dialog/ConfirmDialog"; import { useAlerts } from "../../components/alert/Alerts"; import { FormAccess } from "../../components/form-access/FormAccess"; import type { KeyValueType } from "../../components/key-value-form/key-value-convert"; import { convertFormValuesToObject, convertToFormValues } from "../../util"; import { MultiLineInput } from "../../components/multi-line-input/MultiLineInput"; import { toAuthorizationTab } from "../routes/AuthenticationTab"; import { ScopePicker } from "./ScopePicker"; import { KeyValueInput } from "../../components/key-value-form/KeyValueInput"; import { KeycloakTextInput } from "../../components/keycloak-text-input/KeycloakTextInput"; import "./resource-details.css"; type SubmittedResource = Omit & { attributes: KeyValueType[]; }; export default function ResourceDetails() { const { t } = useTranslation("clients"); const [client, setClient] = useState(); const [resource, setResource] = useState(); const [permissions, setPermission] = useState(); const { adminClient } = useAdminClient(); const { addAlert, addError } = useAlerts(); const form = useForm({ shouldUnregister: false, mode: "onChange", }); const { register, errors, control, setValue, handleSubmit } = form; const { id, resourceId, realm } = useParams(); const navigate = useNavigate(); const setupForm = (resource: ResourceRepresentation = {}) => { convertToFormValues(resource, setValue); }; useFetch( () => Promise.all([ adminClient.clients.findOne({ id }), resourceId ? adminClient.clients.getResource({ id, resourceId }) : Promise.resolve(undefined), resourceId ? adminClient.clients.listPermissionsByResource({ id, resourceId }) : Promise.resolve(undefined), ]), ([client, resource, permissions]) => { if (!client) { throw new Error(t("common:notFound")); } setClient(client); setPermission(permissions); setResource(resource); setupForm(resource); }, [] ); const save = async (submitted: SubmittedResource) => { const resource = convertFormValuesToObject< SubmittedResource, ResourceRepresentation >(submitted); try { if (resourceId) { await adminClient.clients.updateResource({ id, resourceId }, resource); } else { const result = await adminClient.clients.createResource( { id }, resource ); navigate(toResourceDetails({ realm, id, resourceId: result._id! })); } addAlert( t((resourceId ? "update" : "create") + "ResourceSuccess"), AlertVariant.success ); } catch (error) { addError("clients:resourceSaveError", error); } }; const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({ titleKey: "clients:deleteResource", children: ( <> {t("deleteResourceConfirm")} {permissions?.length !== 0 && (

{permissions?.map((permission) => ( {permission.name} ))}

)} ), continueButtonLabel: "clients:confirm", onConfirm: async () => { try { await adminClient.clients.delResource({ id, resourceId: resourceId!, }); addAlert(t("resourceDeletedSuccess"), AlertVariant.success); navigate(toAuthorizationTab({ realm, clientId: id, tab: "resources" })); } catch (error) { addError("clients:resourceDeletedError", error); } }, }); if (!client) { return ; } return ( <> toggleDeleteDialog()} > {t("common:delete")} , ] : undefined } /> } > } helperTextInvalid={t("common:required")} validated={ errors.name ? ValidatedOptions.error : ValidatedOptions.default } isRequired > } > } > } > } > } fieldId="ownerManagedAccess" > ( )} /> } fieldId="resourceAttribute" >
); }