2021-06-10 20:00:14 +00:00
|
|
|
import React from "react";
|
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
Form,
|
|
|
|
FormGroup,
|
|
|
|
Modal,
|
|
|
|
ModalVariant,
|
|
|
|
TextContent,
|
|
|
|
TextInput,
|
|
|
|
ValidatedOptions,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2021-10-14 21:08:42 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
2021-06-10 20:00:14 +00:00
|
|
|
|
|
|
|
import { emailRegexPattern } from "../util";
|
2021-10-14 21:08:42 +00:00
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
|
|
|
import { useWhoAmI } from "../context/whoami/WhoAmI";
|
|
|
|
import type { EmailRegistrationCallback } from "./EmailTab";
|
2021-06-10 20:00:14 +00:00
|
|
|
|
|
|
|
type AddUserEmailModalProps = {
|
2021-10-14 21:08:42 +00:00
|
|
|
callback: EmailRegistrationCallback;
|
2021-06-10 20:00:14 +00:00
|
|
|
};
|
|
|
|
|
2021-10-14 21:08:42 +00:00
|
|
|
type AddUserEmailForm = {
|
|
|
|
email: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const AddUserEmailModal = ({ callback }: AddUserEmailModalProps) => {
|
2021-06-10 20:00:14 +00:00
|
|
|
const { t } = useTranslation("groups");
|
2021-10-14 21:08:42 +00:00
|
|
|
const adminClient = useAdminClient();
|
|
|
|
const { whoAmI } = useWhoAmI();
|
|
|
|
const { register, errors, handleSubmit, watch } = useForm<AddUserEmailForm>({
|
|
|
|
defaultValues: { email: "" },
|
|
|
|
});
|
2021-06-10 20:00:14 +00:00
|
|
|
|
|
|
|
const watchEmailInput = watch("email", "");
|
2021-10-14 21:08:42 +00:00
|
|
|
const cancel = () => callback(false);
|
|
|
|
const proceed = () => callback(true);
|
|
|
|
|
|
|
|
const save = async (formData: AddUserEmailForm) => {
|
|
|
|
await adminClient.users.update({ id: whoAmI.getUserId() }, formData);
|
|
|
|
proceed();
|
|
|
|
};
|
2021-06-10 20:00:14 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
variant={ModalVariant.small}
|
|
|
|
title={t("realm-settings:provideEmailTitle")}
|
|
|
|
isOpen={true}
|
2021-10-14 21:08:42 +00:00
|
|
|
onClose={cancel}
|
2021-06-10 20:00:14 +00:00
|
|
|
actions={[
|
|
|
|
<Button
|
|
|
|
data-testid="modal-test-connection-button"
|
|
|
|
key="confirm"
|
|
|
|
variant="primary"
|
|
|
|
type="submit"
|
|
|
|
form="email-form"
|
|
|
|
isDisabled={!watchEmailInput}
|
|
|
|
>
|
2021-10-05 10:31:39 +00:00
|
|
|
{t("common:testConnection")}
|
2021-06-10 20:00:14 +00:00
|
|
|
</Button>,
|
|
|
|
<Button
|
|
|
|
id="modal-cancel"
|
2022-02-02 11:44:52 +00:00
|
|
|
data-testid="cancel"
|
2021-06-10 20:00:14 +00:00
|
|
|
key="cancel"
|
|
|
|
variant={ButtonVariant.link}
|
2021-10-14 21:08:42 +00:00
|
|
|
onClick={cancel}
|
2021-06-10 20:00:14 +00:00
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>,
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<TextContent className="kc-provide-email-text">
|
|
|
|
{t("realm-settings:provideEmail")}
|
|
|
|
</TextContent>
|
|
|
|
<Form id="email-form" isHorizontal onSubmit={handleSubmit(save)}>
|
|
|
|
<FormGroup
|
|
|
|
className="kc-email-form-group"
|
|
|
|
name="add-email-address"
|
|
|
|
fieldId="email-id"
|
|
|
|
helperTextInvalid={t("users:emailInvalid")}
|
|
|
|
validated={
|
|
|
|
errors.email ? ValidatedOptions.error : ValidatedOptions.default
|
|
|
|
}
|
|
|
|
isRequired
|
|
|
|
>
|
|
|
|
<TextInput
|
|
|
|
data-testid="email-address-input"
|
|
|
|
ref={register({ required: true, pattern: emailRegexPattern })}
|
|
|
|
autoFocus
|
|
|
|
type="text"
|
|
|
|
id="add-email"
|
|
|
|
name="email"
|
|
|
|
validated={
|
|
|
|
errors.email ? ValidatedOptions.error : ValidatedOptions.default
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|