-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(feedback): Option to make name and email required #9587
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
Conversation
size-limit report 📦
|
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.
This looks great, I think we just tweak the error handling logic a bit now that we have up to 3 required fields to check
// Simple validation for now, just check for non-empty message if name required | ||
if (options.isNameRequired && !feedback.name) { | ||
dialog.showError('Please enter in a name before submitting!'); | ||
return; | ||
} | ||
|
||
// Simple validation for now, just check for non-empty message if email required | ||
if (options.isEmailRequired && !feedback.email) { | ||
dialog.showError('Please enter in an email before submitting!'); | ||
return; | ||
} | ||
|
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 may want to check all fields at once instead of sequentially, otherwise user may end up having to fill out each field individually if they were to follow only the error message.
We could display a message like... Please enter in the following required fields: name, description
(we may want to grab the field names from options object using the labels).
it('only submits feedback successfully when all required fields are filled', async () => { | ||
const onSubmitSuccess = jest.fn(() => {}); | ||
const { shadow, widget } = createShadowAndWidget({ | ||
isNameRequired: true, | ||
isEmailRequired: true, | ||
onSubmitSuccess, | ||
}); | ||
|
||
(sendFeedbackRequest as jest.Mock).mockImplementation(() => { | ||
return true; | ||
}); | ||
widget.actor?.el?.dispatchEvent(new Event('click')); | ||
|
||
const nameEl = widget.dialog?.el?.querySelector('[name="name"]') as HTMLInputElement; | ||
const emailEl = widget.dialog?.el?.querySelector('[name="email"]') as HTMLInputElement; | ||
const messageEl = widget.dialog?.el?.querySelector('[name="message"]') as HTMLTextAreaElement; | ||
|
||
nameEl.value = ''; | ||
emailEl.value = ''; | ||
messageEl.value = ''; | ||
|
||
widget.dialog?.el?.querySelector('form')?.dispatchEvent(new Event('submit')); | ||
expect(sendFeedbackRequest).toHaveBeenCalledTimes(0); | ||
|
||
// sendFeedbackRequest is async | ||
await flushPromises(); | ||
expect(onSubmitSuccess).toHaveBeenCalledTimes(0); | ||
|
||
nameEl.value = ''; | ||
emailEl.value = ''; | ||
messageEl.value = 'My feedback'; | ||
|
||
widget.dialog?.el?.querySelector('form')?.dispatchEvent(new Event('submit')); | ||
expect(sendFeedbackRequest).toHaveBeenCalledTimes(0); | ||
|
||
// sendFeedbackRequest is async | ||
await flushPromises(); | ||
expect(onSubmitSuccess).toHaveBeenCalledTimes(0); | ||
|
||
nameEl.value = 'Jane Doe'; | ||
emailEl.value = '[email protected]'; | ||
messageEl.value = 'My feedback'; | ||
|
||
widget.dialog?.el?.querySelector('form')?.dispatchEvent(new Event('submit')); | ||
expect(sendFeedbackRequest).toHaveBeenCalledWith({ | ||
feedback: { | ||
name: 'Jane Doe', | ||
email: '[email protected]', | ||
message: 'My feedback', | ||
url: 'http://localhost/', | ||
replay_id: undefined, | ||
source: 'widget', | ||
}, | ||
}); | ||
|
||
// sendFeedbackRequest is async | ||
await flushPromises(); | ||
expect(onSubmitSuccess).toHaveBeenCalledTimes(1); | ||
|
||
expect(widget.dialog).toBeUndefined(); | ||
expect(shadow.querySelector('.success-message')?.textContent).toBe(SUCCESS_MESSAGE_TEXT); | ||
|
||
jest.runAllTimers(); | ||
expect(shadow.querySelector('.success-message')).toBeNull(); | ||
}); |
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.
Nice 🎉
@@ -99,7 +99,7 @@ export function createWidget({ | |||
if (!feedback.message) { | |||
emptyField.push(options.messageLabel); | |||
} | |||
if (!emptyField.length) { | |||
if (emptyField.length != 0) { |
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.
Ah good catch, I think maybe > 0
would read slightly more straightforward
Adds option to make 'name' and 'email' required. The field isAnonymous takes priority over isNameRequired and isEmailRequired. The fields isNameRequired and isEmailRequired take priority over showName and showEmail.
Closes #9479