2023-05-03 13:51:02 +00:00
|
|
|
import type ComponentRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentRepresentation";
|
2022-02-09 09:16:10 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
DataList,
|
|
|
|
DataListCell,
|
|
|
|
DataListControl,
|
|
|
|
DataListDragButton,
|
|
|
|
DataListItem,
|
|
|
|
DataListItemCells,
|
|
|
|
DataListItemRow,
|
|
|
|
Modal,
|
|
|
|
ModalVariant,
|
|
|
|
Text,
|
2023-05-03 13:51:02 +00:00
|
|
|
TextContent,
|
2022-02-09 09:16:10 +00:00
|
|
|
} from "@patternfly/react-core";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { sortBy } from "lodash-es";
|
|
|
|
import { useState } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
|
|
|
import { adminClient } from "../admin-client";
|
2022-02-09 09:16:10 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
|
|
|
|
type ManagePriorityDialogProps = {
|
|
|
|
components: ComponentRepresentation[];
|
|
|
|
onClose: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ManagePriorityDialog = ({
|
|
|
|
components,
|
|
|
|
onClose,
|
|
|
|
}: ManagePriorityDialogProps) => {
|
|
|
|
const { t } = useTranslation("user-federation");
|
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
|
|
|
|
const [id, setId] = useState("");
|
|
|
|
const [liveText, setLiveText] = useState("");
|
|
|
|
const [order, setOrder] = useState(
|
|
|
|
components.map((component) => component.name!)
|
|
|
|
);
|
|
|
|
|
|
|
|
const onDragStart = (id: string) => {
|
|
|
|
setId(id);
|
|
|
|
setLiveText(t("common:onDragStart", { item: id }));
|
|
|
|
};
|
|
|
|
|
|
|
|
const onDragMove = () => {
|
|
|
|
setLiveText(t("common:onDragMove", { item: id }));
|
|
|
|
};
|
|
|
|
|
|
|
|
const onDragCancel = () => {
|
|
|
|
setLiveText(t("common:onDragCancel"));
|
|
|
|
};
|
|
|
|
|
|
|
|
const onDragFinish = (providerOrder: string[]) => {
|
|
|
|
setLiveText(t("common:onDragFinish", { list: providerOrder }));
|
|
|
|
setOrder(providerOrder);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
variant={ModalVariant.small}
|
|
|
|
title={t("managePriorityOrder")}
|
|
|
|
isOpen={true}
|
|
|
|
onClose={onClose}
|
|
|
|
actions={[
|
|
|
|
<Button
|
|
|
|
id="modal-confirm"
|
|
|
|
key="confirm"
|
2022-07-06 10:31:05 +00:00
|
|
|
onClick={async () => {
|
|
|
|
const updates = order.map((name, index) => {
|
2022-02-09 09:16:10 +00:00
|
|
|
const component = components!.find((c) => c.name === name)!;
|
|
|
|
component.config!.priority = [index.toString()];
|
2022-07-06 10:31:05 +00:00
|
|
|
return adminClient.components.update(
|
|
|
|
{ id: component.id! },
|
|
|
|
component
|
|
|
|
);
|
2022-02-09 09:16:10 +00:00
|
|
|
});
|
|
|
|
|
2022-07-06 10:31:05 +00:00
|
|
|
try {
|
|
|
|
await Promise.all(updates);
|
|
|
|
addAlert(t("orderChangeSuccess"));
|
|
|
|
} catch (error) {
|
|
|
|
addError("orderChangeError", error);
|
|
|
|
}
|
|
|
|
|
2022-02-09 09:16:10 +00:00
|
|
|
onClose();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("common:save")}
|
|
|
|
</Button>,
|
|
|
|
<Button
|
|
|
|
id="modal-cancel"
|
|
|
|
key="cancel"
|
|
|
|
variant={ButtonVariant.link}
|
|
|
|
onClick={onClose}
|
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>,
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<TextContent className="pf-u-pb-lg">
|
|
|
|
<Text>{t("managePriorityInfo")}</Text>
|
|
|
|
</TextContent>
|
|
|
|
|
|
|
|
<DataList
|
|
|
|
aria-label={t("manageOrderTableAria")}
|
|
|
|
data-testid="manageOrderDataList"
|
|
|
|
isCompact
|
|
|
|
onDragFinish={onDragFinish}
|
|
|
|
onDragStart={onDragStart}
|
|
|
|
onDragMove={onDragMove}
|
|
|
|
onDragCancel={onDragCancel}
|
|
|
|
itemOrder={order}
|
|
|
|
>
|
2022-07-06 10:31:05 +00:00
|
|
|
{sortBy(components, "config.priority", "name").map((component) => (
|
2022-02-09 09:16:10 +00:00
|
|
|
<DataListItem
|
2022-07-06 10:31:05 +00:00
|
|
|
aria-label={component.name}
|
|
|
|
id={component.name}
|
2022-02-09 09:16:10 +00:00
|
|
|
key={component.name}
|
|
|
|
>
|
|
|
|
<DataListItemRow>
|
|
|
|
<DataListControl>
|
2022-08-30 11:27:47 +00:00
|
|
|
<DataListDragButton aria-label={t("common-help:dragHelp")} />
|
2022-02-09 09:16:10 +00:00
|
|
|
</DataListControl>
|
|
|
|
<DataListItemCells
|
|
|
|
dataListCells={[
|
|
|
|
<DataListCell
|
2022-07-06 10:31:05 +00:00
|
|
|
key={component.name}
|
2022-02-09 09:16:10 +00:00
|
|
|
data-testid={component.name}
|
|
|
|
>
|
2022-07-06 10:31:05 +00:00
|
|
|
{component.name}
|
2022-02-09 09:16:10 +00:00
|
|
|
</DataListCell>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</DataListItemRow>
|
|
|
|
</DataListItem>
|
|
|
|
))}
|
|
|
|
</DataList>
|
|
|
|
<div className="pf-screen-reader" aria-live="assertive">
|
|
|
|
{liveText}
|
|
|
|
</div>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|