Skip to content

fix[angular-gen2]: Better error handling on form submissions #4031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions packages/sdks/src/blocks/form/form/form.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,21 @@ export default function FormComponent(props: FormProps) {

state.formState = 'sending';

if (
props.sendSubmissionsTo === 'email' &&
(props.sendSubmissionsToEmail === '[email protected]' ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(props.sendSubmissionsToEmail === '[email protected]' ||
const isInvalidEmailConfig = () =>
props.sendSubmissionsTo === 'email' &&
(!props.sendSubmissionsToEmail || props.sendSubmissionsToEmail === '[email protected]');

we can create a function which can validate input emails

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yash-builder This is actually not the email which comes from the form input. This is the email that users set when creating the form. Since this code is not repeating, I'll let it be

props.sendSubmissionsToEmail === '')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if sendSubmissionsToEmail is null or undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handled it now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
props.sendSubmissionsToEmail === '')
!props.sendSubmissionsToEmail)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pushed changes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asking out of curiosity
Are we also responsible to validate if emails are temp emails or an actual email?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we are not doing that here. The API will send error response

) {
const message =
'SubmissionsToEmail is required when sendSubmissionsTo is set to email';
console.error(message);
state.formState = 'error';
state.mergeNewRootState({
formErrorMessage: message,
});
return;
}

const formUrl = `${
getEnv() === 'dev' ? 'http://localhost:5000' : 'https://builder.io'
}/api/v1/form-submit?apiKey=${props.builderContext.value.apiKey}&to=${btoa(
Expand Down Expand Up @@ -214,21 +229,37 @@ export default function FormComponent(props: FormProps) {
body = await res.text();
}

if (!res.ok && props.errorMessagePath) {
/* TODO: allow supplying an error formatter function */
let message = get(body, props.errorMessagePath);
if (!res.ok) {
const submitErrorEvent = new CustomEvent('submit:error', {
detail: {
error: body,
status: res.status,
},
});

if (message) {
if (typeof message !== 'string') {
/* TODO: ideally convert json to yaml so it woul dbe like
error: - email has been taken */
message = JSON.stringify(message);
if (formRef?.nativeElement) {
formRef?.nativeElement.dispatchEvent(submitErrorEvent);
if (submitErrorEvent.defaultPrevented) {
return;
}
state.formErrorMessage = message;
state.mergeNewRootState({
formErrorMessage: message,
});
}
state.responseData = body;
state.formState = 'error';

let message = props.errorMessagePath

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we create a small function to get the message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is not repeated anywhere, why do we need a function.

? get(body, props.errorMessagePath)
: body.message || body.error || body;

if (typeof message !== 'string') {
message = JSON.stringify(message);
}

state.formErrorMessage = message;
state.mergeNewRootState({
formErrorMessage: message,
});

return;
}

state.responseData = body;
Expand Down
Loading