Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.8 kB
2
Indexable
Never
{% block javascripts %}
    <script>
        const shareMailForm = document.getElementById("shareMailForm");
        const eInput = shareMailForm.querySelector(".share-mail");
        const shareMailSubmit = document.getElementById("shareMail-submit");
        const text = shareMailForm.querySelector(".invalide-email");
        const pattern = /^[^ ]+@[^ ]+\.[a-z]{2,3}$/;

        if (shareMailSubmit) {
            shareMailSubmit.addEventListener("click", function (e) {
                e.preventDefault();
                const pathapi = document.getElementById("shareMail-submit").getAttribute('data-path');
                console.log(pathapi);
                const dataIdValue = document.getElementById("shareMail-submit").dataset.id;
                const email = document.getElementById("email").value;
                let url = '{{ path(pathapi, { 'email': 'email_value', 'id': 'id_value' })|raw }}'
                    .replace('email_value', encodeURIComponent(email))
                    .replace('id_value', encodeURIComponent(dataIdValue));
                if (pathapi ==="pac_pool_mail"){
                    url = '{{ path(pathapi) }}';
                }

                let dataToSend = {
                    email: email
                };
                console.log(url);
                // handle form validation
                shareMailForm.classList.add("error");
                shareMailForm.classList.remove("valid");
                if (eInput.value === "") {
                    text.innerText = "L'e-mail ne peut pas être vide";
                } else if (!eInput.value.match(pattern)) {
                    text.innerText = "Email invalide";
                } else {
                    shareMailForm.classList.replace("error", "valid");
                    text.innerText = "";

                    fetch(url, {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify(dataToSend)
                    })
                        .then(response => {
                            if (response.ok && response.status === 200) {
                                console.log("ttttat");
                                // Display the notification toast for a successful response
                                document.querySelector(".PopupMail").setAttribute('data-popup-state', 'closed');
                                document.querySelector(".PopupMail").setAttribute('aria-hidden', 'true');
                                document.querySelector(".toastBlock--email").innerText = eInput.value;
                                document.querySelector(".successShare").style.display = "flex";
                                setTimeout(() => {
                                    document.querySelector(".successShare").style.display = "none";
                                }, 3000);
                            } else {
                                document.querySelector(".PopupMail").setAttribute('data-popup-state', 'closed');
                                document.querySelector(".PopupMail").setAttribute('aria-hidden', 'true');
                                document.querySelector(".failToast").style.display = "flex";
                                setTimeout(() => {
                                    document.querySelector(".failToast").style.display = "none";
                                }, 3000);
                            }
                        })
                        .catch(error => {
                            console.error('Error:', error);
                        });
                }
            });
        }
    </script>

{% endblock %}