Skip to content

Commit

Permalink
Fix issues related to submit form, when handling 400 bad request from…
Browse files Browse the repository at this point in the history
… MailChimp
  • Loading branch information
senghe committed Apr 30, 2023
1 parent 8efe0ea commit 4a63c76
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 15 deletions.
36 changes: 22 additions & 14 deletions src/Controller/NewsletterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManager;
use Symfony\Contracts\Translation\TranslatorInterface;
Expand Down Expand Up @@ -57,24 +58,31 @@ public function subscribeAction(Request $request): JsonResponse
], Response::HTTP_BAD_REQUEST);
}

$errors = $this->validator->validate($email);
try {
$errors = $this->validator->validate($email);

if (!$this->tokenManager->isTokenValid(new CsrfToken('newsletter', $token))) {
$errors[] = $this->translator->trans('bitbag_sylius_mailchimp_plugin.ui.invalid_csrf_token');
}
if (!$this->tokenManager->isTokenValid(new CsrfToken('newsletter', $token))) {
$errors[] = $this->translator->trans('bitbag_sylius_mailchimp_plugin.ui.invalid_csrf_token');
}

if (count($errors) === 0) {
$this->handler->subscribe($email);

if (count($errors) === 0) {
$this->handler->subscribe($email);
return new JsonResponse([
'success' => true,
'message' => $this->translator->trans('bitbag_sylius_mailchimp_plugin.ui.subscribed_successfully'),
]);
}

return new JsonResponse([
'success' => true,
'message' => $this->translator->trans('bitbag_sylius_mailchimp_plugin.ui.subscribed_successfully'),
]);
'success' => false,
'errors' => json_encode($errors),
], Response::HTTP_BAD_REQUEST);
} catch (BadRequestHttpException $e) {
return new JsonResponse([
'success' => false,
'errors' => json_encode([$this->translator->trans('bitbag_sylius_mailchimp_plugin.ui.unexpected_error')]),
], Response::HTTP_BAD_REQUEST);
}

return new JsonResponse([
'success' => false,
'errors' => json_encode($errors),
], Response::HTTP_BAD_REQUEST);
}
}
8 changes: 7 additions & 1 deletion src/Resources/public/bitbag-mailchimp-plugin-newsletter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { $ } from 'jquery';
event.preventDefault();

var successElement = form.find('.success-element');
var input = form.find('input[type=text]');
var validationElement = form.find('.validation-element');

successElement.text('');
Expand All @@ -22,10 +23,15 @@ import { $ } from 'jquery';
.done(function (response) {
if (response.hasOwnProperty('message')) {
successElement.html(response.message);
input.val('');
}
})
.fail(function (response) {
if (response.responseJSON.hasOwnProperty('errors')) {
if (!response.responseJSON) {
var message = 'An unexpected error occurred. Please try again later.';

validationElement.text(message);
} else if (response.responseJSON.hasOwnProperty('errors')) {
var errors = $.parseJSON(response.responseJSON.errors);
var message = '';

Expand Down
1 change: 1 addition & 0 deletions src/Resources/translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ bitbag_sylius_mailchimp_plugin:
invalid_csrf_token: Submited CSRF token is invalid.
unsubscribed_successfully: You are now unsubscribed from the newsletter.
invalid_variable_type: Invalid variable type
unexpected_error: An unexpected error occurred. Please try again later.
1 change: 1 addition & 0 deletions src/Resources/translations/messages.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ bitbag_sylius_mailchimp_plugin:
your_email_address: Votre adresse email
subscribed_successfully: Vous êtes bien inscrit à notre newsletter.
invalid_csrf_token: Le jeton CSRF est invalide.
unexpected_error: Une erreur inattendue est apparue. Veuillez réessayer plus tard.
1 change: 1 addition & 0 deletions src/Resources/translations/messages.nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ bitbag_sylius_mailchimp_plugin:
your_email_address: Jouw e-mailadres
subscribed_successfully: Je bent nu geabonneerd op de nieuwsbrief.
invalid_csrf_token: Ingediende CSRF-token is ongeldig.
unexpected_error: Er is een onverwachte fout opgetreden. Probeer het later opnieuw.
1 change: 1 addition & 0 deletions src/Resources/translations/messages.pl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ bitbag_sylius_mailchimp_plugin:
your_email_address: Twój adres email
subscribed_successfully: Zostałeś zapisany do newslettera.
invalid_csrf_token: Przesłany token CSRF jest nieprawidłowy. Prosimy spróbować ponownie.
unexpected_error: Wystapił nieoczekiwany błąd. Prosimy spróbować ponownie.

0 comments on commit 4a63c76

Please sign in to comment.