save and test connection

This commit is contained in:
jenny-s51 2021-06-21 10:46:05 -04:00
parent fd1466c0d6
commit cc336ca16b

View file

@ -53,6 +53,7 @@ export const RealmSettingsEmailTab = ({
errors, errors,
setValue, setValue,
reset: resetForm, reset: resetForm,
getValues,
} = useForm<RealmRepresentation>(); } = useForm<RealmRepresentation>();
const userForm = useForm<UserRepresentation>({ mode: "onChange" }); const userForm = useForm<UserRepresentation>({ mode: "onChange" });
@ -83,12 +84,25 @@ export const RealmSettingsEmailTab = ({
} }
}; };
const saveAndTestEmail = async (user: UserRepresentation) => { const saveAndTestEmail = async (email?: UserRepresentation) => {
await adminClient.users.update({ id: whoAmI.getUserId() }, user); if (email) {
const updated = await adminClient.users.findOne({ id: whoAmI.getUserId() }); await adminClient.users.update({ id: whoAmI.getUserId() }, email);
const updated = await adminClient.users.findOne({
id: whoAmI.getUserId(),
});
setCurrentUser(updated); setCurrentUser(updated);
handleModalToggle();
await save(getValues());
testConnection(); testConnection();
} else {
const user = await adminClient.users.findOne({ id: whoAmI.getUserId() });
if (!user.email) {
handleModalToggle();
} else {
await save(getValues());
testConnection();
}
}
}; };
const reset = () => { const reset = () => {
@ -110,7 +124,7 @@ export const RealmSettingsEmailTab = ({
Accept: "application/json", Accept: "application/json",
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify(realm.smtpServer! as BodyInit), body: JSON.stringify(getValues()["smtpServer"] as BodyInit),
} }
); );
response.ok response.ok
@ -124,8 +138,9 @@ export const RealmSettingsEmailTab = ({
<AddUserEmailModal <AddUserEmailModal
handleModalToggle={handleModalToggle} handleModalToggle={handleModalToggle}
testConnection={testConnection} testConnection={testConnection}
save={(user) => { save={(email) => {
saveAndTestEmail(user!); saveAndTestEmail(email!);
handleModalToggle();
}} }}
form={userForm} form={userForm}
user={currentUser!} user={currentUser!}
@ -385,11 +400,7 @@ export const RealmSettingsEmailTab = ({
</Button> </Button>
<Button <Button
variant="secondary" variant="secondary"
onClick={ onClick={() => saveAndTestEmail()}
currentUser?.email
? () => testConnection()
: handleModalToggle
}
data-testid="test-connection-button" data-testid="test-connection-button"
> >
{t("realm-settings:testConnection")} {t("realm-settings:testConnection")}