2022-01-05 17:06:53 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
ButtonVariant,
|
|
|
|
DropdownItem,
|
|
|
|
} from "@patternfly/react-core";
|
2023-01-12 08:47:26 +00:00
|
|
|
import { ReactElement } from "react";
|
2023-01-26 09:31:07 +00:00
|
|
|
import { Controller, useFormContext } from "react-hook-form";
|
2023-01-12 08:47:26 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2023-01-18 12:09:49 +00:00
|
|
|
import { useNavigate, useParams } from "react-router-dom";
|
2022-01-05 17:06:53 +00:00
|
|
|
|
2023-05-03 13:51:02 +00:00
|
|
|
import { adminClient } from "../../admin-client";
|
2023-01-12 08:47:26 +00:00
|
|
|
import { useAlerts } from "../../components/alert/Alerts";
|
2022-01-05 17:06:53 +00:00
|
|
|
import { useConfirmDialog } from "../../components/confirm-dialog/ConfirmDialog";
|
|
|
|
import { ViewHeader } from "../../components/view-header/ViewHeader";
|
|
|
|
import { useRealm } from "../../context/realm-context/RealmContext";
|
2023-01-12 08:47:26 +00:00
|
|
|
import { CustomUserFederationRouteParams } from "../routes/CustomUserFederation";
|
2022-01-05 17:06:53 +00:00
|
|
|
import { toUserFederation } from "../routes/UserFederation";
|
|
|
|
|
|
|
|
type HeaderProps = {
|
|
|
|
provider: string;
|
|
|
|
save: () => void;
|
|
|
|
dropdownItems?: ReactElement[];
|
|
|
|
noDivider?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Header = ({
|
|
|
|
provider,
|
|
|
|
save,
|
|
|
|
noDivider = false,
|
|
|
|
dropdownItems = [],
|
|
|
|
}: HeaderProps) => {
|
|
|
|
const { t } = useTranslation("user-federation");
|
2023-01-12 08:47:26 +00:00
|
|
|
const { id } = useParams<Partial<CustomUserFederationRouteParams>>();
|
2022-08-16 13:09:14 +00:00
|
|
|
const navigate = useNavigate();
|
2022-01-05 17:06:53 +00:00
|
|
|
|
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
const { realm } = useRealm();
|
|
|
|
|
|
|
|
const { control, setValue } = useFormContext();
|
|
|
|
|
|
|
|
const [toggleDisableDialog, DisableConfirm] = useConfirmDialog({
|
|
|
|
titleKey: "user-federation:userFedDisableConfirmTitle",
|
|
|
|
messageKey: "user-federation:userFedDisableConfirm",
|
|
|
|
continueButtonLabel: "common:disable",
|
|
|
|
onConfirm: () => {
|
|
|
|
setValue("config.enabled[0]", "false");
|
|
|
|
save();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: "user-federation:userFedDeleteConfirmTitle",
|
|
|
|
messageKey: "user-federation:userFedDeleteConfirm",
|
|
|
|
continueButtonLabel: "common:delete",
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
await adminClient.components.del({ id: id! });
|
|
|
|
addAlert(t("userFedDeletedSuccess"), AlertVariant.success);
|
2022-08-16 13:09:14 +00:00
|
|
|
navigate(toUserFederation({ realm }), { replace: true });
|
2022-01-05 17:06:53 +00:00
|
|
|
} catch (error) {
|
|
|
|
addError("user-federation:userFedDeleteError", error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<DisableConfirm />
|
|
|
|
<DeleteConfirm />
|
|
|
|
<Controller
|
2023-06-19 06:59:45 +00:00
|
|
|
name="config.enabled"
|
|
|
|
defaultValue={["true"]}
|
2022-01-05 17:06:53 +00:00
|
|
|
control={control}
|
2023-01-26 09:31:07 +00:00
|
|
|
render={({ field }) =>
|
2022-01-05 17:06:53 +00:00
|
|
|
!id ? (
|
|
|
|
<ViewHeader
|
|
|
|
titleKey={t("addProvider", {
|
|
|
|
provider: provider,
|
|
|
|
count: 1,
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<ViewHeader
|
|
|
|
divider={!noDivider}
|
|
|
|
titleKey={provider}
|
|
|
|
dropdownItems={[
|
|
|
|
...dropdownItems,
|
|
|
|
<DropdownItem
|
|
|
|
key="delete"
|
|
|
|
onClick={() => toggleDeleteDialog()}
|
|
|
|
data-testid="delete-cmd"
|
|
|
|
>
|
|
|
|
{t("deleteProvider")}
|
|
|
|
</DropdownItem>,
|
|
|
|
]}
|
2023-06-19 06:59:45 +00:00
|
|
|
isEnabled={field.value?.[0] === "true"}
|
2022-01-05 17:06:53 +00:00
|
|
|
onToggle={(value) => {
|
|
|
|
if (!value) {
|
|
|
|
toggleDisableDialog();
|
|
|
|
} else {
|
2023-06-19 06:59:45 +00:00
|
|
|
field.onChange([value.toString()]);
|
2022-01-05 17:06:53 +00:00
|
|
|
save();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|