Skip to content

Commit

Permalink
added test for email server edit
Browse files Browse the repository at this point in the history
  • Loading branch information
fomalhautb committed Dec 19, 2024
1 parent 09399ff commit 56dd28d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
4 changes: 2 additions & 2 deletions apps/backend/src/lib/emails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export async function sendEmailWithKnownErrorTypes(options: SendEmailOptions): P
rawError: error,
errorType: 'HOST_NOT_FOUND',
retryable: false,
message: 'The email host is not found. Please make sure the email host configuration is correct.'
message: 'Failed to connect to the email host. Please make sure the email host configuration is correct.'
});
}

Expand All @@ -120,7 +120,7 @@ export async function sendEmailWithKnownErrorTypes(options: SendEmailOptions): P
rawError: error,
errorType: 'AUTH_FAILED',
retryable: false,
message: 'The email server authentication failed. Please check your email credentials configuration.',
message: 'Failed to authenticate with the email server. Please check your email credentials configuration.',
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ function EditEmailServerDialog(props: {
}) {
const stackAdminApp = useAdminApp();
const project = stackAdminApp.useProject();
const [error, setError] = useState<string | null>(null);

return <FormDialog
trigger={props.trigger}
Expand All @@ -185,16 +186,36 @@ function EditEmailServerDialog(props: {
}
});
} else {
if (!values.host || !values.port || !values.username || !values.password || !values.senderEmail || !values.senderName) {
throwErr("Email server fields are not filled for custom SMTP server");
}

const emailConfig = {
host: values.host,
port: values.port,
username: values.username,
password: values.password,
senderEmail: values.senderEmail,
senderName: values.senderName,
};

const testResult = await stackAdminApp.sendTestEmail({
recipientEmail: values.senderEmail,
emailConfig: emailConfig,
});

if (testResult.status === 'error') {
setError(testResult.error.errorMessage);
return 'prevent-close-and-prevent-reset';
} else {
setError(null);
}

await project.update({
config: {
emailConfig: {
type: 'standard',
senderName: values.senderName!,
host: values.host!,
port: values.port!,
username: values.username!,
password: values.password!,
senderEmail: values.senderEmail!,
...emailConfig,
}
}
});
Expand Down Expand Up @@ -231,6 +252,7 @@ function EditEmailServerDialog(props: {
/>
))}
</>}
{error && <Alert variant="destructive">{error}</Alert>}
</>
)}
/>;
Expand Down
15 changes: 9 additions & 6 deletions apps/dashboard/src/components/form-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function SmartFormDialog<S extends yup.ObjectSchema<any, any, any, any>>(
export function FormDialog<F extends FieldValues>(
props: Omit<ActionDialogProps, 'children'> & {
defaultValues?: Partial<F>,
onSubmit: (values: F) => Promise<void | 'prevent-close'> | void | 'prevent-close',
onSubmit: (values: F) => Promise<void | 'prevent-close' | 'prevent-close-and-prevent-reset'> | void | 'prevent-close' | 'prevent-close-and-prevent-reset',
render: (form: ReturnType<typeof useForm<F>>) => React.ReactNode,
formSchema: yup.ObjectSchema<F>,
onFormChange?: (form: ReturnType<typeof useForm<F>>) => void,
Expand All @@ -73,11 +73,14 @@ export function FormDialog<F extends FieldValues>(
setSubmitting(true);
try {
const result = await props.onSubmit(values);
form.reset();
if (result !== 'prevent-close') {
setOpenState(false);
props.onClose?.();
props.onOpenChange?.(false);
if (result !== 'prevent-close-and-prevent-reset') {
form.reset();

if (result !== 'prevent-close') {
setOpenState(false);
props.onClose?.();
props.onOpenChange?.(false);
}
}
} finally {
setSubmitting(false);
Expand Down

0 comments on commit 56dd28d

Please sign in to comment.