no longer use subgroup cache (#21783)

* no longer use subgroup cache

fixes: #21693

* pr review
This commit is contained in:
Erik Jan de Wit 2023-08-09 13:26:47 +02:00 committed by GitHub
parent 33aab79d9d
commit 66262d0055
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,9 +1,10 @@
import GroupRepresentation from "@keycloak/keycloak-admin-client/lib/defs/groupRepresentation";
import { import {
AlertVariant, AlertVariant,
PageSection, PageSection,
PageSectionVariants, PageSectionVariants,
} from "@patternfly/react-core"; } from "@patternfly/react-core";
import { useEffect } from "react"; import { useState } from "react";
import { useForm } from "react-hook-form"; import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useLocation } from "react-router-dom"; import { useLocation } from "react-router-dom";
@ -14,11 +15,9 @@ import {
AttributeForm, AttributeForm,
AttributesForm, AttributesForm,
} from "../components/key-value-form/AttributeForm"; } from "../components/key-value-form/AttributeForm";
import { import { arrayToKeyValue } from "../components/key-value-form/key-value-convert";
arrayToKeyValue, import { convertFormValuesToObject, convertToFormValues } from "../util";
keyValueToArray, import { useFetch } from "../utils/useFetch";
} from "../components/key-value-form/key-value-convert";
import { useSubGroups } from "./SubGroupsContext";
import { getLastId } from "./groupIdUtils"; import { getLastId } from "./groupIdUtils";
export const GroupAttributes = () => { export const GroupAttributes = () => {
@ -29,27 +28,27 @@ export const GroupAttributes = () => {
}); });
const location = useLocation(); const location = useLocation();
const id = getLastId(location.pathname); const id = getLastId(location.pathname)!;
const { currentGroup, subGroups, setSubGroups } = useSubGroups(); const [currentGroup, setCurrentGroup] = useState<GroupRepresentation>();
const convertAttributes = (attr?: Record<string, any>) => { useFetch(
return arrayToKeyValue(attr || currentGroup()?.attributes!); () => adminClient.groups.findOne({ id }),
}; (group) => {
convertToFormValues(group!, form.setValue);
useEffect(() => { setCurrentGroup(group);
form.setValue("attributes", convertAttributes()); },
}, [subGroups]); [],
);
const save = async (attributeForm: AttributeForm) => { const save = async (attributeForm: AttributeForm) => {
try { try {
const group = currentGroup(); const attributes = convertFormValuesToObject(attributeForm).attributes;
const attributes = keyValueToArray(attributeForm.attributes!); await adminClient.groups.update(
await adminClient.groups.update({ id: id! }, { ...group, attributes }); { id: id! },
{ ...currentGroup, attributes },
);
setSubGroups([ setCurrentGroup({ ...currentGroup, attributes });
...subGroups.slice(0, subGroups.length - 1),
{ ...group, attributes },
]);
addAlert(t("groupUpdated"), AlertVariant.success); addAlert(t("groupUpdated"), AlertVariant.success);
} catch (error) { } catch (error) {
addError("groups:groupUpdateError", error); addError("groups:groupUpdateError", error);
@ -61,10 +60,10 @@ export const GroupAttributes = () => {
<AttributesForm <AttributesForm
form={form} form={form}
save={save} save={save}
fineGrainedAccess={currentGroup()?.access?.manage} fineGrainedAccess={currentGroup?.access?.manage}
reset={() => reset={() =>
form.reset({ form.reset({
attributes: convertAttributes(), attributes: arrayToKeyValue(currentGroup?.attributes!),
}) })
} }
/> />