2021-03-31 13:16:58 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
PageSection,
|
|
|
|
PageSectionVariants,
|
|
|
|
} from "@patternfly/react-core";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { useEffect } from "react";
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { useLocation } from "react-router-dom";
|
2021-03-16 12:37:57 +00:00
|
|
|
|
2023-05-03 13:51:02 +00:00
|
|
|
import { adminClient } from "../admin-client";
|
2021-03-16 12:37:57 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import {
|
|
|
|
AttributeForm,
|
|
|
|
AttributesForm,
|
2022-04-20 17:11:46 +00:00
|
|
|
} from "../components/key-value-form/AttributeForm";
|
2021-12-01 07:58:25 +00:00
|
|
|
import {
|
2022-04-20 17:11:46 +00:00
|
|
|
arrayToKeyValue,
|
2023-05-03 13:51:02 +00:00
|
|
|
keyValueToArray,
|
2022-04-20 17:11:46 +00:00
|
|
|
} from "../components/key-value-form/key-value-convert";
|
2021-03-16 12:37:57 +00:00
|
|
|
import { useSubGroups } from "./SubGroupsContext";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { getLastId } from "./groupIdUtils";
|
2021-03-16 12:37:57 +00:00
|
|
|
|
|
|
|
export const GroupAttributes = () => {
|
|
|
|
const { t } = useTranslation("groups");
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2021-12-01 07:58:25 +00:00
|
|
|
const form = useForm<AttributeForm>({
|
|
|
|
mode: "onChange",
|
2021-03-16 12:37:57 +00:00
|
|
|
});
|
|
|
|
|
2021-03-19 18:37:21 +00:00
|
|
|
const location = useLocation();
|
2021-03-16 12:37:57 +00:00
|
|
|
const id = getLastId(location.pathname);
|
|
|
|
const { currentGroup, subGroups, setSubGroups } = useSubGroups();
|
|
|
|
|
|
|
|
const convertAttributes = (attr?: Record<string, any>) => {
|
2022-04-20 17:11:46 +00:00
|
|
|
return arrayToKeyValue(attr || currentGroup()?.attributes!);
|
2021-03-16 12:37:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
form.setValue("attributes", convertAttributes());
|
|
|
|
}, [subGroups]);
|
|
|
|
|
|
|
|
const save = async (attributeForm: AttributeForm) => {
|
|
|
|
try {
|
|
|
|
const group = currentGroup();
|
2022-04-20 17:11:46 +00:00
|
|
|
const attributes = keyValueToArray(attributeForm.attributes!);
|
2021-03-16 12:37:57 +00:00
|
|
|
await adminClient.groups.update({ id: id! }, { ...group, attributes });
|
|
|
|
|
|
|
|
setSubGroups([
|
|
|
|
...subGroups.slice(0, subGroups.length - 1),
|
|
|
|
{ ...group, attributes },
|
|
|
|
]);
|
|
|
|
addAlert(t("groupUpdated"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("groups:groupUpdateError", error);
|
2021-03-16 12:37:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2021-03-31 13:16:58 +00:00
|
|
|
<PageSection variant={PageSectionVariants.light}>
|
|
|
|
<AttributesForm
|
|
|
|
form={form}
|
|
|
|
save={save}
|
2022-05-09 10:44:37 +00:00
|
|
|
fineGrainedAccess={currentGroup()?.access?.manage}
|
2021-03-31 13:16:58 +00:00
|
|
|
reset={() =>
|
|
|
|
form.reset({
|
|
|
|
attributes: convertAttributes(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</PageSection>
|
2021-03-16 12:37:57 +00:00
|
|
|
);
|
|
|
|
};
|