2021-01-26 19:39:01 +00:00
|
|
|
import React, { useEffect } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2021-08-09 08:47:34 +00:00
|
|
|
import { FormProvider, useForm } from "react-hook-form";
|
2021-01-26 19:39:01 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
Form,
|
|
|
|
Modal,
|
|
|
|
ModalVariant,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
|
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2021-08-09 08:47:34 +00:00
|
|
|
import { NameDescription } from "./form/NameDescription";
|
2021-01-26 19:39:01 +00:00
|
|
|
|
|
|
|
type DuplicateFlowModalProps = {
|
|
|
|
name: string;
|
|
|
|
description: string;
|
|
|
|
toggleDialog: () => void;
|
|
|
|
onComplete: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const DuplicateFlowModal = ({
|
|
|
|
name,
|
|
|
|
description,
|
|
|
|
toggleDialog,
|
|
|
|
onComplete,
|
|
|
|
}: DuplicateFlowModalProps) => {
|
|
|
|
const { t } = useTranslation("authentication");
|
2021-08-09 08:47:34 +00:00
|
|
|
const form = useForm({
|
2021-01-26 19:39:01 +00:00
|
|
|
shouldUnregister: false,
|
|
|
|
});
|
2021-08-09 08:47:34 +00:00
|
|
|
const { setValue, trigger, getValues } = form;
|
2021-01-26 19:39:01 +00:00
|
|
|
const adminClient = useAdminClient();
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2021-01-26 19:39:01 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setValue("description", description);
|
2021-10-14 14:54:07 +00:00
|
|
|
setValue("alias", t("copyOf", { name }));
|
2021-01-26 19:39:01 +00:00
|
|
|
}, [name, description, setValue]);
|
|
|
|
|
|
|
|
const save = async () => {
|
|
|
|
await trigger();
|
|
|
|
const form = getValues();
|
|
|
|
try {
|
|
|
|
await adminClient.authenticationManagement.copyFlow({
|
|
|
|
flow: name,
|
2021-08-25 12:33:57 +00:00
|
|
|
newName: form.alias,
|
2021-01-26 19:39:01 +00:00
|
|
|
});
|
|
|
|
if (form.description !== description) {
|
|
|
|
const newFlow = (
|
|
|
|
await adminClient.authenticationManagement.getFlows()
|
2021-08-25 12:33:57 +00:00
|
|
|
).find((flow) => flow.alias === form.alias)!;
|
2021-01-26 19:39:01 +00:00
|
|
|
|
|
|
|
newFlow.description = form.description;
|
|
|
|
await adminClient.authenticationManagement.updateFlow(
|
|
|
|
{ flowId: newFlow.id! },
|
|
|
|
newFlow
|
|
|
|
);
|
|
|
|
}
|
|
|
|
addAlert(t("copyFlowSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("authentication:copyFlowError", error);
|
2021-01-26 19:39:01 +00:00
|
|
|
}
|
|
|
|
onComplete();
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
title={t("duplicateFlow")}
|
|
|
|
isOpen={true}
|
|
|
|
onClose={toggleDialog}
|
|
|
|
variant={ModalVariant.small}
|
|
|
|
actions={[
|
2021-08-09 08:47:34 +00:00
|
|
|
<Button
|
|
|
|
id="modal-confirm"
|
|
|
|
key="confirm"
|
|
|
|
onClick={save}
|
|
|
|
data-testid="confirm"
|
|
|
|
>
|
|
|
|
{t("duplicate")}
|
2021-01-26 19:39:01 +00:00
|
|
|
</Button>,
|
|
|
|
<Button
|
2021-08-09 08:47:34 +00:00
|
|
|
data-testid="cancel"
|
2021-01-26 19:39:01 +00:00
|
|
|
id="modal-cancel"
|
|
|
|
key="cancel"
|
2021-04-15 10:52:34 +00:00
|
|
|
variant={ButtonVariant.link}
|
2021-01-26 19:39:01 +00:00
|
|
|
onClick={() => {
|
|
|
|
toggleDialog();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>,
|
|
|
|
]}
|
|
|
|
>
|
2021-08-09 08:47:34 +00:00
|
|
|
<FormProvider {...form}>
|
|
|
|
<Form isHorizontal>
|
|
|
|
<NameDescription />
|
|
|
|
</Form>
|
|
|
|
</FormProvider>
|
2021-01-26 19:39:01 +00:00
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|