import React, { useState } from "react"; import { Link, useHistory, useParams } from "react-router-dom"; import { useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { ActionGroup, AlertVariant, Button, ButtonVariant, DropdownItem, FormGroup, PageSection, TextInput, ValidatedOptions, } from "@patternfly/react-core"; import type ScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/scopeRepresentation"; import type { ScopeDetailsParams } from "../routes/Scope"; import { FormAccess } from "../../components/form-access/FormAccess"; import { HelpItem } from "../../components/help-enabler/HelpItem"; import { ViewHeader } from "../../components/view-header/ViewHeader"; import { toClient } from "../routes/Client"; import { useAdminClient, useFetch } from "../../context/auth/AdminClient"; import { useAlerts } from "../../components/alert/Alerts"; import useToggle from "../../utils/useToggle"; import { DeleteScopeDialog } from "./DeleteScopeDialog"; export default function ScopeDetails() { const { t } = useTranslation("clients"); const { id, scopeId, realm } = useParams(); const history = useHistory(); const adminClient = useAdminClient(); const { addAlert, addError } = useAlerts(); const [deleteDialog, toggleDeleteDialog] = useToggle(); const [scope, setScope] = useState(); const { register, errors, reset, handleSubmit } = 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 save = 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, } ); history.push(toClient({ realm, clientId: id, tab: "authorization" })); } addAlert( t((scopeId ? "update" : "create") + "ScopeSuccess"), AlertVariant.success ); } catch (error) { addError("clients:scopeSaveError", error); } }; return ( <> history.push(toClient({ realm, clientId: id, tab: "authorization" })) } /> toggleDeleteDialog()} > {t("common:delete")} , ] : undefined } /> } helperTextInvalid={t("common:required")} validated={ errors.name ? ValidatedOptions.error : ValidatedOptions.default } isRequired > } > } >
{!scope ? ( ) : ( )}
); }