2020-11-12 12:55:52 +00:00
|
|
|
import React, { useState, useEffect, ReactElement, useContext } from "react";
|
2020-10-06 19:51:21 +00:00
|
|
|
import {
|
|
|
|
Alert,
|
|
|
|
AlertVariant,
|
|
|
|
Form,
|
|
|
|
FormGroup,
|
2020-10-29 17:02:06 +00:00
|
|
|
ModalVariant,
|
2020-10-06 19:51:21 +00:00
|
|
|
Select,
|
|
|
|
SelectOption,
|
|
|
|
SelectVariant,
|
|
|
|
Stack,
|
|
|
|
StackItem,
|
|
|
|
TextArea,
|
|
|
|
} from "@patternfly/react-core";
|
2020-10-13 17:57:35 +00:00
|
|
|
import FileSaver from "file-saver";
|
|
|
|
|
2020-10-06 19:51:21 +00:00
|
|
|
import { ConfirmDialogModal } from "../confirm-dialog/ConfirmDialog";
|
|
|
|
import { HelpItem } from "../help-enabler/HelpItem";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2020-10-13 17:57:35 +00:00
|
|
|
import { useServerInfo } from "../../context/server-info/ServerInfoProvider";
|
2020-11-12 12:55:52 +00:00
|
|
|
import { useAdminClient } from "../../context/auth/AdminClient";
|
2020-10-13 17:57:35 +00:00
|
|
|
import { HelpContext } from "../help-enabler/HelpHeader";
|
2020-10-06 19:51:21 +00:00
|
|
|
|
|
|
|
export type DownloadDialogProps = {
|
|
|
|
id: string;
|
|
|
|
protocol?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type DownloadDialogModalProps = DownloadDialogProps & {
|
|
|
|
open: boolean;
|
|
|
|
toggleDialog: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useDownloadDialog = (
|
|
|
|
props: DownloadDialogProps
|
|
|
|
): [() => void, () => ReactElement] => {
|
|
|
|
const [show, setShow] = useState(false);
|
|
|
|
|
|
|
|
function toggleDialog() {
|
|
|
|
setShow((show) => !show);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Dialog = () => (
|
|
|
|
<DownloadDialog {...props} open={show} toggleDialog={toggleDialog} />
|
|
|
|
);
|
|
|
|
return [toggleDialog, Dialog];
|
|
|
|
};
|
|
|
|
|
|
|
|
export const DownloadDialog = ({
|
|
|
|
id,
|
|
|
|
open,
|
|
|
|
toggleDialog,
|
|
|
|
protocol = "openid-connect",
|
|
|
|
}: DownloadDialogModalProps) => {
|
2020-11-12 12:55:52 +00:00
|
|
|
const adminClient = useAdminClient();
|
2020-10-06 19:51:21 +00:00
|
|
|
const { t } = useTranslation("common");
|
2020-10-13 17:57:35 +00:00
|
|
|
const { enabled } = useContext(HelpContext);
|
|
|
|
const serverInfo = useServerInfo();
|
2020-10-06 19:51:21 +00:00
|
|
|
|
2020-11-12 12:55:52 +00:00
|
|
|
const configFormats = serverInfo.clientInstallations![protocol];
|
2020-10-06 19:51:21 +00:00
|
|
|
const [selected, setSelected] = useState(
|
|
|
|
configFormats[configFormats.length - 1].id
|
|
|
|
);
|
|
|
|
const [snippet, setSnippet] = useState("");
|
|
|
|
const [openType, setOpenType] = useState(false);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-10-29 17:02:06 +00:00
|
|
|
let isMounted = true;
|
2020-10-06 19:51:21 +00:00
|
|
|
(async () => {
|
2020-11-12 12:55:52 +00:00
|
|
|
const snippet = await adminClient.clients.getInstallationProviders({
|
|
|
|
id,
|
|
|
|
providerId: selected,
|
|
|
|
});
|
2020-10-29 17:02:06 +00:00
|
|
|
if (isMounted) {
|
2020-11-12 12:55:52 +00:00
|
|
|
if (typeof snippet === "string") {
|
|
|
|
setSnippet(snippet);
|
|
|
|
} else {
|
|
|
|
setSnippet(JSON.stringify(snippet, undefined, 3));
|
|
|
|
}
|
2020-10-29 17:02:06 +00:00
|
|
|
}
|
2020-10-06 19:51:21 +00:00
|
|
|
})();
|
2020-10-29 17:02:06 +00:00
|
|
|
return () => {
|
|
|
|
isMounted = false;
|
|
|
|
};
|
|
|
|
}, [selected]);
|
2020-10-06 19:51:21 +00:00
|
|
|
return (
|
|
|
|
<ConfirmDialogModal
|
|
|
|
titleKey={t("clients:downloadAdaptorTitle")}
|
|
|
|
continueButtonLabel={t("download")}
|
2020-10-13 17:57:35 +00:00
|
|
|
onConfirm={() => {
|
|
|
|
const config = configFormats.find((config) => config.id === selected)!;
|
|
|
|
FileSaver.saveAs(
|
|
|
|
new Blob([snippet], { type: config.mediaType }),
|
|
|
|
config.filename
|
|
|
|
);
|
|
|
|
}}
|
2020-10-06 19:51:21 +00:00
|
|
|
open={open}
|
|
|
|
toggleDialog={toggleDialog}
|
2020-10-29 17:02:06 +00:00
|
|
|
variant={ModalVariant.medium}
|
2020-10-06 19:51:21 +00:00
|
|
|
>
|
|
|
|
<Form>
|
|
|
|
<Stack hasGutter>
|
2020-10-13 17:57:35 +00:00
|
|
|
{enabled && (
|
|
|
|
<StackItem>
|
|
|
|
<Alert
|
|
|
|
id={id}
|
|
|
|
title={t("clients:description")}
|
|
|
|
variant={AlertVariant.info}
|
|
|
|
isInline
|
|
|
|
>
|
|
|
|
{
|
|
|
|
configFormats.find(
|
|
|
|
(configFormat) => configFormat.id === selected
|
|
|
|
)?.helpText
|
|
|
|
}
|
|
|
|
</Alert>
|
|
|
|
</StackItem>
|
|
|
|
)}
|
2020-10-06 19:51:21 +00:00
|
|
|
<StackItem>
|
|
|
|
<FormGroup
|
|
|
|
fieldId="type"
|
|
|
|
label={t("clients:formatOption")}
|
2020-10-09 06:48:50 +00:00
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText={t("clients-help:downloadType")}
|
|
|
|
forLabel={t("clients:formatOption")}
|
|
|
|
forID="type"
|
|
|
|
/>
|
|
|
|
}
|
2020-10-06 19:51:21 +00:00
|
|
|
>
|
|
|
|
<Select
|
2020-10-09 06:48:50 +00:00
|
|
|
toggleId="type"
|
2020-10-06 19:51:21 +00:00
|
|
|
isOpen={openType}
|
|
|
|
onToggle={() => {
|
|
|
|
setOpenType(!openType);
|
|
|
|
}}
|
|
|
|
variant={SelectVariant.single}
|
|
|
|
value={selected}
|
|
|
|
selections={selected}
|
|
|
|
onSelect={(_, value) => {
|
|
|
|
setSelected(value as string);
|
|
|
|
setOpenType(false);
|
|
|
|
}}
|
|
|
|
aria-label="Select Input"
|
|
|
|
>
|
|
|
|
{configFormats.map((configFormat) => (
|
|
|
|
<SelectOption
|
|
|
|
key={configFormat.id}
|
|
|
|
value={configFormat.id}
|
|
|
|
isSelected={selected === configFormat.id}
|
|
|
|
>
|
|
|
|
{configFormat.displayType}
|
|
|
|
</SelectOption>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
</FormGroup>
|
|
|
|
</StackItem>
|
|
|
|
<StackItem isFilled>
|
|
|
|
<FormGroup
|
|
|
|
fieldId="details"
|
|
|
|
label={t("clients:details")}
|
2020-10-09 06:48:50 +00:00
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText={t("clients-help:details")}
|
|
|
|
forLabel={t("clients:details")}
|
2020-10-13 17:57:35 +00:00
|
|
|
forID="details"
|
2020-10-09 06:48:50 +00:00
|
|
|
/>
|
|
|
|
}
|
2020-10-06 19:51:21 +00:00
|
|
|
>
|
|
|
|
<TextArea
|
|
|
|
id="details"
|
|
|
|
readOnly
|
|
|
|
rows={12}
|
|
|
|
resizeOrientation="vertical"
|
|
|
|
value={snippet}
|
|
|
|
aria-label="text area example"
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
</StackItem>
|
|
|
|
</Stack>
|
|
|
|
</Form>
|
|
|
|
</ConfirmDialogModal>
|
|
|
|
);
|
|
|
|
};
|