Keep realm value from RealmContext in sync with the URL (#1130)
This commit is contained in:
parent
3fcf5c44d0
commit
b5e5677cb5
6 changed files with 16 additions and 23 deletions
|
@ -47,7 +47,6 @@ export const MockAdminClient: FunctionComponent<{ mock?: object }> = (
|
||||||
<RealmContext.Provider
|
<RealmContext.Provider
|
||||||
value={{
|
value={{
|
||||||
realm: "master",
|
realm: "master",
|
||||||
setRealm: () => {},
|
|
||||||
realms: [],
|
realms: [],
|
||||||
refresh: () => Promise.resolve(),
|
refresh: () => Promise.resolve(),
|
||||||
}}
|
}}
|
||||||
|
|
|
@ -22,7 +22,6 @@ describe("FormAccess", () => {
|
||||||
<RealmContext.Provider
|
<RealmContext.Provider
|
||||||
value={{
|
value={{
|
||||||
realm,
|
realm,
|
||||||
setRealm: () => {},
|
|
||||||
realms: [],
|
realms: [],
|
||||||
refresh: () => Promise.resolve(),
|
refresh: () => Promise.resolve(),
|
||||||
}}
|
}}
|
||||||
|
|
|
@ -17,6 +17,7 @@ import { useHistory } from "react-router-dom";
|
||||||
|
|
||||||
import { useRealm } from "../../context/realm-context/RealmContext";
|
import { useRealm } from "../../context/realm-context/RealmContext";
|
||||||
import { useWhoAmI } from "../../context/whoami/WhoAmI";
|
import { useWhoAmI } from "../../context/whoami/WhoAmI";
|
||||||
|
import { toDashboard } from "../../dashboard/routes/Dashboard";
|
||||||
import { toAddRealm } from "../../realm/routes/AddRealm";
|
import { toAddRealm } from "../../realm/routes/AddRealm";
|
||||||
import { toUpperCase } from "../../util";
|
import { toUpperCase } from "../../util";
|
||||||
import { RecentUsed } from "./recent-used";
|
import { RecentUsed } from "./recent-used";
|
||||||
|
@ -24,7 +25,7 @@ import { RecentUsed } from "./recent-used";
|
||||||
import "./realm-selector.css";
|
import "./realm-selector.css";
|
||||||
|
|
||||||
export const RealmSelector = () => {
|
export const RealmSelector = () => {
|
||||||
const { realm, setRealm, realms } = useRealm();
|
const { realm, realms } = useRealm();
|
||||||
const { whoAmI } = useWhoAmI();
|
const { whoAmI } = useWhoAmI();
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
const [search, setSearch] = useState("");
|
const [search, setSearch] = useState("");
|
||||||
|
@ -76,9 +77,8 @@ export const RealmSelector = () => {
|
||||||
);
|
);
|
||||||
|
|
||||||
const selectRealm = (realm: string) => {
|
const selectRealm = (realm: string) => {
|
||||||
setRealm(realm);
|
|
||||||
setOpen(!open);
|
setOpen(!open);
|
||||||
history.push(`/${realm}/`);
|
history.push(toDashboard({ realm }));
|
||||||
};
|
};
|
||||||
|
|
||||||
const dropdownItems = realms.map((r) => (
|
const dropdownItems = realms.map((r) => (
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation";
|
import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation";
|
||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
import React, { FunctionComponent, useEffect, useState } from "react";
|
import React, { FunctionComponent, useEffect, useMemo, useState } from "react";
|
||||||
import { useRouteMatch } from "react-router-dom";
|
import { useRouteMatch } from "react-router-dom";
|
||||||
import { RecentUsed } from "../../components/realm-selector/recent-used";
|
import { RecentUsed } from "../../components/realm-selector/recent-used";
|
||||||
import {
|
import {
|
||||||
|
@ -13,7 +13,6 @@ import { useAdminClient, useFetch } from "../auth/AdminClient";
|
||||||
|
|
||||||
type RealmContextType = {
|
type RealmContextType = {
|
||||||
realm: string;
|
realm: string;
|
||||||
setRealm: (realm: string) => void;
|
|
||||||
realms: RealmRepresentation[];
|
realms: RealmRepresentation[];
|
||||||
refresh: () => Promise<void>;
|
refresh: () => Promise<void>;
|
||||||
};
|
};
|
||||||
|
@ -24,9 +23,12 @@ export const RealmContext = React.createContext<RealmContextType | undefined>(
|
||||||
|
|
||||||
export const RealmContextProvider: FunctionComponent = ({ children }) => {
|
export const RealmContextProvider: FunctionComponent = ({ children }) => {
|
||||||
const routeMatch = useRouteMatch<DashboardParams>(DashboardRoute.path);
|
const routeMatch = useRouteMatch<DashboardParams>(DashboardRoute.path);
|
||||||
const [realm, setRealm] = useState(
|
const realmParam = routeMatch?.params.realm;
|
||||||
routeMatch?.params.realm ?? environment.loginRealm
|
const realm = useMemo(
|
||||||
|
() => realmParam ?? environment.loginRealm,
|
||||||
|
[realmParam]
|
||||||
);
|
);
|
||||||
|
|
||||||
const [realms, setRealms] = useState<RealmRepresentation[]>([]);
|
const [realms, setRealms] = useState<RealmRepresentation[]>([]);
|
||||||
const adminClient = useAdminClient();
|
const adminClient = useAdminClient();
|
||||||
const recentUsed = new RecentUsed();
|
const recentUsed = new RecentUsed();
|
||||||
|
@ -42,18 +44,16 @@ export const RealmContextProvider: FunctionComponent = ({ children }) => {
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Configure admin client to use selected realm when it changes.
|
||||||
useEffect(() => adminClient.setConfig({ realmName: realm }), [realm]);
|
useEffect(() => adminClient.setConfig({ realmName: realm }), [realm]);
|
||||||
|
|
||||||
const set = (realm: string) => {
|
// Keep track of recently used realms when selected realm changes.
|
||||||
recentUsed.setRecentUsed(realm);
|
useEffect(() => recentUsed.setRecentUsed(realm), [realm]);
|
||||||
setRealm(realm);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<RealmContext.Provider
|
<RealmContext.Provider
|
||||||
value={{
|
value={{
|
||||||
realm,
|
realm,
|
||||||
setRealm: set,
|
|
||||||
realms,
|
realms,
|
||||||
refresh: async () => {
|
refresh: async () => {
|
||||||
//this is needed otherwise the realm find function will not return
|
//this is needed otherwise the realm find function will not return
|
||||||
|
|
|
@ -147,11 +147,7 @@ export const RealmSettingsTabs = ({
|
||||||
const { t } = useTranslation("realm-settings");
|
const { t } = useTranslation("realm-settings");
|
||||||
const adminClient = useAdminClient();
|
const adminClient = useAdminClient();
|
||||||
const { addAlert, addError } = useAlerts();
|
const { addAlert, addError } = useAlerts();
|
||||||
const {
|
const { realm: realmName, refresh: refreshRealm } = useRealm();
|
||||||
realm: realmName,
|
|
||||||
refresh: refreshRealm,
|
|
||||||
setRealm: setCurrentRealm,
|
|
||||||
} = useRealm();
|
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
|
|
||||||
const kpComponentTypes =
|
const kpComponentTypes =
|
||||||
|
@ -197,7 +193,6 @@ export const RealmSettingsTabs = ({
|
||||||
const isRealmRenamed = realmName !== realm.realm;
|
const isRealmRenamed = realmName !== realm.realm;
|
||||||
if (isRealmRenamed) {
|
if (isRealmRenamed) {
|
||||||
await refreshRealm();
|
await refreshRealm();
|
||||||
setCurrentRealm(realm.realm!);
|
|
||||||
history.push(toRealmSettings({ realm: realm.realm! }));
|
history.push(toRealmSettings({ realm: realm.realm! }));
|
||||||
}
|
}
|
||||||
addAlert(t("saveSuccess"), AlertVariant.success);
|
addAlert(t("saveSuccess"), AlertVariant.success);
|
||||||
|
|
|
@ -19,12 +19,13 @@ import { ViewHeader } from "../../components/view-header/ViewHeader";
|
||||||
import { useAdminClient } from "../../context/auth/AdminClient";
|
import { useAdminClient } from "../../context/auth/AdminClient";
|
||||||
import { useRealm } from "../../context/realm-context/RealmContext";
|
import { useRealm } from "../../context/realm-context/RealmContext";
|
||||||
import { useWhoAmI } from "../../context/whoami/WhoAmI";
|
import { useWhoAmI } from "../../context/whoami/WhoAmI";
|
||||||
|
import { toDashboard } from "../../dashboard/routes/Dashboard";
|
||||||
|
|
||||||
export const NewRealmForm = () => {
|
export const NewRealmForm = () => {
|
||||||
const { t } = useTranslation("realm");
|
const { t } = useTranslation("realm");
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
const { refresh } = useWhoAmI();
|
const { refresh } = useWhoAmI();
|
||||||
const { refresh: realmRefresh, setRealm } = useRealm();
|
const { refresh: realmRefresh } = useRealm();
|
||||||
const adminClient = useAdminClient();
|
const adminClient = useAdminClient();
|
||||||
const { addAlert, addError } = useAlerts();
|
const { addAlert, addError } = useAlerts();
|
||||||
|
|
||||||
|
@ -45,8 +46,7 @@ export const NewRealmForm = () => {
|
||||||
|
|
||||||
refresh();
|
refresh();
|
||||||
await realmRefresh();
|
await realmRefresh();
|
||||||
setRealm(realm.realm!);
|
history.push(toDashboard({ realm: realm.realm }));
|
||||||
history.push(`/${realm.realm}`);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
addError("realm:saveRealmError", error);
|
addError("realm:saveRealmError", error);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue