UNPKG

@keycloakify/keycloak-admin-ui

Version:
40 lines 2.01 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { createNamedContext, useAlerts, useFetch, useRequiredContext, } from "../../ui-shared"; import { AlertVariant } from "@patternfly/react-core"; import { useState } from "react"; import { useTranslation } from "react-i18next"; import { useAdminClient } from "../../admin-client"; import { useRealm } from "../../context/realm-context/RealmContext"; export const UserProfileContext = createNamedContext("UserProfileContext", undefined); export const UserProfileProvider = ({ children }) => { const { adminClient } = useAdminClient(); const { realm } = useRealm(); const { addAlert, addError } = useAlerts(); const { t } = useTranslation(); const [config, setConfig] = useState(null); const [refreshCount, setRefreshCount] = useState(0); const [isSaving, setIsSaving] = useState(false); useFetch(() => adminClient.users.getProfile({ realm }), (config) => setConfig(config), [refreshCount]); const save = async (updatedConfig, options) => { var _a, _b; setIsSaving(true); try { await adminClient.users.updateProfile({ ...updatedConfig, realm, }); setIsSaving(false); setRefreshCount(refreshCount + 1); addAlert(t((_a = options === null || options === void 0 ? void 0 : options.successMessageKey) !== null && _a !== void 0 ? _a : "userProfileSuccess"), AlertVariant.success); return true; } catch (error) { setIsSaving(false); addError((_b = options === null || options === void 0 ? void 0 : options.errorMessageKey) !== null && _b !== void 0 ? _b : "userProfileError", error); return false; } }; return (_jsx(UserProfileContext.Provider, { value: { config, save, isSaving }, children: children })); }; export const useUserProfile = () => useRequiredContext(UserProfileContext); //# sourceMappingURL=UserProfileContext.js.map