| 1 |
document.addEventListener("DOMContentLoaded", () => { |
| 2 |
const form = document.getElementById("aibui-settings-form"); |
| 3 |
const toast = document.getElementById("aibui-settings-toast"); |
| 4 |
|
| 5 |
function showToast(message, isError = false) { |
| 6 |
if (!toast) return; |
| 7 |
toast.textContent = message; |
| 8 |
toast.style.display = "block"; |
| 9 |
toast.style.background = isError ? "#b00020" : "#23272f"; |
| 10 |
toast.style.color = "#fff"; |
| 11 |
toast.style.padding = "10px 14px"; |
| 12 |
toast.style.borderRadius = "8px"; |
| 13 |
setTimeout(() => { |
| 14 |
toast.style.display = "none"; |
| 15 |
}, 4000); |
| 16 |
} |
| 17 |
|
| 18 |
async function getJwtToken() { |
| 19 |
const res = await fetch(aiBuilderVars.ajaxurl, { |
| 20 |
method: "POST", |
| 21 |
headers: { "Content-Type": "application/x-www-form-urlencoded" }, |
| 22 |
body: `action=aibui_get_token&nonce=${aiBuilderVars.nonce}`, |
| 23 |
}); |
| 24 |
const data = await res.json(); |
| 25 |
if (data.success && data.data.token) return data.data.token; |
| 26 |
throw new Error("Missing token"); |
| 27 |
} |
| 28 |
|
| 29 |
async function loadSettings() { |
| 30 |
try { |
| 31 |
const jwt = await getJwtToken(); |
| 32 |
const res = await fetch(window.config.apiUrl + "/settings", { |
| 33 |
method: "GET", |
| 34 |
headers: { Authorization: `Bearer ${jwt}` }, |
| 35 |
}); |
| 36 |
if (!res.ok) throw new Error("Failed to load settings"); |
| 37 |
const data = await res.json(); |
| 38 |
|
| 39 |
// Expect shape: { primaryColor, secondaryColor, siteName, siteDescription, nativeFont } |
| 40 |
const { |
| 41 |
primaryColor = "#2f80ed", |
| 42 |
secondaryColor = "#00b87c", |
| 43 |
siteName = "", |
| 44 |
siteDescription = "", |
| 45 |
// nativeFont = "system-ui", |
| 46 |
} = data || {}; |
| 47 |
|
| 48 |
document.getElementById("primaryColor").value = primaryColor; |
| 49 |
console.log("a"); |
| 50 |
document.getElementById("secondaryColor").value = secondaryColor; |
| 51 |
console.log("b"); |
| 52 |
document.getElementById("siteName").value = siteName; |
| 53 |
console.log("d"); |
| 54 |
document.getElementById("siteDescription").value = siteDescription; |
| 55 |
console.log("e"); |
| 56 |
// document.getElementById("nativeFont").value = nativeFont; |
| 57 |
console.log("f"); |
| 58 |
} catch (e) { |
| 59 |
// Silent load error; user can still save |
| 60 |
console.warn("AI Builder Settings: load failed", e); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
form?.addEventListener("submit", async (e) => { |
| 65 |
e.preventDefault(); |
| 66 |
try { |
| 67 |
const jwt = await getJwtToken(); |
| 68 |
const body = { |
| 69 |
primaryColor: document.getElementById("primaryColor").value, |
| 70 |
secondaryColor: document.getElementById("secondaryColor").value, |
| 71 |
siteName: document.getElementById("siteName").value.trim(), |
| 72 |
siteDescription: document |
| 73 |
.getElementById("siteDescription") |
| 74 |
.value.trim(), |
| 75 |
// nativeFont: document.getElementById("nativeFont").value, |
| 76 |
}; |
| 77 |
|
| 78 |
const res = await fetch(window.config.apiUrl + "/settings", { |
| 79 |
method: "POST", |
| 80 |
headers: { |
| 81 |
"Content-Type": "application/json", |
| 82 |
Authorization: `Bearer ${jwt}`, |
| 83 |
}, |
| 84 |
body: JSON.stringify(body), |
| 85 |
}); |
| 86 |
if (!res.ok) throw new Error("Save failed"); |
| 87 |
showToast("Settings saved"); |
| 88 |
} catch (err) { |
| 89 |
showToast("Error saving settings", true); |
| 90 |
} |
| 91 |
}); |
| 92 |
|
| 93 |
loadSettings(); |
| 94 |
}); |
| 95 |
|