2021-04-21 13:18:45 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2022-02-02 10:33:57 +00:00
|
|
|
import { sortBy } from "lodash-es";
|
2021-04-21 13:18:45 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
DataList,
|
|
|
|
DataListCell,
|
|
|
|
DataListControl,
|
|
|
|
DataListDragButton,
|
|
|
|
DataListItem,
|
|
|
|
DataListItemCells,
|
|
|
|
DataListItemRow,
|
|
|
|
Modal,
|
|
|
|
ModalVariant,
|
|
|
|
TextContent,
|
|
|
|
Text,
|
|
|
|
} from "@patternfly/react-core";
|
2021-08-26 08:39:35 +00:00
|
|
|
import type IdentityProviderRepresentation from "@keycloak/keycloak-admin-client/lib/defs/identityProviderRepresentation";
|
2021-04-21 13:18:45 +00:00
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
|
2022-02-09 09:16:10 +00:00
|
|
|
type ManageOrderDialogProps = {
|
2021-04-21 13:18:45 +00:00
|
|
|
providers: IdentityProviderRepresentation[];
|
|
|
|
onClose: () => void;
|
|
|
|
};
|
|
|
|
|
2022-02-09 09:16:10 +00:00
|
|
|
export const ManageOrderDialog = ({
|
2021-04-21 13:18:45 +00:00
|
|
|
providers,
|
|
|
|
onClose,
|
2022-02-09 09:16:10 +00:00
|
|
|
}: ManageOrderDialogProps) => {
|
2021-04-21 13:18:45 +00:00
|
|
|
const { t } = useTranslation("identity-providers");
|
|
|
|
const adminClient = useAdminClient();
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2021-04-21 13:18:45 +00:00
|
|
|
|
|
|
|
const [alias, setAlias] = useState("");
|
|
|
|
const [liveText, setLiveText] = useState("");
|
|
|
|
const [order, setOrder] = useState(
|
|
|
|
providers.map((provider) => provider.alias!)
|
|
|
|
);
|
|
|
|
|
|
|
|
const onDragStart = (id: string) => {
|
|
|
|
setAlias(id);
|
2021-08-09 08:47:34 +00:00
|
|
|
setLiveText(t("common:onDragStart", { item: id }));
|
2021-04-21 13:18:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const onDragMove = () => {
|
2021-08-09 08:47:34 +00:00
|
|
|
setLiveText(t("common:onDragMove", { item: alias }));
|
2021-04-21 13:18:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const onDragCancel = () => {
|
2021-08-09 08:47:34 +00:00
|
|
|
setLiveText(t("common:onDragCancel"));
|
2021-04-21 13:18:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const onDragFinish = (providerOrder: string[]) => {
|
2021-08-09 08:47:34 +00:00
|
|
|
setLiveText(t("common:onDragFinish", { list: providerOrder }));
|
2021-04-21 13:18:45 +00:00
|
|
|
setOrder(providerOrder);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
variant={ModalVariant.small}
|
|
|
|
title={t("manageDisplayOrder")}
|
|
|
|
isOpen={true}
|
|
|
|
onClose={onClose}
|
|
|
|
actions={[
|
|
|
|
<Button
|
|
|
|
id="modal-confirm"
|
2022-02-02 11:44:52 +00:00
|
|
|
data-testid="confirm"
|
2021-04-21 13:18:45 +00:00
|
|
|
key="confirm"
|
|
|
|
onClick={() => {
|
|
|
|
order.map(async (alias, index) => {
|
|
|
|
const provider = providers.find((p) => p.alias === alias)!;
|
|
|
|
provider.config!.guiOrder = index;
|
|
|
|
try {
|
|
|
|
await adminClient.identityProviders.update({ alias }, provider);
|
|
|
|
addAlert(t("orderChangeSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("identity-providers:orderChangeError", error);
|
2021-04-21 13:18:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
onClose();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("common:save")}
|
|
|
|
</Button>,
|
|
|
|
<Button
|
|
|
|
id="modal-cancel"
|
2022-02-02 11:44:52 +00:00
|
|
|
data-testid="cancel"
|
2021-04-21 13:18:45 +00:00
|
|
|
key="cancel"
|
|
|
|
variant={ButtonVariant.link}
|
|
|
|
onClick={onClose}
|
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>,
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<TextContent className="pf-u-pb-lg">
|
2022-02-09 09:16:10 +00:00
|
|
|
<Text>{t("orderDialogIntro")}</Text>
|
2021-04-21 13:18:45 +00:00
|
|
|
</TextContent>
|
|
|
|
|
|
|
|
<DataList
|
|
|
|
aria-label={t("manageOrderTableAria")}
|
|
|
|
data-testid="manageOrderDataList"
|
|
|
|
isCompact
|
|
|
|
onDragFinish={onDragFinish}
|
|
|
|
onDragStart={onDragStart}
|
|
|
|
onDragMove={onDragMove}
|
|
|
|
onDragCancel={onDragCancel}
|
|
|
|
itemOrder={order}
|
|
|
|
>
|
2022-02-02 10:33:57 +00:00
|
|
|
{sortBy(providers, "config.guiOrder").map((provider) => (
|
2021-04-21 13:18:45 +00:00
|
|
|
<DataListItem
|
|
|
|
aria-labelledby={provider.alias}
|
|
|
|
id={provider.alias}
|
|
|
|
key={provider.alias}
|
|
|
|
>
|
|
|
|
<DataListItemRow>
|
|
|
|
<DataListControl>
|
|
|
|
<DataListDragButton
|
|
|
|
aria-label="Reorder"
|
|
|
|
aria-labelledby={provider.alias}
|
|
|
|
aria-describedby={t("manageOrderItemAria")}
|
|
|
|
aria-pressed="false"
|
|
|
|
/>
|
|
|
|
</DataListControl>
|
|
|
|
<DataListItemCells
|
|
|
|
dataListCells={[
|
|
|
|
<DataListCell
|
|
|
|
key={`${provider.alias}-cell`}
|
|
|
|
data-testid={provider.alias}
|
|
|
|
>
|
|
|
|
<span id={provider.alias}>{provider.alias}</span>
|
|
|
|
</DataListCell>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</DataListItemRow>
|
|
|
|
</DataListItem>
|
|
|
|
))}
|
|
|
|
</DataList>
|
|
|
|
<div className="pf-screen-reader" aria-live="assertive">
|
|
|
|
{liveText}
|
|
|
|
</div>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|