2023-01-04 11:19:09 +00:00
|
|
|
import { AlertVariant, PageSection } from "@patternfly/react-core";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2023-01-18 12:09:49 +00:00
|
|
|
import { useNavigate } from "react-router-dom";
|
2023-01-04 11:19:09 +00:00
|
|
|
|
2023-05-03 13:51:02 +00:00
|
|
|
import { adminClient } from "../admin-client";
|
2023-01-04 11:19:09 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import {
|
|
|
|
ClientScopeDefaultOptionalType,
|
2023-05-03 13:51:02 +00:00
|
|
|
changeScope,
|
2023-01-04 11:19:09 +00:00
|
|
|
} from "../components/client-scope/ClientScopeTypes";
|
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
|
|
|
import { convertFormValuesToObject } from "../util";
|
|
|
|
import { ScopeForm } from "./details/ScopeForm";
|
|
|
|
import { toClientScope } from "./routes/ClientScope";
|
|
|
|
|
|
|
|
export default function CreateClientScope() {
|
|
|
|
const { t } = useTranslation("client-scopes");
|
|
|
|
const navigate = useNavigate();
|
|
|
|
const { realm } = useRealm();
|
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
|
|
|
|
const onSubmit = async (formData: ClientScopeDefaultOptionalType) => {
|
|
|
|
const clientScope = convertFormValuesToObject({
|
|
|
|
...formData,
|
|
|
|
name: formData.name?.trim().replace(/ /g, "_"),
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.clientScopes.create(clientScope);
|
|
|
|
|
|
|
|
const scope = await adminClient.clientScopes.findOneByName({
|
|
|
|
name: clientScope.name!,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!scope) {
|
|
|
|
throw new Error(t("common:notFound"));
|
|
|
|
}
|
|
|
|
|
2023-05-03 13:51:02 +00:00
|
|
|
await changeScope({ ...clientScope, id: scope.id }, clientScope.type);
|
2023-01-04 11:19:09 +00:00
|
|
|
|
|
|
|
addAlert(t("createSuccess", AlertVariant.success));
|
|
|
|
|
|
|
|
navigate(
|
|
|
|
toClientScope({
|
|
|
|
realm,
|
|
|
|
id: scope.id!,
|
|
|
|
tab: "settings",
|
2023-07-11 14:03:21 +00:00
|
|
|
}),
|
2023-01-04 11:19:09 +00:00
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
addError("client-scopes:createError", error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ViewHeader titleKey="client-scopes:createClientScope" />
|
|
|
|
<PageSection variant="light" className="pf-u-p-0">
|
|
|
|
<PageSection variant="light">
|
|
|
|
<ScopeForm save={onSubmit} />
|
|
|
|
</PageSection>
|
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|