Skip to content

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

Merged
merged 7 commits into from
Nov 20, 2023

Conversation

c298lee
Copy link
Contributor

@c298lee c298lee commented Nov 16, 2023

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

@c298lee c298lee requested a review from a team November 16, 2023 21:59
Copy link
Contributor

github-actions bot commented Nov 16, 2023

size-limit report 📦

Path Size
@sentry/browser (incl. Tracing, Replay) - Webpack (gzipped) 65.53 KB (0%)
@sentry/browser (incl. Tracing, Replay) - Webpack with treeshaking flags (gzipped) 55.67 KB (0%)
@sentry/browser (incl. Tracing) - Webpack (gzipped) 31.01 KB (0%)
@sentry/browser - Webpack (gzipped) 21.32 KB (0%)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (gzipped) 61.99 KB (0%)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (gzipped) 29.13 KB (0%)
@sentry/browser - ES6 CDN Bundle (gzipped) 21.26 KB (0%)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (minified & uncompressed) 195.53 KB (0%)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (minified & uncompressed) 88.38 KB (0%)
@sentry/browser - ES6 CDN Bundle (minified & uncompressed) 63.34 KB (0%)
@sentry/browser (incl. Tracing) - ES5 CDN Bundle (gzipped) 31.85 KB (0%)
@sentry/react (incl. Tracing, Replay) - Webpack (gzipped) 65.84 KB (0%)
@sentry/react - Webpack (gzipped) 21.36 KB (0%)
@sentry/nextjs Client (incl. Tracing, Replay) - Webpack (gzipped) 82.58 KB (0%)
@sentry/nextjs Client - Webpack (gzipped) 48.15 KB (0%)
@sentry-internal/feedback - Webpack (gzipped) 16.21 KB (+0.79% 🔺)

@c298lee c298lee requested a review from billyvg November 17, 2023 20:26
Copy link
Member

@billyvg billyvg left a 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

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;
}

Copy link
Member

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).

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 = '[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();
});
Copy link
Member

Choose a reason for hiding this comment

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

Nice 🎉

@c298lee c298lee requested a review from billyvg November 17, 2023 21:31
@c298lee c298lee requested a review from billyvg November 17, 2023 21:38
@c298lee c298lee merged commit aed4bf0 into develop Nov 20, 2023
@c298lee c298lee deleted the feedback-name-and-email-required branch November 20, 2023 15:36
@@ -99,7 +99,7 @@ export function createWidget({
if (!feedback.message) {
emptyField.push(options.messageLabel);
}
if (!emptyField.length) {
if (emptyField.length != 0) {
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add options to make name and email fields required
2 participants