keycloak-scim/src/groups/GroupAttributes.tsx
Erik Jan de Wit 0bbd4ddad1
Initial version of the create and edit resouce screen (#1573)
* Initial version of the create and edit resouce screen

* refactored and fixed the attributes form

introduced a new component that can be used more easily

* Update src/components/json-file-upload/FileUploadForm.tsx

Co-authored-by: Jon Koops <jonkoops@gmail.com>

* Update src/components/attribute-form/attribute-convert.ts

Co-authored-by: Jon Koops <jonkoops@gmail.com>

* Update src/clients/authorization/ResourceDetails.tsx

Co-authored-by: Jon Koops <jonkoops@gmail.com>

* Update src/clients/authorization/ResourceDetails.tsx

Co-authored-by: Jon Koops <jonkoops@gmail.com>

* Update src/clients/authorization/ResourceDetails.tsx

Co-authored-by: Jon Koops <jonkoops@gmail.com>

* PR review

* fixed tests

* PR review comments

* resourceId is optional

* Update src/components/attribute-form/attribute-convert.ts

Co-authored-by: Jon Koops <jonkoops@gmail.com>

Co-authored-by: Jon Koops <jonkoops@gmail.com>
2021-12-01 08:58:25 +01:00

75 lines
2.1 KiB
TypeScript

import React, { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { useForm } from "react-hook-form";
import {
AlertVariant,
PageSection,
PageSectionVariants,
} from "@patternfly/react-core";
import { useAlerts } from "../components/alert/Alerts";
import {
AttributeForm,
AttributesForm,
} from "../components/attribute-form/AttributeForm";
import {
arrayToAttributes,
attributesToArray,
} from "../components/attribute-form/attribute-convert";
import { useAdminClient } from "../context/auth/AdminClient";
import { getLastId } from "./groupIdUtils";
import { useSubGroups } from "./SubGroupsContext";
import { useLocation } from "react-router-dom";
export const GroupAttributes = () => {
const { t } = useTranslation("groups");
const adminClient = useAdminClient();
const { addAlert, addError } = useAlerts();
const form = useForm<AttributeForm>({
mode: "onChange",
shouldUnregister: false,
});
const location = useLocation();
const id = getLastId(location.pathname);
const { currentGroup, subGroups, setSubGroups } = useSubGroups();
const convertAttributes = (attr?: Record<string, any>) => {
return attributesToArray(attr || currentGroup().attributes!);
};
useEffect(() => {
form.setValue("attributes", convertAttributes());
}, [subGroups]);
const save = async (attributeForm: AttributeForm) => {
try {
const group = currentGroup();
const attributes = arrayToAttributes(attributeForm.attributes!);
await adminClient.groups.update({ id: id! }, { ...group, attributes });
setSubGroups([
...subGroups.slice(0, subGroups.length - 1),
{ ...group, attributes },
]);
addAlert(t("groupUpdated"), AlertVariant.success);
} catch (error) {
addError("groups:groupUpdateError", error);
}
};
return (
<PageSection variant={PageSectionVariants.light}>
<AttributesForm
form={form}
save={save}
reset={() =>
form.reset({
attributes: convertAttributes(),
})
}
/>
</PageSection>
);
};