2023-10-06 13:15:39 +00:00
|
|
|
import type { UserProfileConfig } from "@keycloak/keycloak-admin-client/lib/defs/userProfileConfig";
|
2022-02-07 11:49:47 +00:00
|
|
|
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";
|
|
|
|
import { useRealm } from "../../context/realm-context/RealmContext";
|
2023-05-03 15:40:27 +00:00
|
|
|
import { useFetch } from "../../utils/useFetch";
|
2022-02-07 11:49:47 +00:00
|
|
|
|
|
|
|
type UserProfileProps = {
|
|
|
|
config: UserProfileConfig | null;
|
|
|
|
save: SaveCallback;
|
|
|
|
isSaving: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type SaveCallback = (
|
|
|
|
updatedConfig: UserProfileConfig,
|
2023-07-11 14:03:21 +00:00
|
|
|
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),
|
2023-07-11 14:03:21 +00:00
|
|
|
[refreshCount],
|
2022-02-07 11:49:47 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
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(
|
2023-09-25 07:06:56 +00:00
|
|
|
t(options?.successMessageKey ?? "userProfileSuccess"),
|
2023-07-11 14:03:21 +00:00
|
|
|
AlertVariant.success,
|
2022-02-07 11:49:47 +00:00
|
|
|
);
|
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);
|
2023-09-25 07:06:56 +00:00
|
|
|
addError(options?.errorMessageKey ?? "userProfileError", error);
|
2022-02-07 11:49:47 +00:00
|
|
|
|
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);
|