2023-02-16 15:29:28 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
DataListAction,
|
|
|
|
DataListCell,
|
|
|
|
DataListItem,
|
|
|
|
DataListItemCells,
|
|
|
|
DataListItemRow,
|
|
|
|
Label,
|
|
|
|
Split,
|
|
|
|
SplitItem,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { LinkIcon, UnlinkIcon } from "@patternfly/react-icons";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { IconMapper, useAlerts } from "ui-shared";
|
|
|
|
import { linkAccount, unLinkAccount } from "../api/methods";
|
|
|
|
import { LinkedAccountRepresentation } from "../api/representations";
|
2024-02-01 15:46:11 +00:00
|
|
|
import { useEnvironment } from "../root/KeycloakContext";
|
2023-02-16 15:29:28 +00:00
|
|
|
|
|
|
|
type AccountRowProps = {
|
|
|
|
account: LinkedAccountRepresentation;
|
|
|
|
isLinked?: boolean;
|
|
|
|
refresh: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const AccountRow = ({ account, isLinked = false }: AccountRowProps) => {
|
|
|
|
const { t } = useTranslation();
|
2024-02-01 15:46:11 +00:00
|
|
|
const context = useEnvironment();
|
2023-02-16 15:29:28 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
|
|
|
|
const unLink = async (account: LinkedAccountRepresentation) => {
|
|
|
|
try {
|
2024-02-01 15:46:11 +00:00
|
|
|
await unLinkAccount(context, account);
|
2023-03-02 12:54:34 +00:00
|
|
|
addAlert(t("unLinkSuccess"));
|
2023-02-16 15:29:28 +00:00
|
|
|
} catch (error) {
|
2023-03-02 12:54:34 +00:00
|
|
|
addError(t("unLinkError", { error }).toString());
|
2023-02-16 15:29:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const link = async (account: LinkedAccountRepresentation) => {
|
|
|
|
try {
|
2024-02-01 15:46:11 +00:00
|
|
|
const { accountLinkUri } = await linkAccount(context, account);
|
2023-02-16 15:29:28 +00:00
|
|
|
location.href = accountLinkUri;
|
|
|
|
} catch (error) {
|
2023-03-02 12:54:34 +00:00
|
|
|
addError(t("linkError", { error }).toString());
|
2023-02-16 15:29:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<DataListItem
|
|
|
|
id={`${account.providerAlias}-idp`}
|
|
|
|
key={account.providerName}
|
2023-07-13 16:23:44 +00:00
|
|
|
aria-label={t("linkedAccounts")}
|
2023-02-16 15:29:28 +00:00
|
|
|
>
|
2024-02-27 06:15:23 +00:00
|
|
|
<DataListItemRow
|
|
|
|
key={account.providerName}
|
|
|
|
data-testid={`linked-accounts/${account.providerName}`}
|
|
|
|
>
|
2023-02-16 15:29:28 +00:00
|
|
|
<DataListItemCells
|
|
|
|
dataListCells={[
|
|
|
|
<DataListCell key="idp">
|
|
|
|
<Split>
|
|
|
|
<SplitItem className="pf-u-mr-sm">
|
|
|
|
<IconMapper icon={account.providerName} />
|
|
|
|
</SplitItem>
|
|
|
|
<SplitItem className="pf-u-my-xs" isFilled>
|
|
|
|
<span id={`${account.providerAlias}-idp-name`}>
|
|
|
|
{account.displayName}
|
|
|
|
</span>
|
|
|
|
</SplitItem>
|
|
|
|
</Split>
|
|
|
|
</DataListCell>,
|
|
|
|
<DataListCell key="label">
|
|
|
|
<Split>
|
|
|
|
<SplitItem className="pf-u-my-xs" isFilled>
|
|
|
|
<span id={`${account.providerAlias}-idp-label`}>
|
|
|
|
<Label color={account.social ? "blue" : "green"}>
|
|
|
|
{t(account.social ? "socialLogin" : "systemDefined")}
|
|
|
|
</Label>
|
|
|
|
</span>
|
|
|
|
</SplitItem>
|
|
|
|
</Split>
|
|
|
|
</DataListCell>,
|
|
|
|
<DataListCell key="username" width={5}>
|
|
|
|
<Split>
|
|
|
|
<SplitItem className="pf-u-my-xs" isFilled>
|
|
|
|
<span id={`${account.providerAlias}-idp-username`}>
|
|
|
|
{account.linkedUsername}
|
|
|
|
</span>
|
|
|
|
</SplitItem>
|
|
|
|
</Split>
|
|
|
|
</DataListCell>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
<DataListAction
|
|
|
|
aria-labelledby={t("link")}
|
|
|
|
aria-label={t("unLink")}
|
|
|
|
id="setPasswordAction"
|
|
|
|
>
|
|
|
|
{isLinked && (
|
|
|
|
<Button
|
|
|
|
id={`${account.providerAlias}-idp-unlink`}
|
|
|
|
variant="link"
|
|
|
|
onClick={() => unLink(account)}
|
|
|
|
>
|
|
|
|
<UnlinkIcon size="sm" /> {t("unLink")}
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
{!isLinked && (
|
|
|
|
<Button
|
|
|
|
id={`${account.providerAlias}-idp-link`}
|
|
|
|
variant="link"
|
|
|
|
onClick={() => link(account)}
|
|
|
|
>
|
|
|
|
<LinkIcon size="sm" /> {t("link")}
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</DataListAction>
|
|
|
|
</DataListItemRow>
|
|
|
|
</DataListItem>
|
|
|
|
);
|
|
|
|
};
|