File size: 8,604 Bytes
de08998 bb80b30 de08998 bb80b30 de08998 bb80b30 de08998 f308110 bb80b30 f308110 bb80b30 f308110 bb80b30 f308110 bb80b30 f308110 bb80b30 de08998 bb80b30 de08998 f308110 de08998 f308110 de08998 a711756 de08998 f308110 de08998 f308110 12c2987 f308110 a711756 f308110 de08998 f308110 de08998 f308110 de08998 f308110 de08998 f308110 a711756 de08998 a711756 f308110 417dd3b f308110 417dd3b a711756 f308110 417dd3b f308110 417dd3b a711756 de08998 a711756 f308110 de08998 f308110 cdfdc4e de08998 f308110 de08998 bb80b30 de08998 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
// ===== Configuration =====
const API_BASE = "";
// ===== DOM Elements =====
const form = document.getElementById("researchForm");
const categoryInput = document.getElementById("productCategory");
const descriptionInput = document.getElementById("productDescription");
const submitBtn = document.getElementById("submitBtn");
const btnText = submitBtn.querySelector(".btn-text");
const btnLoader = submitBtn.querySelector(".btn-loader");
const errorBanner = document.getElementById("errorBanner");
const resultsDiv = document.getElementById("results");
const toggleBtns = document.querySelectorAll(".toggle-btn");
// Multi-select elements
const multiselect = document.getElementById("audienceMultiselect");
const selectedContainer = document.getElementById("selectedAudiences");
const dropdown = document.getElementById("audienceDropdown");
const searchInput = document.getElementById("audienceSearch");
const optionsContainer = document.getElementById("audienceOptions");
let selectedMethod = "gpt";
let selectedAudiences = [];
let allAudiences = [];
// ===== Load Target Audiences =====
async function loadAudiences() {
try {
const res = await fetch(`${API_BASE}/api/target-audiences`);
if (!res.ok) throw new Error("Failed to load audiences");
const data = await res.json();
allAudiences = data.audiences;
renderOptions();
renderSelected();
} catch (err) {
console.error("Could not load audiences:", err);
selectedContainer.innerHTML =
'<span class="multiselect-placeholder">⚠ Could not load — is the backend running?</span>';
}
}
// ===== Multi-select Logic (UNCHANGED) =====
function renderOptions(filter = "") {
const filterLower = filter.toLowerCase();
const filtered = allAudiences.filter((a) =>
a.toLowerCase().includes(filterLower)
);
optionsContainer.innerHTML = filtered
.map((a) => {
const isSelected = selectedAudiences.includes(a);
return `
<div class="multiselect-option ${isSelected ? "selected" : ""}" data-value="${escapeAttr(a)}">
<span class="check">${isSelected ? "✓" : ""}</span>
<span>${escapeHtml(a)}</span>
</div>
`;
})
.join("");
optionsContainer.querySelectorAll(".multiselect-option").forEach((opt) => {
opt.addEventListener("click", () => {
const val = opt.dataset.value;
if (selectedAudiences.includes(val)) {
selectedAudiences = selectedAudiences.filter((a) => a !== val);
} else {
selectedAudiences.push(val);
}
renderOptions(searchInput.value);
renderSelected();
});
});
}
function renderSelected() {
if (selectedAudiences.length === 0) {
selectedContainer.innerHTML =
'<span class="multiselect-placeholder">Select target audiences…</span>';
return;
}
selectedContainer.innerHTML = selectedAudiences
.map(
(a) => `
<span class="multiselect-tag">
${escapeHtml(a)}
<span class="multiselect-tag-remove" data-value="${escapeAttr(a)}">×</span>
</span>
`
)
.join("");
selectedContainer.querySelectorAll(".multiselect-tag-remove").forEach((btn) => {
btn.addEventListener("click", (e) => {
e.stopPropagation();
const val = btn.dataset.value;
selectedAudiences = selectedAudiences.filter((a) => a !== val);
renderOptions(searchInput.value);
renderSelected();
});
});
}
selectedContainer.addEventListener("click", () => {
dropdown.classList.toggle("hidden");
});
document.addEventListener("click", (e) => {
if (!multiselect.contains(e.target)) {
dropdown.classList.add("hidden");
}
});
searchInput.addEventListener("input", () => {
renderOptions(searchInput.value);
});
dropdown.addEventListener("click", (e) => {
e.stopPropagation();
});
// ===== Method Toggle =====
toggleBtns.forEach((btn) => {
btn.addEventListener("click", () => {
toggleBtns.forEach((b) => b.classList.remove("active"));
btn.classList.add("active");
selectedMethod = btn.dataset.method;
});
});
// ===== Form Submit =====
form.addEventListener("submit", async (e) => {
e.preventDefault();
hideError();
hideResults();
const payload = {
target_audience: selectedAudiences,
product_category: categoryInput.value.trim(),
product_description: descriptionInput.value.trim(),
method: selectedMethod,
};
if (
payload.target_audience.length === 0 ||
!payload.product_category ||
!payload.product_description
) {
showError(
"Please fill in all fields and select at least one target audience."
);
return;
}
setLoading(true);
try {
const res = await fetch(`${API_BASE}/api/research`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
if (!res.ok) throw new Error("Server error");
const data = await res.json();
renderResults(data.results, selectedMethod);
} catch (err) {
showError(err.message || "Something went wrong.");
} finally {
setLoading(false);
}
});
// =====================================================
// ✅ NEW RESULTS (VERTICAL NESTED COLLAPSIBLE)
// =====================================================
function renderResults(audienceResults, method) {
const badge =
method === "gpt"
? `<span class="results-badge gpt">GPT</span>`
: `<span class="results-badge claude">Claude</span>`;
let html = `
<div class="results-header">
<h2 class="results-title">Results</h2>
${badge}
</div>
<div class="audience-stack">
`;
audienceResults.forEach((group, groupIndex) => {
html += `
<div class="audience-card ${groupIndex === 0 ? "open" : ""}">
<div class="audience-card-header">
<div class="audience-header-left">
<span>👥</span>
<h3>${escapeHtml(group.target_audience)}</h3>
</div>
<span class="audience-chevron">▾</span>
</div>
<div class="audience-card-body">
`;
group.output.forEach((item, idx) => {
html += `
<div class="trigger-card ${idx === 0 && groupIndex === 0 ? "open" : ""}">
<div class="trigger-header">
<div>
<span class="trigger-label">Trigger ${idx + 1}</span>
<h4 class="trigger-name">${escapeHtml(item.phsychologyTriggers)}</h4>
</div>
<span class="trigger-chevron">▾</span>
</div>
<div class="trigger-body">
<div class="trigger-section">
<p class="section-title">Ad Angles</p>
<ul class="section-list">
${item.angles.map((a) => `<li>${escapeHtml(a)}</li>`).join("")}
</ul>
</div>
<div class="trigger-section">
<p class="section-title">Ad Concepts</p>
<ul class="section-list">
${item.concepts.map((c) => `<li>${escapeHtml(c)}</li>`).join("")}
</ul>
</div>
</div>
</div>
`;
});
html += `
</div>
</div>
`;
});
html += `</div>`;
resultsDiv.innerHTML = html;
resultsDiv.classList.remove("hidden");
attachAccordionEvents();
}
// ===== Accordion Logic =====
function attachAccordionEvents() {
document.querySelectorAll(".audience-card-header").forEach((header) => {
header.addEventListener("click", () => {
const card = header.parentElement;
card.classList.toggle("open");
});
});
document.querySelectorAll(".trigger-header").forEach((header) => {
header.addEventListener("click", () => {
const card = header.parentElement;
// Close siblings
const siblings = card.parentElement.querySelectorAll(".trigger-card");
siblings.forEach((s) => {
if (s !== card) s.classList.remove("open");
});
card.classList.toggle("open");
});
});
}
// ===== Helpers =====
function setLoading(isLoading) {
submitBtn.disabled = isLoading;
btnText.classList.toggle("hidden", isLoading);
btnLoader.classList.toggle("hidden", !isLoading);
}
function showError(msg) {
errorBanner.textContent = msg;
errorBanner.classList.remove("hidden");
}
function hideError() {
errorBanner.classList.add("hidden");
}
function hideResults() {
resultsDiv.classList.add("hidden");
}
function escapeHtml(str) {
const div = document.createElement("div");
div.textContent = str;
return div.innerHTML;
}
function escapeAttr(str) {
return str.replace(/"/g, """).replace(/'/g, "'");
}
loadAudiences();
|