2020-12-07 19:23:18 +00:00
|
|
|
import React from "react";
|
|
|
|
import { Link, useHistory } from "react-router-dom";
|
2020-09-03 19:25:05 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2020-12-07 19:23:18 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Badge,
|
|
|
|
Button,
|
|
|
|
PageSection,
|
|
|
|
} from "@patternfly/react-core";
|
2020-09-03 19:25:05 +00:00
|
|
|
|
2020-09-25 17:29:03 +00:00
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
2020-11-12 12:55:52 +00:00
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
2020-12-11 10:28:38 +00:00
|
|
|
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
|
2020-12-07 19:23:18 +00:00
|
|
|
import { IFormatter, IFormatterValueType } from "@patternfly/react-table";
|
|
|
|
import { exportClient } from "../util";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import ClientRepresentation from "keycloak-admin/lib/defs/clientRepresentation";
|
|
|
|
import { ExternalLink } from "../components/external-link/ExternalLink";
|
2020-09-03 19:25:05 +00:00
|
|
|
|
2020-09-10 18:04:03 +00:00
|
|
|
export const ClientsSection = () => {
|
|
|
|
const { t } = useTranslation("clients");
|
2020-12-07 19:23:18 +00:00
|
|
|
const { addAlert } = useAlerts();
|
2020-09-03 19:25:05 +00:00
|
|
|
const history = useHistory();
|
2020-09-17 11:37:30 +00:00
|
|
|
|
2020-11-12 12:55:52 +00:00
|
|
|
const adminClient = useAdminClient();
|
2020-12-07 19:23:18 +00:00
|
|
|
const baseUrl = adminClient.keycloak
|
|
|
|
? adminClient.keycloak.authServerUrl!
|
|
|
|
: adminClient.baseUrl + "/";
|
2020-09-03 19:25:05 +00:00
|
|
|
|
2020-12-07 19:23:18 +00:00
|
|
|
const loader = async (first?: number, max?: number, search?: string) => {
|
|
|
|
const params: { [name: string]: string | number } = {
|
|
|
|
first: first!,
|
|
|
|
max: max!,
|
|
|
|
};
|
2020-10-05 18:13:23 +00:00
|
|
|
if (search) {
|
|
|
|
params.clientId = search;
|
|
|
|
params.search = "true";
|
|
|
|
}
|
2020-12-07 19:23:18 +00:00
|
|
|
return await adminClient.clients.find({ ...params });
|
|
|
|
};
|
|
|
|
|
|
|
|
const emptyFormatter = (): IFormatter => (data?: IFormatterValueType) => {
|
|
|
|
return data ? data : "—";
|
2020-09-03 19:25:05 +00:00
|
|
|
};
|
|
|
|
|
2020-12-07 19:23:18 +00:00
|
|
|
const externalLink = (): IFormatter => (data?: IFormatterValueType) => {
|
|
|
|
return (data ? (
|
|
|
|
<ExternalLink href={data.toString()} />
|
|
|
|
) : undefined) as object;
|
|
|
|
};
|
2020-10-05 18:13:23 +00:00
|
|
|
|
2020-12-07 19:23:18 +00:00
|
|
|
const ClientDetailLink = (client: ClientRepresentation) => (
|
|
|
|
<>
|
|
|
|
<Link key={client.id} to={`/clients/${client.id}`}>
|
|
|
|
{client.clientId}
|
|
|
|
{!client.enabled && <Badge isRead>Disabled</Badge>}
|
|
|
|
</Link>
|
|
|
|
</>
|
|
|
|
);
|
2020-09-03 19:25:05 +00:00
|
|
|
return (
|
2020-09-25 17:29:03 +00:00
|
|
|
<>
|
|
|
|
<ViewHeader
|
|
|
|
titleKey="clients:clientList"
|
|
|
|
subKey="clients:clientsExplain"
|
|
|
|
/>
|
|
|
|
<PageSection variant="light">
|
2020-12-11 10:18:29 +00:00
|
|
|
<KeycloakDataTable
|
2020-12-07 19:23:18 +00:00
|
|
|
loader={loader}
|
|
|
|
isPaginated
|
|
|
|
ariaLabelKey="clients:clientList"
|
|
|
|
searchPlaceholderKey="clients:searchForClient"
|
|
|
|
toolbarItem={
|
|
|
|
<>
|
|
|
|
<Button onClick={() => history.push("/add-client")}>
|
|
|
|
{t("createClient")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
onClick={() => history.push("/import-client")}
|
|
|
|
variant="link"
|
|
|
|
>
|
|
|
|
{t("importClient")}
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
actions={[
|
|
|
|
{
|
|
|
|
title: t("common:export"),
|
|
|
|
onRowClick: (client) => {
|
|
|
|
exportClient(client);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: t("common:delete"),
|
|
|
|
onRowClick: async (client) => {
|
|
|
|
try {
|
|
|
|
await adminClient.clients.del({
|
|
|
|
id: client.id!,
|
|
|
|
});
|
|
|
|
addAlert(t("clientDeletedSuccess"), AlertVariant.success);
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
addAlert(
|
|
|
|
t("clientDeleteError", { error }),
|
|
|
|
AlertVariant.danger
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
name: "clientId",
|
|
|
|
displayKey: "clients:clientID",
|
|
|
|
cellRenderer: ClientDetailLink,
|
|
|
|
},
|
|
|
|
{ name: "protocol", displayKey: "clients:type" },
|
|
|
|
{
|
|
|
|
name: "description",
|
|
|
|
displayKey: "clients:description",
|
|
|
|
cellFormatters: [emptyFormatter()],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "baseUrl",
|
|
|
|
displayKey: "clients:homeURL",
|
|
|
|
cellFormatters: [externalLink(), emptyFormatter()],
|
|
|
|
cellRenderer: (client) => {
|
|
|
|
if (client.rootUrl) {
|
|
|
|
if (
|
|
|
|
!client.rootUrl.startsWith("http") ||
|
|
|
|
client.rootUrl.indexOf("$") !== -1
|
|
|
|
) {
|
|
|
|
client.rootUrl =
|
|
|
|
client.rootUrl
|
|
|
|
.replace("${authBaseUrl}", baseUrl)
|
|
|
|
.replace("${authAdminUrl}", baseUrl) +
|
|
|
|
(client.baseUrl ? client.baseUrl.substr(1) : "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return client.rootUrl;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
2020-09-25 17:29:03 +00:00
|
|
|
</PageSection>
|
|
|
|
</>
|
2020-09-03 19:25:05 +00:00
|
|
|
);
|
|
|
|
};
|