2022-01-03 15:00:03 +00:00
|
|
|
import type UserProfileConfig from "@keycloak/keycloak-admin-client/lib/defs/userProfileConfig";
|
2022-01-19 09:41:52 +00:00
|
|
|
import { AlertVariant, Tab, TabTitleText } from "@patternfly/react-core";
|
2022-01-03 15:00:03 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2022-01-19 09:41:52 +00:00
|
|
|
import { useHistory } from "react-router-dom";
|
2022-01-03 15:00:03 +00:00
|
|
|
import { useAlerts } from "../../components/alert/Alerts";
|
2022-01-19 09:41:52 +00:00
|
|
|
import {
|
|
|
|
routableTab,
|
|
|
|
RoutableTabs,
|
|
|
|
} from "../../components/routable-tabs/RoutableTabs";
|
2022-01-03 15:00:03 +00:00
|
|
|
import { useAdminClient, useFetch } from "../../context/auth/AdminClient";
|
|
|
|
import { useRealm } from "../../context/realm-context/RealmContext";
|
2022-01-19 09:41:52 +00:00
|
|
|
import { toUserProfile } from "../routes/UserProfile";
|
2022-01-07 12:56:27 +00:00
|
|
|
import { AttributesGroupTab } from "./AttributesGroupTab";
|
2022-01-03 15:00:03 +00:00
|
|
|
import { JsonEditorTab } from "./JsonEditorTab";
|
|
|
|
|
2022-01-07 12:56:27 +00:00
|
|
|
export type OnSaveCallback = (
|
|
|
|
updatedProfiles: UserProfileConfig,
|
|
|
|
options?: OnSaveOptions
|
|
|
|
) => Promise<void>;
|
|
|
|
|
|
|
|
export type OnSaveOptions = {
|
|
|
|
successMessageKey?: string;
|
|
|
|
errorMessageKey?: string;
|
|
|
|
};
|
|
|
|
|
2022-01-03 15:00:03 +00:00
|
|
|
export const UserProfileTab = () => {
|
|
|
|
const adminClient = useAdminClient();
|
|
|
|
const { realm } = useRealm();
|
|
|
|
const { t } = useTranslation("realm-settings");
|
2022-01-19 09:41:52 +00:00
|
|
|
const history = useHistory();
|
2022-01-03 15:00:03 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
const [config, setConfig] = useState<UserProfileConfig>();
|
|
|
|
const [isSaving, setIsSaving] = useState(false);
|
|
|
|
const [refreshCount, setRefreshCount] = useState(0);
|
|
|
|
|
|
|
|
useFetch(
|
|
|
|
() => adminClient.users.getProfile({ realm }),
|
|
|
|
(config) => setConfig(config),
|
|
|
|
[refreshCount]
|
|
|
|
);
|
|
|
|
|
2022-01-07 12:56:27 +00:00
|
|
|
const onSave: OnSaveCallback = async (
|
|
|
|
updatedProfiles: UserProfileConfig,
|
|
|
|
options?: OnSaveOptions
|
|
|
|
) => {
|
2022-01-03 15:00:03 +00:00
|
|
|
setIsSaving(true);
|
|
|
|
|
|
|
|
try {
|
2022-01-07 12:56:27 +00:00
|
|
|
await adminClient.users.updateProfile({
|
2022-01-03 15:00:03 +00:00
|
|
|
...updatedProfiles,
|
|
|
|
realm,
|
|
|
|
});
|
|
|
|
|
|
|
|
setRefreshCount(refreshCount + 1);
|
2022-01-07 12:56:27 +00:00
|
|
|
addAlert(
|
|
|
|
t(options?.successMessageKey ?? "userProfileSuccess"),
|
|
|
|
AlertVariant.success
|
|
|
|
);
|
2022-01-03 15:00:03 +00:00
|
|
|
} catch (error) {
|
2022-01-07 12:56:27 +00:00
|
|
|
addError(
|
|
|
|
options?.errorMessageKey ?? "realm-settings:userProfileError",
|
|
|
|
error
|
|
|
|
);
|
2022-01-03 15:00:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setIsSaving(false);
|
2022-01-07 12:56:27 +00:00
|
|
|
};
|
2022-01-03 15:00:03 +00:00
|
|
|
|
|
|
|
return (
|
2022-01-19 09:41:52 +00:00
|
|
|
<RoutableTabs
|
|
|
|
defaultLocation={toUserProfile({ realm, tab: "attributes" })}
|
2022-01-03 15:00:03 +00:00
|
|
|
mountOnEnter
|
|
|
|
>
|
|
|
|
<Tab
|
|
|
|
title={<TabTitleText>{t("attributes")}</TabTitleText>}
|
2022-01-19 09:41:52 +00:00
|
|
|
{...routableTab({
|
|
|
|
to: toUserProfile({ realm, tab: "attributes" }),
|
|
|
|
history,
|
|
|
|
})}
|
2022-01-03 15:00:03 +00:00
|
|
|
></Tab>
|
|
|
|
<Tab
|
|
|
|
title={<TabTitleText>{t("attributesGroup")}</TabTitleText>}
|
2022-01-07 12:56:27 +00:00
|
|
|
data-testid="attributesGroupTab"
|
2022-01-19 09:41:52 +00:00
|
|
|
{...routableTab({
|
|
|
|
to: toUserProfile({ realm, tab: "attributesGroup" }),
|
|
|
|
history,
|
|
|
|
})}
|
2022-01-07 12:56:27 +00:00
|
|
|
>
|
|
|
|
<AttributesGroupTab config={config} onSave={onSave} />
|
|
|
|
</Tab>
|
2022-01-03 15:00:03 +00:00
|
|
|
<Tab
|
|
|
|
title={<TabTitleText>{t("jsonEditor")}</TabTitleText>}
|
2022-01-19 09:41:52 +00:00
|
|
|
{...routableTab({
|
|
|
|
to: toUserProfile({ realm, tab: "jsonEditor" }),
|
|
|
|
history,
|
|
|
|
})}
|
2022-01-03 15:00:03 +00:00
|
|
|
>
|
|
|
|
<JsonEditorTab config={config} onSave={onSave} isSaving={isSaving} />
|
|
|
|
</Tab>
|
2022-01-19 09:41:52 +00:00
|
|
|
</RoutableTabs>
|
2022-01-03 15:00:03 +00:00
|
|
|
);
|
|
|
|
};
|