// Elementų užklausos const button = document.querySelector(".hook-generator button"); const loading = document.querySelector(".loading"); const result = document.querySelector(".lws-tool-result"); const popup = document.querySelector(".popup"); const selectedType = document.querySelector(".selected-type"); const list = document.querySelector(".list"); const counter = document.querySelector(".counter");
let inputVal = ''; let emoji = []; let response = ""; let data = ""; let copyAll = ""; let contentType = "Short Video";
const API_BASE_URL = "https://marketora.endgoalmarketing.com/wp-admin/admin-ajax.php"; const loadingTemplate = `
Generating Hooks...
`;
// Formos pateikimo valdymas function onFormSubmit(event) { event.preventDefault();
if (!inputVal) return;
button.disabled = true; emoji = []; copyAll = ""; response = ""; loading.innerHTML = loadingTemplate; result.innerHTML = ""; result.style.display = "none"; loading.style.display = "flex";
const formData = new FormData(); formData.append("action", "generate_hooks_basic"); formData.append("topic", inputVal); formData.append("usage", contentType);
fetchData(formData); }
// Duomenų gavimas async function fetchData(formData) { try { const response = await fetch(API_BASE_URL, { method: "POST", body: formData, });
if (!response.ok) throw new Error("Request failed. Please try again!");
data = await response.json();
// if (data === "LOGIN_FIRST") { showPopup("Login Error", "You need to login first!", "https://marketora.endgoalmarketing.com/user-login"); } else if (data === "NO_BALANCE") { showPopup("Balance Error", "Not Enough Power Points!", "https://marketora.endgoalmarketing.com/points-system"); } else { data = JSON.parse(data); showResultUI(data); } } catch (error) { console.error("Error occurred:", error); showErrorUI(); } finally { button.disabled = false; loading.style.display = "none"; } }
// Rodo klaidos UI function showErrorUI() { loading.innerHTML = `

Uh Oh!
Something went wrong! Please try again
`; }
// Rezultato UI rodymas function showResultUI(data) { data.forEach((item, index) => { copyAll += `${item.hook}n`; emoji.push(getEmoji(item.hook_type)); });
const resultHTML = `
Hooks Generated
${emoji[index]} ${item.hook_type}
${item.hook}
`).join("")}
Copy all
`; result.insertAdjacentHTML("beforeend", resultHTML); }
// Pasirenkamas teisingas emoji function getEmoji(hookType) { const emojiMap = { "Strong": '💪', "Intriguing": '🤔', "Fact": '📚', "Metaphor": '🌈', "Story": '📜', "Statistical": '📈', "Quotation": '💬', "Challenge": '🏋️♂️', "Visual": '👀', "Action": '🏃♂️', "Historical": '✍🏻', "Anecdotal": '😄', "Humorous": '😂', "Controversial": '🤷♂️', "Rhetorical": '❓' }; return emojiMap[hookType] || ''; }
// Įvesties pokytis function handleInputChange(event) { inputVal = event.target.value.trim(); const maxLength = 150;
button.disabled = inputVal.length === 0;
if (inputVal.length > maxLength) { inputVal = inputVal.substring(0, maxLength); event.target.value = inputVal; }
counter.style.display = "block"; counter.textContent = `${inputVal.length} / ${maxLength}`; }
// Kopijavimo funkcija function copyToClipboard(index) { let copyText = index === 'All' ? copyAll : data[index]?.hook; const copyAlerts = document.getElementsByClassName("sub-copy-alert");
navigator.clipboard.writeText(copyText);
if (index === 'All') { document.querySelector(".copy-all").textContent = "Copied All"; setTimeout(() => document.querySelector(".copy-all").textContent = "Copy all", 2000); } else { copyAlerts[index].style.display = "block"; setTimeout(() => copyAlerts[index].style.display = "none", 2000); } }
// Dropdown pasirinkimo valdymas function handleDropDownClick() { list.classList.toggle("active"); }
function handleContentTypeSelection(event) { contentType = event.target.innerText; selectedType.innerText = contentType; list.classList.toggle("active"); }
// Rodo iššokantį langą function showPopup(title, message, link) { popup.innerHTML = `
`; popup.style.display = "flex"; document.querySelector(".popup").addEventListener("click", (event) => { if (event.target === popup || event.target.classList.contains("close")) { popup.style.display = "none"; } }); }