Re-use toTabs() function in RealmRoleTabs component (#4197)

This commit is contained in:
Jon Koops 2023-01-16 08:18:11 +01:00 committed by GitHub
parent 4d44265ed0
commit b13bcdc020
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -12,7 +12,6 @@ import { omit } from "lodash-es";
import { useState } from "react"; import { useState } from "react";
import { SubmitHandler, useForm } from "react-hook-form"; import { SubmitHandler, useForm } from "react-hook-form";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useRouteMatch } from "react-router-dom";
import { useLocation, useMatch, useNavigate } from "react-router-dom-v5-compat"; import { useLocation, useMatch, useNavigate } from "react-router-dom-v5-compat";
import { toClient } from "../clients/routes/Client"; import { toClient } from "../clients/routes/Client";
@ -169,6 +168,36 @@ export default function RealmRoleTabs() {
throw new Error("Roles overview route could not be determined."); throw new Error("Roles overview route could not be determined.");
}; };
const toTab = (tab: RealmRoleTab | ClientRoleTab) => {
if (realmRoleMatch) {
return toRealmRole({
realm: realmName,
id,
tab,
});
}
if (clientRoleMatch) {
return toClientRole({
realm: realmName,
id,
clientId: clientRoleMatch.params.clientId!,
tab: tab as ClientRoleTab,
});
}
throw new Error("Route could not be determined.");
};
const useTab = (tab: RealmRoleTab | ClientRoleTab) =>
useRoutableTab(toTab(tab));
const detailsTab = useTab("details");
const associatedRolesTab = useTab("associated-roles");
const attributesTab = useTab("attributes");
const usersInRoleTab = useTab("users-in-role");
const permissionsTab = useTab("permissions");
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({ const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
titleKey: "roles:roleDeleteConfirm", titleKey: "roles:roleDeleteConfirm",
messageKey: t("roles:roleDeleteConfirmDialog", { messageKey: t("roles:roleDeleteConfirmDialog", {
@ -252,7 +281,7 @@ export default function RealmRoleTabs() {
AlertVariant.success, AlertVariant.success,
t("compositesRemovedAlertDescription") t("compositesRemovedAlertDescription")
); );
toDetail(); navigate(toTab("details"));
refresh(); refresh();
} catch (error) { } catch (error) {
addError("roles:roleDeleteError", error); addError("roles:roleDeleteError", error);
@ -264,59 +293,6 @@ export default function RealmRoleTabs() {
setOpen(!open); setOpen(!open);
}; };
const clientRoleRouteMatch = useRouteMatch<ClientRoleParams>(
ClientRoleRoute.path
);
const toDetail = () => {
const to = clientRoleRouteMatch
? toClientRole({
...clientRoleRouteMatch.params,
tab: "details",
})
: toRealmRole({
realm: realm?.realm!,
id,
tab: "details",
});
navigate(to);
};
const toAssociatedRoles = () => {
const to = clientRoleRouteMatch
? toClientRole({
...clientRoleRouteMatch.params,
tab: "associated-roles",
})
: toRealmRole({
realm: realm?.realm!,
id,
tab: "associated-roles",
});
navigate(to);
};
const toTab = (tab: RealmRoleTab | ClientRoleTab) =>
clientRoleRouteMatch
? toClientRole({
...clientRoleRouteMatch.params,
tab: tab as ClientRoleTab,
})
: toRealmRole({
realm: realmName,
id,
tab,
});
const useTab = (tab: RealmRoleTab | ClientRoleTab) =>
useRoutableTab(toTab(tab));
const detailsTab = useTab("details");
const associatedRolesTab = useTab("associated-roles");
const attributesTab = useTab("attributes");
const usersInRoleTab = useTab("users-in-role");
const permissionsTab = useTab("permissions");
const addComposites = async (composites: RoleRepresentation[]) => { const addComposites = async (composites: RoleRepresentation[]) => {
try { try {
await adminClient.roles.createComposite( await adminClient.roles.createComposite(
@ -324,7 +300,7 @@ export default function RealmRoleTabs() {
composites composites
); );
refresh(); refresh();
toAssociatedRoles(); navigate(toTab("associated-roles"));
addAlert(t("addAssociatedRolesSuccess"), AlertVariant.success); addAlert(t("addAssociatedRolesSuccess"), AlertVariant.success);
} catch (error) { } catch (error) {
addError("roles:addAssociatedRolesError", error); addError("roles:addAssociatedRolesError", error);
@ -372,9 +348,9 @@ export default function RealmRoleTabs() {
<RoleForm <RoleForm
form={form} form={form}
onSubmit={onSubmit} onSubmit={onSubmit}
role={clientRoleRouteMatch ? "manage-clients" : "manage-realm"} role={clientRoleMatch ? "manage-clients" : "manage-realm"}
cancelLink={ cancelLink={
clientRoleRouteMatch clientRoleMatch
? toClient({ realm: realmName, clientId, tab: "roles" }) ? toClient({ realm: realmName, clientId, tab: "roles" })
: toRealmRoles({ realm: realmName }) : toRealmRoles({ realm: realmName })
} }