feat(feedback): Option to make name and email required#9587
Merged
Conversation
Contributor
size-limit report 📦
|
billyvg
reviewed
Nov 16, 2023
billyvg
reviewed
Nov 17, 2023
Member
billyvg
left a comment
There was a problem hiding this comment.
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
Comment on lines
+97
to
+108
| // 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; | ||
| } | ||
|
|
Member
There was a problem hiding this comment.
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).
Comment on lines
+174
to
+238
| 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 = 'jane@example.com'; | ||
| messageEl.value = 'My feedback'; | ||
|
|
||
| widget.dialog?.el?.querySelector('form')?.dispatchEvent(new Event('submit')); | ||
| expect(sendFeedbackRequest).toHaveBeenCalledWith({ | ||
| feedback: { | ||
| name: 'Jane Doe', | ||
| email: 'jane@example.com', | ||
| 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(); | ||
| }); |
billyvg
reviewed
Nov 17, 2023
billyvg
approved these changes
Nov 17, 2023
billyvg
reviewed
Nov 20, 2023
| emptyField.push(options.messageLabel); | ||
| } | ||
| if (!emptyField.length) { | ||
| if (emptyField.length != 0) { |
Member
There was a problem hiding this comment.
Ah good catch, I think maybe > 0 would read slightly more straightforward
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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