2020-12-04 21:08:11 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
2020-11-24 20:11:13 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
Dropdown,
|
|
|
|
DropdownToggle,
|
|
|
|
Modal,
|
|
|
|
ModalVariant,
|
|
|
|
DropdownDirection,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { CaretUpIcon } from "@patternfly/react-icons";
|
|
|
|
import {
|
|
|
|
Table,
|
|
|
|
TableBody,
|
|
|
|
TableHeader,
|
|
|
|
TableVariant,
|
|
|
|
} from "@patternfly/react-table";
|
|
|
|
import ClientScopeRepresentation from "keycloak-admin/lib/defs/clientScopeRepresentation";
|
|
|
|
|
2021-04-06 07:29:11 +00:00
|
|
|
import {
|
|
|
|
ClientScopeType,
|
|
|
|
clientScopeTypesDropdown,
|
|
|
|
} from "../../components/client-scope/ClientScopeTypes";
|
2020-11-24 20:11:13 +00:00
|
|
|
|
|
|
|
export type AddScopeDialogProps = {
|
|
|
|
clientScopes: ClientScopeRepresentation[];
|
|
|
|
open: boolean;
|
|
|
|
toggleDialog: () => void;
|
2020-12-04 21:08:11 +00:00
|
|
|
onAdd: (
|
|
|
|
scopes: { scope: ClientScopeRepresentation; type: ClientScopeType }[]
|
|
|
|
) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
type Row = {
|
|
|
|
selected: boolean;
|
|
|
|
scope: ClientScopeRepresentation;
|
|
|
|
cells: (string | undefined)[];
|
2020-11-24 20:11:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const AddScopeDialog = ({
|
|
|
|
clientScopes,
|
|
|
|
open,
|
|
|
|
toggleDialog,
|
2020-12-04 21:08:11 +00:00
|
|
|
onAdd,
|
2020-11-24 20:11:13 +00:00
|
|
|
}: AddScopeDialogProps) => {
|
|
|
|
const { t } = useTranslation("clients");
|
|
|
|
const [addToggle, setAddToggle] = useState(false);
|
2020-12-04 21:08:11 +00:00
|
|
|
const [rows, setRows] = useState<Row[]>([]);
|
2020-11-24 20:11:13 +00:00
|
|
|
|
2020-12-04 21:08:11 +00:00
|
|
|
useEffect(() => {
|
|
|
|
setRows(
|
|
|
|
clientScopes.map((scope) => {
|
|
|
|
return {
|
|
|
|
selected: false,
|
|
|
|
scope,
|
|
|
|
cells: [scope.name, scope.description],
|
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}, [clientScopes]);
|
|
|
|
|
|
|
|
const action = (scope: ClientScopeType) => {
|
|
|
|
const scopes = rows
|
|
|
|
.filter((row) => row.selected)
|
|
|
|
.map((row) => {
|
|
|
|
return { scope: row.scope, type: scope };
|
|
|
|
});
|
|
|
|
onAdd(scopes);
|
|
|
|
setAddToggle(false);
|
|
|
|
toggleDialog();
|
|
|
|
};
|
2020-11-24 20:11:13 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
variant={ModalVariant.medium}
|
|
|
|
title={t("addClientScopesTo", { clientId: "test" })}
|
|
|
|
isOpen={open}
|
|
|
|
onClose={toggleDialog}
|
|
|
|
actions={[
|
|
|
|
<Dropdown
|
|
|
|
id="add-dropdown"
|
|
|
|
key="add-dropdown"
|
|
|
|
direction={DropdownDirection.up}
|
|
|
|
isOpen={addToggle}
|
|
|
|
toggle={
|
|
|
|
<DropdownToggle
|
|
|
|
onToggle={() => setAddToggle(!addToggle)}
|
|
|
|
isPrimary
|
|
|
|
toggleIndicator={CaretUpIcon}
|
|
|
|
id="add-scope-toggle"
|
|
|
|
>
|
|
|
|
{t("common:add")}
|
|
|
|
</DropdownToggle>
|
|
|
|
}
|
2020-12-04 21:08:11 +00:00
|
|
|
dropdownItems={clientScopeTypesDropdown(t, action)}
|
2020-11-24 20:11:13 +00:00
|
|
|
/>,
|
|
|
|
<Button
|
|
|
|
id="modal-cancel"
|
|
|
|
key="cancel"
|
|
|
|
variant={ButtonVariant.secondary}
|
2020-12-04 21:08:11 +00:00
|
|
|
onClick={() => {
|
|
|
|
setRows(
|
|
|
|
rows.map((row) => {
|
|
|
|
row.selected = false;
|
|
|
|
return row;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
toggleDialog();
|
|
|
|
}}
|
2020-11-24 20:11:13 +00:00
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>,
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<Table
|
|
|
|
variant={TableVariant.compact}
|
2021-01-13 20:48:16 +00:00
|
|
|
cells={[t("common:name"), t("common:description")]}
|
2020-12-04 21:08:11 +00:00
|
|
|
onSelect={(_, isSelected, rowIndex) => {
|
|
|
|
if (rowIndex === -1) {
|
|
|
|
setRows(
|
|
|
|
rows.map((row) => {
|
|
|
|
row.selected = isSelected;
|
|
|
|
return row;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
rows[rowIndex].selected = isSelected;
|
|
|
|
setRows([...rows]);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
rows={rows}
|
2020-11-24 20:11:13 +00:00
|
|
|
aria-label={t("chooseAMapperType")}
|
|
|
|
>
|
|
|
|
<TableHeader />
|
|
|
|
<TableBody />
|
|
|
|
</Table>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|