Skip to content

Commit 82f5440

Browse files
authored
fix(core): show success message when a subscribed user resubscribes (#2795)
* fix(core): show success message when a subscribed user resubscribes * chore: remove changset * fix: add additional comments * fix: update test name
1 parent 75b0a1a commit 82f5440

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

.changeset/tall-walls-tan.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,8 @@ Add the following translation keys to your locale files (e.g., `messages/en.json
7373
"title": "Sign up for our newsletter",
7474
"placeholder": "Enter your email",
7575
"description": "Stay up to date with the latest news and offers from our store.",
76-
"success": "You have been subscribed to our newsletter.",
76+
"subscribedToNewsletter": "You have been subscribed to our newsletter.",
7777
"Errors": {
78-
"subcriberAlreadyExists": "You are already subscribed to our newsletter.",
7978
"invalidEmail": "Please enter a valid email address.",
8079
"somethingWentWrong": "Something went wrong. Please try again later."
8180
}

core/components/subscribe/_actions/subscribe.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,25 @@ export const subscribe = async (
5757

5858
const errors = response.data.newsletter.subscribe.errors;
5959

60-
if (!errors.length) {
61-
return { lastResult: submission.reply({ resetForm: true }), successMessage: t('success') };
60+
const subcriberAlreadyExists = errors.some(
61+
({ __typename }) => __typename === 'CreateSubscriberAlreadyExistsError',
62+
);
63+
64+
// If there are no errors or the subscriber already exists, we want to reset the form and show the success message
65+
// This is for privacy reasons, we don't want to show the error message to the user if they are already subscribed
66+
if (!errors.length || subcriberAlreadyExists) {
67+
return {
68+
lastResult: submission.reply(),
69+
successMessage: t('subscribedToNewsletter'),
70+
};
6271
}
6372

6473
if (errors.length > 0) {
74+
// If there are other errors, we want to show the error message to the user
6575
return {
6676
lastResult: submission.reply({
6777
formErrors: errors.map(({ __typename }) => {
6878
switch (__typename) {
69-
case 'CreateSubscriberAlreadyExistsError':
70-
return t('Errors.subcriberAlreadyExists');
71-
7279
case 'CreateSubscriberEmailInvalidError':
7380
return t('Errors.invalidEmail');
7481

core/messages/en.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,8 @@
504504
"title": "Sign up for our newsletter",
505505
"placeholder": "Enter your email",
506506
"description": "Stay up to date with the latest news and offers from our store.",
507-
"success": "You have been subscribed to our newsletter.",
507+
"subscribedToNewsletter": "You have been subscribed to our newsletter.",
508508
"Errors": {
509-
"subcriberAlreadyExists": "You are already subscribed to our newsletter.",
510509
"invalidEmail": "Please enter a valid email address.",
511510
"somethingWentWrong": "Something went wrong. Please try again later."
512511
}

core/tests/ui/e2e/subscribe.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ test(
2424
await submitButton.click();
2525
await page.waitForLoadState('networkidle');
2626

27-
await expect(page.getByText(t('success'))).toBeVisible();
27+
await expect(page.getByText(t('subscribedToNewsletter'))).toBeVisible();
2828

2929
subscribe.trackSubscription(email);
3030
},
3131
);
3232

33-
test('Shows error when user tries to subscribe again with the same email', async ({
33+
test('Shows success message when user tries to subscribe again with the same email', async ({
3434
page,
3535
subscribe,
3636
}) => {
@@ -50,14 +50,14 @@ test('Shows error when user tries to subscribe again with the same email', async
5050
await submitButton.click();
5151
await page.waitForLoadState('networkidle');
5252

53-
await expect(page.getByText(t('success'))).toBeVisible();
53+
await expect(page.getByText(t('subscribedToNewsletter'))).toBeVisible();
5454

5555
// Try to subscribe again with the same email
5656
await emailInput.fill(email);
5757
await submitButton.click();
5858
await page.waitForLoadState('networkidle');
5959

60-
await expect(page.getByText(t('Errors.subcriberAlreadyExists'))).toBeVisible();
60+
await expect(page.getByText(t('subscribedToNewsletter'))).toBeVisible();
6161

6262
// Track that we attempted to subscribe this email
6363
subscribe.trackSubscription(email);

0 commit comments

Comments
 (0)