UNPKG

@keycloakify/keycloak-admin-ui

Version:
33 lines 1.79 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { CodeEditor, Language } from "@patternfly/react-code-editor"; import { ActionGroup, Button, Form, PageSection } from "@patternfly/react-core"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { useAlerts } from "../../ui-shared"; import { prettyPrintJSON } from "../../util"; import { useUserProfile } from "../../realm-settings/user-profile/UserProfileContext"; export const JsonEditorTab = () => { const { config, save, isSaving } = useUserProfile(); const { t } = useTranslation(); const { addError } = useAlerts(); const [editor, setEditor] = useState(); useEffect(() => resetCode(), [config, editor]); function resetCode() { editor === null || editor === void 0 ? void 0 : editor.setValue(config ? prettyPrintJSON(config) : ""); } async function handleSave() { const value = editor === null || editor === void 0 ? void 0 : editor.getValue(); if (!value) { return; } try { await save(JSON.parse(value)); } catch (error) { addError("invalidJsonError", error); return; } } return (_jsxs(PageSection, { variant: "light", children: [_jsx(CodeEditor, { language: Language.json, height: "30rem", onEditorDidMount: (editor) => setEditor(editor), isLanguageLabelVisible: true }), _jsx(Form, { children: _jsxs(ActionGroup, { children: [_jsx(Button, { "data-testid": "save", variant: "primary", onClick: handleSave, isDisabled: isSaving, children: t("save") }), _jsx(Button, { variant: "link", onClick: resetCode, isDisabled: isSaving, children: t("revert") })] }) })] })); }; //# sourceMappingURL=JsonEditorTab.js.map