Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .eslint_todo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This configuration was generated by `exe/eslint_autogen`
// on 2026-11-28 02:17:20 UTC.
// on 2026-11-28 02:54:57 UTC.
// The point is for the user to remove these configuration records
// one by one as the offenses are removed from the code base.

Expand Down Expand Up @@ -32,10 +32,9 @@ const config: Linter.Config[] = [
"@stylistic/list-style": "off",
},
},
// Offense count: 3
// Offense count: 2
{
files: [
"spec/javascript/controllers/story_controller_open_spec.ts",
"spec/javascript/setup.ts",
"vitest.config.ts",
],
Expand Down
8 changes: 2 additions & 6 deletions app/components/story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
# state classes; its contents sit in a Turbo Frame so refreshing the story
# from its feed swaps them in place (see `Components::StoryFrame`).
class Components::Story < Components::Base
# The star and refresh forms submit inside the story's frame: sample
# stories skip them, and a failed refresh leaves the story as it was.
ACTIONS = [
"submit->story#formSubmitted",
"turbo:frame-missing->story#frameMissing"
].join(" ").freeze
# A failed refresh leaves the story as it was.
ACTIONS = "turbo:frame-missing->story#frameMissing"

attr_accessor :story

Expand Down
3 changes: 3 additions & 0 deletions app/javascript/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ application.register("tooltip", TooltipController);
import TutorialStartController from "./tutorial_start_controller";
application.register("tutorial-start", TutorialStartController);

import TutorialStoriesController from "./tutorial_stories_controller";
application.register("tutorial-stories", TutorialStoriesController);

import UnreadCountController from "./unread_count_controller";
application.register("unread-count", UnreadCountController);
7 changes: 0 additions & 7 deletions app/javascript/controllers/story_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,6 @@ export default class extends Controller {
this.dispatch("read-changed");
}

// Sample stories aren't saved, so their star and refresh forms do nothing.
formSubmitted(event: Event): void {
if (this.isSample()) {
event.preventDefault();
}
}

/*
* A failed refresh can respond without the story's Turbo Frame, e.g. when
* the story was deleted. Cancelling the event keeps the story as it was,
Expand Down
109 changes: 109 additions & 0 deletions app/javascript/controllers/tutorial_stories_controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { Controller } from "@hotwired/stimulus";

// How long a pretend refresh spins its icon.
const REFRESH_MS = 600;

function keepUnreadIcon(kept: boolean): string {
if (kept) {
return "fa-check";
}

return "fa-square-o";
}

function hiddenValue(form: HTMLFormElement, name: string): HTMLInputElement {
const input = form.querySelector<HTMLInputElement>(`input[name='${name}']`);
if (!input) {
throw new Error(`Missing ${name} input`);
}

return input;
}

function setIcon(form: HTMLFormElement, icon: string): void {
const element = form.querySelector("i");
if (element) {
element.className = `fa ${icon}`;
}
}

/*
* Swaps in an updated copy of the keep unread form, like the keep unread
* stream does. The story controller copies the new state onto the story
* when the copy connects.
*/
function keepUnread(form: HTMLFormElement): void {
const kept = hiddenValue(form, "keep_unread").value === "true";
const copy = form.cloneNode(true);
if (!(copy instanceof HTMLFormElement)) {
return;
}

copy.dataset.keepUnread = String(kept);
copy.dataset.isRead = String(!kept);
hiddenValue(copy, "keep_unread").value = String(!kept);
setIcon(copy, keepUnreadIcon(kept));
form.replaceWith(copy);
}

// Spins the refresh icon for a moment, as a real refresh does.
function refresh(form: HTMLFormElement): void {
form.setAttribute("aria-busy", "true");
setTimeout(() => {
form.removeAttribute("aria-busy");
}, REFRESH_MS);
}

/*
* Imitates the story forms on the tutorial page, whose sample stories have
* nothing to save. It cancels each submit and makes the change the server's
* Turbo Stream response would have made, so the tutorial behaves like the
* real stories page.
*/
export default class extends Controller {
static override values = { starLabel: String, unstarLabel: String };

declare starLabelValue: string;

declare unstarLabelValue: string;

submitted(event: Event): void {
const form = event.target;
if (!(form instanceof HTMLFormElement)) {
return;
}

event.preventDefault();

if (form.classList.contains("story-star-form")) {
this.star(form);
} else if (form.classList.contains("story-keep-unread-form")) {
keepUnread(form);
} else if (form.classList.contains("story-refresh-form")) {
refresh(form);
}
}

// Updates both of the story's star buttons, like the star stream does.
private star(form: HTMLFormElement): void {
const starred = hiddenValue(form, "starred").value === "true";
const story = form.closest("li.story") ?? form;
const forms = story.querySelectorAll<HTMLFormElement>(".story-star-form");

for (const starForm of forms) {
hiddenValue(starForm, "starred").value = String(!starred);
this.showStar(starForm, starred);
}
}

private showStar(form: HTMLFormElement, starred: boolean): void {
const button = form.querySelector("button");
if (starred) {
setIcon(form, "fa-star");
button?.setAttribute("aria-label", this.unstarLabelValue);
} else {
setIcon(form, "fa-star-o");
button?.setAttribute("aria-label", this.starLabelValue);
}
}
}
16 changes: 15 additions & 1 deletion app/views/tutorials/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def view_template
render(Components::TutorialActionBar.new(stories:))
end

render(Components::StoryList.new(stories:, title: site_title))
div(data: stories_data) do
render(Components::StoryList.new(stories:, title: site_title))
end
setup
end

Expand Down Expand Up @@ -83,6 +85,18 @@ def start_button
a(**START_BUTTON) { t("tutorial.start") }
end

# The sample stories have nothing to save, so tutorial-stories imitates
# their forms instead of submitting them. It listens in the capture phase
# because each story's Turbo Frame stops the submit event from bubbling.
def stories_data
{
controller: "tutorial-stories",
action: "submit->tutorial-stories#submitted:capture",
tutorial_stories_star_label_value: t("stories.star"),
tutorial_stories_unstar_label_value: t("stories.unstar")
}
end

def start_data
{
controller: "tutorial-start",
Expand Down
7 changes: 2 additions & 5 deletions spec/components/story_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@ def values(element)
expect(values(story_li(story))).to eq([story.id.to_s, "false", "true"])
end

it "wires the form submit and missing frame actions" do
it "keeps the story in place when a refresh fails" do
li = story_li(create(:story))

expect(li["data-action"].split).to include(
"submit->story#formSubmitted",
"turbo:frame-missing->story#frameMissing"
)
expect(li["data-action"]).to eq("turbo:frame-missing->story#frameMissing")
end

it "makes the li a story-list and unread-count target", :aggregate_failures do
Expand Down
22 changes: 0 additions & 22 deletions spec/javascript/controllers/story_controller_open_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,25 +151,3 @@ describe("frameMissing from outside the story", () => {
expect(event.defaultPrevented).toBe(false);
});
});

describe("formSubmitted", () => {
function submitRefresh(): SubmitEvent {
const form = ensure(element().querySelector("form"));
const event = new SubmitEvent("submit", { bubbles: true, cancelable: true });
form.dispatchEvent(event);
return event;
}

it("cancels form submits for sample stories", async () => {
await setupController({ id: "-5" });

expect(submitRefresh().defaultPrevented).toBe(true);
});

it("lets form submits through for real stories", async () => {
await setupController();
const event = submitRefresh();

expect(event.defaultPrevented).toBe(false);
});
});
128 changes: 128 additions & 0 deletions spec/javascript/controllers/tutorial_stories_controller_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { bootStimulus } from "support/stimulus";
import TutorialStoriesController from "controllers/tutorial_stories_controller";
import { ensure } from "helpers/ensure";

// An unstarred star form, as the server renders it.
function starForm(id: string): string {
return [
`<form id='${id}' class='story-star-form'>`,
" <input type='hidden' name='starred' value='true'>",
" <button><i class='fa fa-star-o'></i></button>",
"</form>",
].join("\n");
}

async function setupController(): Promise<void> {
// Static test fixture — safe to use innerHTML
document.body.innerHTML = [
"<div data-controller='tutorial-stories'",
" data-action='submit->tutorial-stories#submitted'",
" data-tutorial-stories-star-label-value='Star'",
" data-tutorial-stories-unstar-label-value='Unstar'>",
" <li class='story'>",
starForm("star-preview"),
starForm("star-actions"),
" <form class='story-keep-unread-form' data-is-read='true'",
" data-keep-unread='false'>",
" <input type='hidden' name='keep_unread' value='true'>",
" <button><i class='fa fa-square-o'></i> Keep unread</button>",
" </form>",
" <form class='story-refresh-form'><button>Refresh</button></form>",
" </li>",
"</div>",
].join("\n");

await bootStimulus("tutorial-stories", TutorialStoriesController);
}

function form(selector: string): HTMLFormElement {
return ensure(document.querySelector<HTMLFormElement>(selector));
}

function submit(selector: string): SubmitEvent {
const event = new SubmitEvent("submit", { bubbles: true, cancelable: true });
form(selector).dispatchEvent(event);
return event;
}

function icon(selector: string): string {
return ensure(form(selector).querySelector("i")).className;
}

describe("submitted", () => {
it("cancels the submit", async () => {
await setupController();

expect(submit(".story-refresh-form").defaultPrevented).toBe(true);
});
});

describe("submitted for a star form", () => {
it("stars both of the story's star buttons", async () => {
await setupController();

submit("#star-preview");

expect([icon("#star-preview"), icon("#star-actions")])
.toStrictEqual(["fa fa-star", "fa fa-star"]);
});

it("asks to unstar next time, with a matching label", async () => {
await setupController();

submit("#star-preview");

const starred = form("#star-actions");
expect([
ensure(starred.querySelector("input")).value,
ensure(starred.querySelector("button")).getAttribute("aria-label"),
]).toStrictEqual(["false", "Unstar"]);
});

it("unstars on a second submit", async () => {
await setupController();

submit("#star-preview");
submit("#star-actions");

expect(icon("#star-preview")).toBe("fa fa-star-o");
});
});

describe("submitted for the keep unread form", () => {
it("swaps in a copy with the kept unread state", async () => {
await setupController();
const original = form(".story-keep-unread-form");

submit(".story-keep-unread-form");

const copy = form(".story-keep-unread-form");
expect([copy === original, copy.dataset.isRead, copy.dataset.keepUnread])
.toStrictEqual([false, "false", "true"]);
});

it("shows a check and asks to stop keeping it unread next", async () => {
await setupController();

submit(".story-keep-unread-form");

const copy = form(".story-keep-unread-form");
const input = ensure(copy.querySelector("input"));
expect([icon(".story-keep-unread-form"), input.value])
.toStrictEqual(["fa fa-check", "false"]);
});
});

describe("submitted for the refresh form", () => {
it("marks the form busy for a moment", async () => {
vi.useFakeTimers();
await setupController();

submit(".story-refresh-form");
const busy = form(".story-refresh-form").getAttribute("aria-busy");
vi.runAllTimers();

expect([busy, form(".story-refresh-form").hasAttribute("aria-busy")])
.toStrictEqual(["true", false]);
});
});
3 changes: 1 addition & 2 deletions spec/javascript/support/story_fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ void {
document.body.innerHTML = [
"<li class='story'",
" data-controller='story'",
" data-action='submit->story#formSubmitted",
" turbo:frame-missing->story#frameMissing'",
" data-action='turbo:frame-missing->story#frameMissing'",
` data-story-id-value='${id}'`,
` data-story-is-read-value='${isRead}'`,
` data-story-keep-unread-value='${keepUnread}'>`,
Expand Down
Loading
Loading