2022-02-07 11:49:47 +00:00
|
|
|
import type UserProfileConfig from "@keycloak/keycloak-admin-client/lib/defs/userProfileConfig";
|
|
|
|
import { AlertVariant } from "@patternfly/react-core";
|
2023-01-20 14:28:32 +00:00
|
|
|
import { PropsWithChildren, useState } from "react";
|
2022-02-07 11:49:47 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { createNamedContext, useRequiredContext } from "ui-shared";
|
2023-01-20 14:28:32 +00:00
|
|
|
|
2023-05-03 13:51:02 +00:00
|
|
|
import { adminClient } from "../../admin-client";
|
2022-02-07 11:49:47 +00:00
|
|
|
import { useAlerts } from "../../components/alert/Alerts";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { useFetch } from "../../context/auth/AdminClient";
|
2022-02-07 11:49:47 +00:00
|
|
|
import { useRealm } from "../../context/realm-context/RealmContext";
|
|
|
|
|
|
|
|
type UserProfileProps = {
|
|
|
|
config: UserProfileConfig | null;
|
|
|
|
save: SaveCallback;
|
|
|
|
isSaving: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type SaveCallback = (
|
|
|
|
updatedConfig: UserProfileConfig,
|
|
|
|
options?: SaveOptions
|
2022-03-01 05:44:42 +00:00
|
|
|
) => Promise<boolean>;
|
2022-02-07 11:49:47 +00:00
|
|
|
|
|
|
|
export type SaveOptions = {
|
|
|
|
successMessageKey?: string;
|
|
|
|
errorMessageKey?: string;
|
|
|
|
};
|
|
|
|
|
2022-08-03 13:15:04 +00:00
|
|
|
export const UserProfileContext = createNamedContext<
|
|
|
|
UserProfileProps | undefined
|
|
|
|
>("UserProfileContext", undefined);
|
2022-02-07 11:49:47 +00:00
|
|
|
|
2023-02-13 07:18:16 +00:00
|
|
|
export const UserProfileProvider = ({ children }: PropsWithChildren) => {
|
2022-02-07 11:49:47 +00:00
|
|
|
const { realm } = useRealm();
|
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
const { t } = useTranslation();
|
|
|
|
const [config, setConfig] = useState<UserProfileConfig | null>(null);
|
|
|
|
const [refreshCount, setRefreshCount] = useState(0);
|
|
|
|
const [isSaving, setIsSaving] = useState(false);
|
|
|
|
|
|
|
|
useFetch(
|
|
|
|
() => adminClient.users.getProfile({ realm }),
|
|
|
|
(config) => setConfig(config),
|
|
|
|
[refreshCount]
|
|
|
|
);
|
|
|
|
|
|
|
|
const save: SaveCallback = async (updatedConfig, options) => {
|
|
|
|
setIsSaving(true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.users.updateProfile({
|
|
|
|
...updatedConfig,
|
|
|
|
realm,
|
|
|
|
});
|
|
|
|
|
2022-03-01 05:44:42 +00:00
|
|
|
setIsSaving(false);
|
2022-02-07 11:49:47 +00:00
|
|
|
setRefreshCount(refreshCount + 1);
|
|
|
|
addAlert(
|
|
|
|
t(options?.successMessageKey ?? "realm-settings:userProfileSuccess"),
|
|
|
|
AlertVariant.success
|
|
|
|
);
|
2022-03-01 05:44:42 +00:00
|
|
|
|
|
|
|
return true;
|
2022-02-07 11:49:47 +00:00
|
|
|
} catch (error) {
|
2022-03-01 05:44:42 +00:00
|
|
|
setIsSaving(false);
|
2022-02-07 11:49:47 +00:00
|
|
|
addError(
|
|
|
|
options?.errorMessageKey ?? "realm-settings:userProfileError",
|
|
|
|
error
|
|
|
|
);
|
|
|
|
|
2022-03-01 05:44:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-02-07 11:49:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-08-03 13:15:04 +00:00
|
|
|
<UserProfileContext.Provider value={{ config, save, isSaving }}>
|
2022-02-07 11:49:47 +00:00
|
|
|
{children}
|
2022-08-03 13:15:04 +00:00
|
|
|
</UserProfileContext.Provider>
|
2022-02-07 11:49:47 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-08-03 13:15:04 +00:00
|
|
|
export const useUserProfile = () => useRequiredContext(UserProfileContext);
|