diff --git a/composer.json b/composer.json index ccace32..7d79ab6 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "slinicraftet204/notifyifavail", "description": "benachrichtigt Kunden, sobald ein Produkt wieder verfügbar ist", - "version": "0.1.492", + "version": "0.1.499", "type": "shopware-platform-plugin", "license": "MIT", "authors": [ diff --git a/src/Resources/app/storefront/src/main.js b/src/Resources/app/storefront/src/main.js index a9cd913..534c774 100644 --- a/src/Resources/app/storefront/src/main.js +++ b/src/Resources/app/storefront/src/main.js @@ -1 +1,4 @@ -import './js/notify-me.js'; +import PluginManager from 'src/plugin-system/plugin.manager'; +import NotifyMe from './plugin/notify-me.plugin'; + +PluginManager.register('NotifyMe', NotifyMe, '[data-notify-me]'); diff --git a/src/Resources/public/js/notify-me.js b/src/Resources/public/js/notify-me.js deleted file mode 100644 index a8719f0..0000000 --- a/src/Resources/public/js/notify-me.js +++ /dev/null @@ -1,58 +0,0 @@ -document.addEventListener('DOMContentLoaded', function () { - console.log("✅ NotifyMe Script geladen"); - - const notifyContainer = document.getElementById('notify-me-container'); - if (!notifyContainer) { - console.error("❌ NotifyMe: Container nicht gefunden!"); - return; - } - - console.log("✅ NotifyMe: Container gefunden"); - - const notifyButton = document.querySelector('.notify-me-button'); - const submitButton = document.querySelector('.notify-me-submit'); - const emailInput = document.querySelector('.notify-me-email'); - - if (notifyButton) { - console.log("✅ NotifyMe: Button gefunden"); - notifyButton.addEventListener('click', function () { - const email = this.dataset.customerEmail; - const productId = this.dataset.productId; - sendNotificationRequest(email, productId); - }); - } else { - console.error("❌ NotifyMe: Button nicht gefunden!"); - } - - if (submitButton) { - console.log("✅ NotifyMe: Submit-Button gefunden"); - submitButton.addEventListener('click', function () { - const email = emailInput.value; - const productId = this.dataset.productId; - - if (!email) { - alert("Bitte geben Sie eine gültige E-Mail-Adresse ein."); - return; - } - - sendNotificationRequest(email, productId); - }); - } else { - console.error("❌ NotifyMe: Submit-Button nicht gefunden!"); - } - - function sendNotificationRequest(email, productId) { - console.log(`📩 Anfrage für Produkt: ${productId}, Email: ${email}`); - - fetch('/notification/subscribe', { - method: 'POST', - headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, - body: `email=${encodeURIComponent(email)}&productId=${encodeURIComponent(productId)}` - }) - .then(response => response.json()) - .then(data => { - alert(data.message); - }) - .catch(error => console.error('❌ Fehler:', error)); - } -}); diff --git a/src/Resources/public/js/notify-me.plugin.js b/src/Resources/public/js/notify-me.plugin.js new file mode 100644 index 0000000..c9b9667 --- /dev/null +++ b/src/Resources/public/js/notify-me.plugin.js @@ -0,0 +1,50 @@ +import Plugin from 'src/plugin-system/plugin.class'; + +export default class NotifyMePlugin extends Plugin { + init() { + this.notifyButton = document.getElementById('notify-me-button'); + this.emailInput = document.getElementById('notify-me-email'); + this.submitButton = document.getElementById('submit-notify'); + + this.registerEvents(); + } + + registerEvents() { + if (this.notifyButton) { + this.notifyButton.addEventListener('click', () => this.submitForm(true)); + } + + if (this.submitButton) { + this.submitButton.addEventListener('click', () => this.submitForm(false)); + } + } + + submitForm(isLoggedIn) { + let email; + const productId = this.notifyButton?.dataset.productId || this.submitButton?.dataset.productId; + + if (isLoggedIn) { + email = this.notifyButton.dataset.customerEmail; + } else { + email = this.emailInput.value.trim(); + if (!this.validateEmail(email)) { + alert("Bitte geben Sie eine gültige E-Mail-Adresse ein."); + return; + } + } + + fetch('/notification/subscribe', { + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + body: `email=${encodeURIComponent(email)}&productId=${encodeURIComponent(productId)}` + }) + .then(response => response.json()) + .then(data => alert(data.message)) + .catch(error => console.error('Error:', error)); + } + + validateEmail(email) { + const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + return emailPattern.test(email); + } +} diff --git a/src/Resources/theme.json b/src/Resources/theme.json new file mode 100644 index 0000000..f30e2e2 --- /dev/null +++ b/src/Resources/theme.json @@ -0,0 +1,6 @@ +{ + "views": [ + "@Storefront", + "@NotifyIfAvail" + ] +} diff --git a/src/Resources/views/storefront/page/product-detail/buy-widget.html.twig b/src/Resources/views/storefront/component/buy-widget/buy-widget.html.twig similarity index 63% rename from src/Resources/views/storefront/page/product-detail/buy-widget.html.twig rename to src/Resources/views/storefront/component/buy-widget/buy-widget.html.twig index 9c4d127..79e6ab7 100644 --- a/src/Resources/views/storefront/page/product-detail/buy-widget.html.twig +++ b/src/Resources/views/storefront/component/buy-widget/buy-widget.html.twig @@ -1,6 +1,6 @@ -{% sw_extends '@Storefront/storefront/page/product-detail/buy-widget.html.twig' %} +{% sw_extends '@Storefront/storefront/component/buy-widget/buy-widget.html.twig' %} -{% block page_product_detail_price %} +{% block buy_widget_price %} {{ parent() }} {% if not product.available or product.stock <= 0 %} @@ -8,13 +8,18 @@

{{ "NotifyIfAvail.notify_me"|trans }}

{% if app.customer %} - {% else %} + - {% endif %}