/* The Sources tab: saved searches, company boards, and the prune by region. * * The template is a shell; the lists come from /api/sources and every action * is a fetch through `api()` from app.js, so a refusal (a search that finds * nothing, a board that does not answer) is one toast with the server's own * sentence. The catch blocks here only put a control back. */ (function () { "use strict"; const api = window.api; const $ = (sel, root = document) => root.querySelector(sel); /** @type {{searches: object[], boards: object[], adzuna_key: boolean}} */ let data = { searches: [], boards: [], adzuna_key: false }; /** @type {object[]} the boards the last prune preview named */ let pruneBoards = []; function count(n, one, many) { return `${n} ${n === 1 ? one : many || `${one}s`}`; } function debounce(fn, ms) { let timer; return (...args) => { clearTimeout(timer); timer = setTimeout(() => fn(...args), ms); }; } function cell(tr, label, content) { const td = document.createElement("td"); td.dataset.label = label; if (typeof content === "string") td.textContent = content; else td.appendChild(content); tr.appendChild(td); return td; } /** "DK 120 · EU 4 · unknown 2", the three biggest. */ function countries(source) { const shown = source.countries.slice(0, 3) .map(([code, n]) => `${code || "unknown"} ${n}`); if (source.countries.length > 3) shown.push("…"); return shown.join(" · "); } function query(source) { const p = source.params || {}; const where = source.provider === "adzuna" ? [p.country && p.country.toUpperCase(), p.where].filter(Boolean).join(", ") : (p.area ? $(`#s-area option[value="${p.area}"]`)?.textContent : "All of Denmark"); return `${p.q || ""}${where ? ` — ${where}` : ""}`; } function onSwitch(source, rerender) { const box = document.createElement("input"); box.type = "checkbox"; box.checked = source.on; box.setAttribute("aria-label", `Pull ${source.label}`); box.addEventListener("change", async () => { box.disabled = true; try { const result = await api(`/api/sources/${source.id}/switch`, { method: "POST", body: { on: box.checked } }); Object.assign(source, result.source); if (!box.checked && result.closed) { window.toast.info(`${source.label}: ${count(result.closed, "open role")} closed.`); } } catch (err) { box.checked = !box.checked; } box.disabled = false; rerender(); }); return box; } async function load() { data = await api("/api/sources"); } // ---------------------------------------------------------------- searches function renderSearches() { const body = $("#searches-list"); body.replaceChildren(); for (const source of data.searches) { const tr = document.createElement("tr"); if (!source.on) tr.classList.add("muted"); const name = document.createElement("span"); name.textContent = source.label; const provider = document.createElement("span"); provider.className = "badge"; provider.textContent = source.provider; const title = document.createElement("span"); title.append(name, " ", provider); cell(tr, "Search", title); cell(tr, "Query", query(source)).className = "mono small"; cell(tr, "Open roles", String(source.open_roles)).className = "num"; cell(tr, "Countries", countries(source)).className = "small"; cell(tr, "On", onSwitch(source, renderSearches)); const actions = document.createElement("span"); actions.className = "row-actions"; const edit = document.createElement("button"); edit.type = "button"; edit.className = "secondary"; edit.textContent = "Edit"; edit.addEventListener("click", () => fillSearchForm(source)); const remove = document.createElement("button"); remove.type = "button"; remove.className = "secondary"; remove.textContent = "Remove"; remove.addEventListener("click", () => removeSearch(source)); actions.append(edit, remove); cell(tr, "", actions); body.appendChild(tr); } const open = data.searches.reduce((sum, s) => sum + s.open_roles, 0); $("#searches-count").textContent = data.searches.length ? `${count(data.searches.length, "search", "searches")}, ${count(open, "open role")}` : "No searches yet. Add one below; the starter searches in config.yaml import on the first pull."; } function showProvider() { const provider = $("#s-provider").value; for (const node of document.querySelectorAll("#search-form [data-provider]")) { node.hidden = node.dataset.provider !== provider; } $("#adzuna-key").hidden = provider !== "adzuna" || data.adzuna_key; } function fillSearchForm(source) { const p = (source && source.params) || {}; $("#search-form-title").textContent = source ? `Edit ${source.label}` : "New search"; $("#s-provider").value = source ? source.provider : "jobindex"; $("#s-name").value = source ? source.label : ""; $("#s-name").readOnly = Boolean(source); $("#s-provider").disabled = Boolean(source); $("#s-q").value = p.q || ""; $("#s-area").value = String(p.area || (source ? 0 : 1)); $("#s-country").value = p.country || "de"; $("#s-where").value = p.where || ""; $("#s-pages").value = String(p.pages || 10); $("#search-status").textContent = ""; $("#search-sample").replaceChildren(); showProvider(); if (source) $("#search-form").scrollIntoView({ behavior: "smooth", block: "start" }); } function searchBody() { return { provider: $("#s-provider").value, name: $("#s-name").value.trim(), q: $("#s-q").value.trim(), area: Number($("#s-area").value) || 0, country: $("#s-country").value, where: $("#s-where").value.trim(), pages: Number($("#s-pages").value) || 0, }; } function found(result) { const all = result.complete ? "every match" : "the page limit cut it short"; return `Found ${count(result.found, "role")} (${all}).`; } async function trySearch() { const status = $("#search-status"); const button = $("#s-try"); button.disabled = true; status.textContent = "Searching…"; $("#search-sample").replaceChildren(); try { const result = await api("/api/searches/try", { method: "POST", body: searchBody() }); status.textContent = `${found(result)} The newest ${Math.min(result.found, 10)}:`; const list = $("#search-sample"); for (const row of result.sample) { const li = document.createElement("li"); const link = document.createElement("a"); link.href = row.url; link.target = "_blank"; link.rel = "noopener"; link.textContent = row.title; li.append(link, ` — ${[row.company, row.place].filter(Boolean).join(", ")}`); list.appendChild(li); } } catch (err) { status.textContent = ""; } button.disabled = false; } async function saveSearch(event) { event.preventDefault(); const status = $("#search-status"); const button = $("#search-form button[type=submit]"); button.disabled = true; status.textContent = "Running it once before saving…"; try { const result = await api("/api/searches", { method: "POST", body: searchBody() }); await load(); renderSearches(); fillSearchForm(null); status.textContent = `Saved ${result.source.label}. ${found(result)} ` + "Its roles arrive with the next pull."; } catch (err) { status.textContent = ""; } button.disabled = false; } async function removeSearch(source) { if (!window.confirm(`Remove ${source.label}? Its ${count(source.open_roles, "open role")} close.`)) { return; } try { await api(`/api/searches/${source.id}`, { method: "DELETE" }); data.searches = data.searches.filter((s) => s.id !== source.id); renderSearches(); } catch (err) { /* already toasted */ } } // ------------------------------------------------------------------ boards function renderBoards() { const q = $("#b-q").value.trim().toLowerCase(); const on = $("#b-on").value; const rows = data.boards .filter((b) => !q || b.label.toLowerCase().includes(q) || b.target.includes(q)) .filter((b) => !on || (on === "on") === b.on) .sort((a, b) => Number(b.on) - Number(a.on) || b.open_roles - a.open_roles || a.label.localeCompare(b.label)); const body = $("#boards-list"); body.replaceChildren(); for (const board of rows) { const tr = document.createElement("tr"); if (!board.on) tr.classList.add("muted"); const link = document.createElement("a"); link.href = `/?company=${encodeURIComponent(board.label)}`; link.title = "This company's open roles"; link.textContent = board.label; cell(tr, "Company", link); cell(tr, "Board", board.target).className = "mono small"; cell(tr, "Open roles", String(board.open_roles)).className = "num"; cell(tr, "Countries", countries(board)).className = "small"; cell(tr, "On", onSwitch(board, renderBoards)); body.appendChild(tr); } const onCount = data.boards.filter((b) => b.on).length; $("#boards-count").textContent = `${count(data.boards.length, "board")}, ${onCount} on. Showing ${rows.length}.`; } async function addBoard(event) { event.preventDefault(); const status = $("#board-status"); const button = $("#board-form button[type=submit]"); button.disabled = true; status.textContent = "Reading the board once before following it…"; try { const result = await api("/api/boards", { method: "POST", body: { board: $("#b-board").value.trim(), name: $("#b-name").value.trim() }, }); data.boards.push(result.source); renderBoards(); $("#board-form").reset(); status.textContent = `Following ${result.source.label}: ` + `${count(result.found, "role")} listed. They arrive with the next pull.`; } catch (err) { status.textContent = ""; } button.disabled = false; } // ------------------------------------------------------------------- prune async function previewPrune(event) { event.preventDefault(); const status = $("#prune-status"); const list = $("#prune-list"); const region = $("#p-region").value.trim(); list.replaceChildren(); $("#p-apply").disabled = true; status.textContent = "Checking every board…"; try { const result = await api(`/api/prune?region=${encodeURIComponent(region)}`); pruneBoards = result.boards; const roles = pruneBoards.reduce((sum, b) => sum + b.open_roles, 0); status.textContent = pruneBoards.length ? `${count(pruneBoards.length, "board")} with no open role in ${region}, ` + `${count(roles, "open role")} between them:` : `Every board that is on has a role in ${region}.`; for (const board of pruneBoards) { const li = document.createElement("li"); li.textContent = `${board.label} (${board.target}), ${count(board.open_roles, "open role")}`; list.appendChild(li); } $("#p-apply").disabled = pruneBoards.length === 0; } catch (err) { status.textContent = ""; } } async function applyPrune() { const region = $("#p-region").value.trim(); if (!window.confirm(`Switch off ${count(pruneBoards.length, "board")} and close their roles? ` + "Each can be switched back on in the list above.")) return; $("#p-apply").disabled = true; try { const result = await api("/api/prune", { method: "POST", body: { region } }); $("#prune-status").textContent = `Switched off ${count(result.pruned, "board")}, ` + `closed ${count(result.roles_closed, "role")}. ${result.boards_left} boards are on.`; $("#prune-list").replaceChildren(); await load(); renderBoards(); } catch (err) { $("#p-apply").disabled = false; } } // -------------------------------------------------------------------- pull async function pullNow() { const button = $("#pull-now"); button.disabled = true; try { await api("/api/jobs", { method: "POST", body: { kind: "pull", payload: {} } }); $("#pull-status").replaceChildren("Pull started. "); const link = document.createElement("a"); link.href = "/jobs"; link.textContent = "Follow it on the Jobs tab"; $("#pull-status").appendChild(link); } catch (err) { /* already toasted */ } button.disabled = false; } // -------------------------------------------------------------------- init document.addEventListener("DOMContentLoaded", async () => { if (!$("#searches-list")) return; try { await load(); } catch (err) { return; } renderSearches(); renderBoards(); fillSearchForm(null); $("#s-provider").addEventListener("change", showProvider); $("#s-try").addEventListener("click", trySearch); $("#search-form").addEventListener("submit", saveSearch); $("#s-new").addEventListener("click", () => fillSearchForm(null)); $("#b-q").addEventListener("input", debounce(renderBoards, 150)); $("#b-on").addEventListener("change", renderBoards); $("#board-form").addEventListener("submit", addBoard); $("#prune-form").addEventListener("submit", previewPrune); $("#p-apply").addEventListener("click", applyPrune); $("#pull-now").addEventListener("click", pullNow); }); })();