import type ScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/scopeRepresentation"; import { ActionGroup, AlertVariant, Button, ButtonVariant, DropdownItem, FormGroup, PageSection, ValidatedOptions, } from "@patternfly/react-core"; import { useState } from "react"; import { useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { Link, useNavigate } from "react-router-dom"; import { useAlerts } from "../../components/alert/Alerts"; import { FormAccess } from "../../components/form-access/FormAccess"; import { HelpItem } from "ui-shared"; import { KeycloakTextInput } from "../../components/keycloak-text-input/KeycloakTextInput"; import { ViewHeader } from "../../components/view-header/ViewHeader"; import { useAdminClient, useFetch } from "../../context/auth/AdminClient"; import { useParams } from "../../utils/useParams"; import useToggle from "../../utils/useToggle"; import { toAuthorizationTab } from "../routes/AuthenticationTab"; import type { ScopeDetailsParams } from "../routes/Scope"; import { DeleteScopeDialog } from "./DeleteScopeDialog"; type FormFields = Omit; export default function ScopeDetails() { const { t } = useTranslation("clients"); const { id, scopeId, realm } = useParams(); const navigate = useNavigate(); const { adminClient } = useAdminClient(); const { addAlert, addError } = useAlerts(); const [deleteDialog, toggleDeleteDialog] = useToggle(); const [scope, setScope] = useState(); const { register, reset, handleSubmit, formState: { errors }, } = useForm({ mode: "onChange", }); useFetch( async () => { if (scopeId) { const scope = await adminClient.clients.getAuthorizationScope({ id, scopeId, }); if (!scope) { throw new Error(t("common:notFound")); } return scope; } }, (scope) => { setScope(scope); reset({ ...scope }); }, [] ); const onSubmit = async (scope: ScopeRepresentation) => { try { if (scopeId) { await adminClient.clients.updateAuthorizationScope( { id, scopeId }, scope ); setScope(scope); } else { await adminClient.clients.createAuthorizationScope( { id }, { name: scope.name!, displayName: scope.displayName, iconUri: scope.iconUri, } ); navigate(toAuthorizationTab({ realm, clientId: id, tab: "scopes" })); } addAlert( t((scopeId ? "update" : "create") + "ScopeSuccess"), AlertVariant.success ); } catch (error) { addError("clients:scopeSaveError", error); } }; return ( <> navigate(toAuthorizationTab({ realm, clientId: id, tab: "scopes" })) } /> toggleDeleteDialog()} > {t("common:delete")} , ] : undefined } /> } helperTextInvalid={t("common:required")} validated={ errors.name ? ValidatedOptions.error : ValidatedOptions.default } isRequired > } > } >
{!scope ? ( ) : ( )}
); }