Skip to content

Commit 5acf0f0

Browse files
committed
Use ajax in the dialog
1 parent 40d7ae5 commit 5acf0f0

File tree

7 files changed

+97
-4
lines changed

7 files changed

+97
-4
lines changed

src/Controller/RestockNotificationRequestAction.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Setono\SyliusRestockNotificationPlugin\Form\Type\RestockNotificationShopRequestType;
1111
use Setono\SyliusRestockNotificationPlugin\Model\RestockNotificationRequestInterface;
1212
use Symfony\Component\Form\FormFactoryInterface;
13+
use Symfony\Component\HttpFoundation\JsonResponse;
1314
use Symfony\Component\HttpFoundation\RedirectResponse;
1415
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpFoundation\Session\Session;
@@ -29,9 +30,12 @@ public function __construct(
2930
$this->managerRegistry = $managerRegistry;
3031
}
3132

32-
public function __invoke(Request $request): RedirectResponse
33+
public function __invoke(Request $request): RedirectResponse|JsonResponse
3334
{
34-
$form = $this->formFactory->create(RestockNotificationShopRequestType::class, $this->restockNotificationRequestFactory->createWithChannelAndLocaleContext());
35+
$form = $this->formFactory->create(
36+
RestockNotificationShopRequestType::class,
37+
$this->restockNotificationRequestFactory->createWithChannelAndLocaleContext(),
38+
);
3539
$form->handleRequest($request);
3640

3741
if ($form->isSubmitted() && $form->isValid()) {
@@ -43,13 +47,31 @@ public function __invoke(Request $request): RedirectResponse
4347
$manager->persist($data);
4448
$manager->flush();
4549

50+
if ($request->isXmlHttpRequest()) {
51+
return new JsonResponse([
52+
'success' => true,
53+
]);
54+
}
55+
4656
self::addFlash($request, 'success', 'setono_sylius_restock_notification.restock_notification_request_created');
4757

4858
return self::createRedirect($request, $this->urlGenerator->generate('sylius_shop_product_show', [
4959
'slug' => $data->getProductVariant()?->getProduct()?->getSlug(),
5060
]));
5161
}
5262

63+
if ($request->isXmlHttpRequest()) {
64+
$errors = [];
65+
foreach ($form->getErrors(true) as $error) {
66+
$errors[] = $error->getMessage();
67+
}
68+
69+
return new JsonResponse([
70+
'success' => false,
71+
'errors' => $errors,
72+
]);
73+
}
74+
5375
self::addFlash($request, 'error', 'setono_sylius_restock_notification.restock_notification_request_failed');
5476

5577
return self::createRedirect($request, $this->urlGenerator->generate('sylius_shop_homepage'));

src/Resources/config/validation/RestockNotificationRequest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<value>email</value>
1212
</option>
1313
<option name="groups">setono_sylius_restock_notification</option>
14+
<option name="message">setono_sylius_restock_notification.restock_notification_request.unique</option>
1415
</constraint>
1516
<property name="channel">
1617
<constraint name="NotBlank">

src/Resources/public/css/styles.css

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
/**
22
* Style the <dialog>
33
*/
4-
dialog.ssrn-dialog {
4+
.ssrn-dialog {
55
border: 1px solid #ccc;
66
border-radius: 5px;
77
padding: 20px;
88
position: relative;
9+
max-width: 400px;
910
}
1011

11-
dialog.ssrn-dialog button.close {
12+
.ssrn-dialog::backdrop {
13+
background-color: rgba(0, 0, 0, 0.75);
14+
}
15+
16+
.ssrn-dialog button.close {
1217
position: absolute;
1318
top: 10px;
1419
right: 10px;
@@ -21,6 +26,28 @@ dialog.ssrn-dialog button.close {
2126
outline: none;
2227
}
2328

29+
.ssrn-dialog.success form {
30+
display: none;
31+
}
32+
33+
.ssrn-dialog.success .ssrn-success-message {
34+
display: block !important;
35+
}
36+
37+
.ssrn-dialog.error .ssrn-error-message {
38+
display: block !important;
39+
}
40+
41+
.ssrn-success-message {
42+
color: green;
43+
display: none;
44+
}
45+
46+
.ssrn-error-message {
47+
color: red;
48+
display: none;
49+
}
50+
2451
dialog.ssrn-dialog h4 {
2552
font-size: 20px;
2653
margin: 0 0 10px;

src/Resources/public/js/dialog.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,42 @@ export default class Dialog {
3131
*/
3232
show(product) {
3333
const element = this.#getElement();
34+
35+
element.querySelector('form').addEventListener('submit', (event) => {
36+
event.preventDefault();
37+
38+
/** @type {HTMLFormElement} */
39+
const form = event.target;
40+
41+
const formData = new FormData(form);
42+
43+
fetch(form.action, {
44+
method: form.method || 'POST',
45+
body: formData,
46+
headers: {
47+
'X-Requested-With': 'XMLHttpRequest',
48+
},
49+
})
50+
.then((response) => {
51+
if (!response.ok) {
52+
throw new Error(`HTTP error! status: ${response.status}`);
53+
}
54+
return response.json();
55+
})
56+
.then((result) => {
57+
if(result.success) {
58+
element.classList.add('success');
59+
} else {
60+
element.classList.add('error');
61+
element.querySelector('.ssrn-error-message').innerHTML = result.errors.join('<br>');
62+
}
63+
})
64+
.catch((error) => {
65+
element.classList.add('error');
66+
console.error(error.message);
67+
});
68+
});
69+
3470
element.querySelector('.product-variant').value = product.code;
3571
element.showModal();
3672
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
setono_sylius_restock_notification:
2+
restock_notification_request:
3+
unique: You already subscribed to get a notification when this product is back in stock.

src/Resources/views/shop/_dialog.html.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{{ include('@SetonoSyliusRestockNotificationPlugin/shop/dialog/_close_button.html.twig') }}
66
{{ include('@SetonoSyliusRestockNotificationPlugin/shop/dialog/_heading.html.twig') }}
77
{{ include('@SetonoSyliusRestockNotificationPlugin/shop/dialog/_body.html.twig') }}
8+
{{ include('@SetonoSyliusRestockNotificationPlugin/shop/dialog/_messages.html.twig') }}
89
{{ include('@SetonoSyliusRestockNotificationPlugin/shop/dialog/_form.html.twig') }}
910
</dialog>
1011
</template>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{# todo translate #}
2+
<div class="ssrn-success-message">You will receive an email when the product is back in stock</div>
3+
<div class="ssrn-error-message">Something went wrong. Please try again.</div>

0 commit comments

Comments
 (0)