The Roles tab is news's feed row: the title opens /roles/{uid}, company,
place and age under it, the badges, a one-line excerpt (summary, else the
description's opening), and ▲ ● ▼ ★ on the right, under the text at 640px.
The company affinity control leaves the row. A passed role and a hidden
company's are dimmed and stay. The filter bar is one row: order, search and
a Filters <details> holding the rest, with a judged select and "Show hidden
companies". The list loads 60 and appends on an IntersectionObserver
sentinel; back from a read page restores the filters, rows and scroll.
GET /api/roles takes offset, defaults to 60 and answers {items, total}.
judged= is a WHERE on roles.vote. Under a ranking the served score and the
order are the stored score moved by the vote (x0.5 pass, x1.2 like, capped
at 1), so a vote shows at once without a rescore. POST and DELETE
/api/roles/{uid}/vote set and clear it; pressing the pressed stance clears.
★ saves; pressed again it calls DELETE /api/applications/{id}, which drops
the application while it is still saved and answers 409 after. save_role no
longer writes the placeholder next step.
The read page (role.html, role.js) is news's reader: a bar with back,
▲ ● ▼ ★ and ↗, the company with its affinity control, place, badges, salary,
summary, the text (fetched posting, else description) and the link out.
It offers Fetch the posting for a teaser and Summarise without a summary,
against the /text and /summarise routes B4 adds. The vote and star buttons
are judge.js, shared by the feed and the reader.
Applications: titles open the read page, the Next column is gone, a Saved
column instead, and the lede says what the page is for.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
133 lines
4.6 KiB
JavaScript
133 lines
4.6 KiB
JavaScript
/* ▲ ● ▼ ★ for one role: the feed row and the read page share them.
|
|
*
|
|
* news's `voteButtons`, `starButton` and `vote()`, adapted. Three stances and
|
|
* no stance is four states from three buttons; pressing the stance a role
|
|
* already has clears it, which is the only way back to unjudged, since ● is
|
|
* an answer ("neither way") and not the absence of one. A vote never hides a
|
|
* role: a stray tap stays on screen, pressed, and one more tap undoes it.
|
|
*
|
|
* ★ is Save. It files the role under Applications (`POST /api/applications`)
|
|
* and, pressed again, takes it back out (`DELETE /api/applications/{id}`),
|
|
* which the server allows only while the application is still at `saved`; a
|
|
* 409 is toasted by `api()` and the star stays lit.
|
|
*
|
|
* Both are optimistic: the button changes before the request and changes back
|
|
* if it fails. `api()` has already toasted the failure by then.
|
|
*
|
|
* await loadTracked();
|
|
* container.append(judgeButtons(role, (role) => row.classList.toggle(...)));
|
|
*/
|
|
(function () {
|
|
"use strict";
|
|
|
|
const { api } = window;
|
|
|
|
/** @type {Map<string, object>} role_uid -> application, for roles saved */
|
|
const tracked = new Map();
|
|
|
|
async function loadTracked() {
|
|
const applications = await api("/api/applications");
|
|
tracked.clear();
|
|
for (const a of applications) if (a.role_uid) tracked.set(a.role_uid, a);
|
|
return tracked;
|
|
}
|
|
|
|
const STANCES = [
|
|
{ value: 1, cls: "up", glyph: "▲", said: "Like" },
|
|
{ value: 0, cls: "mid", glyph: "●", said: "Neither way" },
|
|
{ value: -1, cls: "down", glyph: "▼", said: "Pass" },
|
|
];
|
|
|
|
function button(cls, glyph) {
|
|
const b = document.createElement("button");
|
|
b.type = "button";
|
|
b.className = cls;
|
|
b.textContent = glyph;
|
|
return b;
|
|
}
|
|
|
|
/* The four buttons as a fragment, bound to `role`, which they read and
|
|
write in place (`role.vote`), so the caller's copy is always current.
|
|
`onChange(role)` runs after every change, optimistic or reverted: the
|
|
feed dims a passed row with it. */
|
|
function judgeButtons(role, onChange) {
|
|
const fragment = document.createDocumentFragment();
|
|
const votes = STANCES.map((s) => {
|
|
const b = button(s.cls, s.glyph);
|
|
b.addEventListener("click", () => vote(s.value));
|
|
fragment.appendChild(b);
|
|
return b;
|
|
});
|
|
const star = button("star", "★");
|
|
star.addEventListener("click", () => toggleSave());
|
|
fragment.appendChild(star);
|
|
|
|
function paint() {
|
|
STANCES.forEach((s, i) => {
|
|
const on = role.vote === s.value;
|
|
const said = on ? `${s.said} — tap again to undo` : s.said;
|
|
votes[i].setAttribute("aria-pressed", String(on));
|
|
votes[i].title = said;
|
|
votes[i].setAttribute("aria-label", said);
|
|
});
|
|
const saved = tracked.has(role.uid);
|
|
const said = saved ? "Saved to Applications — tap to unsave" : "Save to Applications";
|
|
star.setAttribute("aria-pressed", String(saved));
|
|
star.title = said;
|
|
star.setAttribute("aria-label", said);
|
|
if (onChange) onChange(role);
|
|
}
|
|
|
|
async function vote(value) {
|
|
const before = role.vote ?? null;
|
|
const clearing = before === value;
|
|
role.vote = clearing ? null : value;
|
|
paint();
|
|
const path = `/api/roles/${encodeURIComponent(role.uid)}/vote`;
|
|
try {
|
|
const updated = clearing
|
|
? await api(path, { method: "DELETE" })
|
|
: await api(path, { method: "POST", body: { value } });
|
|
role.vote = updated.vote;
|
|
role.voted_at = updated.voted_at;
|
|
} catch (err) {
|
|
role.vote = before;
|
|
}
|
|
paint();
|
|
}
|
|
|
|
// One request at a time: a second tap while the first is in flight would
|
|
// unsave an application whose id has not come back yet.
|
|
async function toggleSave() {
|
|
if (star.disabled) return;
|
|
star.disabled = true;
|
|
const before = tracked.get(role.uid);
|
|
try {
|
|
if (before) {
|
|
tracked.delete(role.uid);
|
|
paint();
|
|
await api(`/api/applications/${encodeURIComponent(before.id)}`,
|
|
{ method: "DELETE" });
|
|
} else {
|
|
tracked.set(role.uid, { role_uid: role.uid });
|
|
paint();
|
|
tracked.set(role.uid, await api("/api/applications", {
|
|
method: "POST", body: { role_uid: role.uid, status: "saved" },
|
|
}));
|
|
}
|
|
} catch (err) {
|
|
if (before) tracked.set(role.uid, before);
|
|
else tracked.delete(role.uid);
|
|
} finally {
|
|
star.disabled = false;
|
|
}
|
|
paint();
|
|
}
|
|
|
|
paint();
|
|
return fragment;
|
|
}
|
|
|
|
Object.assign(window, { judgeButtons, loadTracked });
|
|
})();
|