keycloak-scim/src/realm-settings/PartialExport.tsx

145 lines
3.6 KiB
TypeScript
Raw Normal View History

import {
Alert,
AlertVariant,
Button,
ButtonVariant,
2022-06-27 08:46:49 +00:00
Form,
FormGroup,
Modal,
ModalVariant,
2022-06-27 08:46:49 +00:00
Switch,
Text,
TextContent,
} from "@patternfly/react-core";
import FileSaver from "file-saver";
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import { useAlerts } from "../components/alert/Alerts";
import { useAdminClient } from "../context/auth/AdminClient";
import { useRealm } from "../context/realm-context/RealmContext";
import { prettyPrintJSON } from "../util";
2022-06-27 08:46:49 +00:00
import "./partial-export.css";
export type PartialExportDialogProps = {
isOpen: boolean;
onClose: () => void;
};
export const PartialExportDialog = ({
isOpen,
onClose,
}: PartialExportDialogProps) => {
2022-06-27 08:46:49 +00:00
const { t } = useTranslation("realm-settings");
const { realm } = useRealm();
const { adminClient } = useAdminClient();
const { addAlert, addError } = useAlerts();
const [exportGroupsAndRoles, setExportGroupsAndRoles] = useState(false);
const [exportClients, setExportClients] = useState(false);
const [isExporting, setIsExporting] = useState(false);
const showWarning = exportGroupsAndRoles || exportClients;
async function exportRealm() {
setIsExporting(true);
try {
const realmExport = await adminClient.realms.export({
realm,
exportClients,
exportGroupsAndRoles,
});
FileSaver.saveAs(
new Blob([prettyPrintJSON(realmExport)], {
type: "application/json",
}),
"realm-export.json"
);
2022-06-27 08:46:49 +00:00
addAlert(t("exportSuccess"), AlertVariant.success);
onClose();
} catch (error) {
2022-06-27 08:46:49 +00:00
addError("exportFail", error);
}
setIsExporting(false);
}
return (
<Modal
variant={ModalVariant.small}
2022-06-27 08:46:49 +00:00
title={t("partialExport")}
isOpen={isOpen}
onClose={onClose}
actions={[
<Button
key="export"
data-testid="export-button"
isDisabled={isExporting}
onClick={exportRealm}
>
{t("common:export")}
</Button>,
<Button
key="cancel"
data-testid="cancel-button"
variant={ButtonVariant.link}
onClick={onClose}
>
{t("common:cancel")}
</Button>,
]}
>
2022-06-27 08:46:49 +00:00
<TextContent>
<Text>{t("partialExportHeaderText")}</Text>
</TextContent>
<Form
isHorizontal
className="keycloak__realm-settings__partial-import_form"
>
<FormGroup
label={t("includeGroupsAndRoles")}
fieldId="include-groups-and-roles-check"
hasNoPaddingTop
>
<Switch
id="include-groups-and-roles-check"
2022-06-27 08:46:49 +00:00
data-testid="include-groups-and-roles-check"
isChecked={exportGroupsAndRoles}
onChange={setExportGroupsAndRoles}
2022-06-27 08:46:49 +00:00
label={t("common:on")}
labelOff={t("common:off")}
/>
2022-06-27 08:46:49 +00:00
</FormGroup>
<FormGroup
label={t("includeClients")}
fieldId="include-clients-check"
hasNoPaddingTop
>
<Switch
id="include-clients-check"
2022-06-27 08:46:49 +00:00
data-testid="include-clients-check"
onChange={setExportClients}
2022-06-27 08:46:49 +00:00
isChecked={exportClients}
label={t("common:on")}
labelOff={t("common:off")}
/>
2022-06-27 08:46:49 +00:00
</FormGroup>
</Form>
{showWarning && (
<Alert
data-testid="warning-message"
variant="warning"
title={t("exportWarningTitle")}
isInline
>
{t("exportWarningDescription")}
</Alert>
)}
</Modal>
);
};