-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Changes from 1 commit
8cbb4ea
d0ea341
07dc220
01cb064
8620a68
92a354e
cf36873
7e08c64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -187,6 +187,21 @@ export default function FormComponent(props: FormProps) { | |||||
|
||||||
state.formState = 'sending'; | ||||||
|
||||||
if ( | ||||||
props.sendSubmissionsTo === 'email' && | ||||||
(props.sendSubmissionsToEmail === '[email protected]' || | ||||||
props.sendSubmissionsToEmail === '') | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if sendSubmissionsToEmail is null or undefined There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handled it now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pushed changes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Asking out of curiosity There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||||||
|
@@ -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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we create a small function to get the message? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can create a function which can validate input emails
There was a problem hiding this comment.
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