diff --git a/src/Controller/NewsletterController.php b/src/Controller/NewsletterController.php index e314e48..79cad6e 100644 --- a/src/Controller/NewsletterController.php +++ b/src/Controller/NewsletterController.php @@ -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; @@ -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); } } diff --git a/src/Resources/public/bitbag-mailchimp-plugin-newsletter.js b/src/Resources/public/bitbag-mailchimp-plugin-newsletter.js index 4d423ef..9f4832c 100644 --- a/src/Resources/public/bitbag-mailchimp-plugin-newsletter.js +++ b/src/Resources/public/bitbag-mailchimp-plugin-newsletter.js @@ -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(''); @@ -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 = ''; diff --git a/src/Resources/translations/messages.en.yml b/src/Resources/translations/messages.en.yml index 0c97339..bb88e15 100644 --- a/src/Resources/translations/messages.en.yml +++ b/src/Resources/translations/messages.en.yml @@ -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. diff --git a/src/Resources/translations/messages.fr.yml b/src/Resources/translations/messages.fr.yml index 0a70c01..4c3569a 100644 --- a/src/Resources/translations/messages.fr.yml +++ b/src/Resources/translations/messages.fr.yml @@ -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. diff --git a/src/Resources/translations/messages.nl.yml b/src/Resources/translations/messages.nl.yml index f83bb52..68b7fc2 100644 --- a/src/Resources/translations/messages.nl.yml +++ b/src/Resources/translations/messages.nl.yml @@ -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. diff --git a/src/Resources/translations/messages.pl.yml b/src/Resources/translations/messages.pl.yml index b2caed9..aaaeaa2 100644 --- a/src/Resources/translations/messages.pl.yml +++ b/src/Resources/translations/messages.pl.yml @@ -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.