document.addEventListener("DOMContentLoaded", function() {
// Não chamar o plugin se a url terminar com /redirecionando
if (!window.location.pathname.endsWith("/redirecionando")) {
callWhatsApp();
}
});
var numberWhats = "";
var loading = false;
const callWhatsApp = () => {
// adiciona o CSS do plugin
let css = document.createElement("link");
css.setAttribute("rel", "stylesheet");
css.setAttribute("href", "https://painel.noot.com.br/build/assets/css/plugin-whatsapp.css");
document.head.appendChild(css);
number_whats = "";
let div = document.createElement("div");
div.setAttribute("id", "noot-whats-plugin");
let closed = "";
let decodedCookie = decodeURIComponent(document.cookie);
if (decodedCookie.includes("noot-whats-plugin_close_whats=")) {
closed = "closed";
}
div.innerHTML = `
`;
document.body.appendChild(div);
document.getElementById("w-form").addEventListener("submit", sendWhatsForm);
document.getElementById("w-close").addEventListener("click", whatsClose);
document.getElementById("noot-whats-plugin_circle").addEventListener("click", whatsOpen);
document.getElementById("noot-whats-plugin_circle").addEventListener("click", closeWhatss);
document.getElementById("noot-whats-plugin_close").addEventListener("click", function () {
closeWhatss();
let d = new Date();
d.setTime(d.getTime() + 3 * 24 * 60 * 60 * 1000);
let expires = "expires=" + d.toUTCString();
document.cookie = "noot-whats-plugin_close_whats=true;" + expires + ";path=/";
});
document.querySelectorAll('input.noot-whats-plugin_modal__form__input').forEach(input => {
input.addEventListener('focus', () => {
input.classList.remove('error');
});
});
};
const whatsOpen = () => {
document.body.classList.add("active-noot-whats-plugin");
};
const whatsClose = () => {
document.body.classList.remove("active-noot-whats-plugin");
document.querySelectorAll('input.noot-whats-plugin_modal__form__input').forEach(input => {
input.classList.remove('error');
});
};
const sendWhatsForm = () => {
if(loading) return;
document.getElementById("w-submit").disabled = true;
loading = true;
fetch("https://painel.noot.com.br/api/v1/contact?tag=lead-whatsapp", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': '428b9163c23850cb0db29ab84a849bb08ebf324d06a5a'
},
body: JSON.stringify({
name: document.getElementById("w-name").value,
email: document.getElementById("w-email").value,
phone: document.getElementById("w-phone").value
})
})
.then(response => {
if (!response.ok) {
response.json().then(data => {
if (data?.errors) {
Object.keys(data.errors).forEach(field => {
document.getElementById(`w-${field}`).classList.add('error');
document.getElementById(`w-${field}`).parentElement.querySelector('span.noot-whats-plugin_modal__form__text-error').textContent = data.errors[field][0];
});
}
});
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
window.open('/redirecionando');
document.getElementById("w-name").value = "";
document.getElementById("w-email").value = "";
document.getElementById("w-phone").value = "";
whatsClose();
})
.catch(error => {
console.log(error);
})
.finally(() => {
document.getElementById("w-submit").disabled = false;
loading = false;
});
}
const closeWhatss = () => {
document.getElementById("noot-whats-plugin_popup").classList.add("closed");
}
const mascara = (e, r) => {
v_obj = e,
v_fun = r,
setTimeout("execmascara()", 1)
}
const execmascara = () => {
v_obj.value = v_fun(v_obj.value)
}
const phone = (e) => {
return e = (e = e.replace(/\D/g, "")).length > 10 ? (e = e.replace(/^(\d\d)(\d)/g, "($1) $2")).replace(/(\d{5})(\d)/, "$1-$2") : (e = e.replace(/^(\d\d)(\d)/g, "($1) $2")).replace(/(\d{4})(\d)/, "$1-$2")
}