Skip to content

Commit

Permalink
Reimplement redirect and dismiss as custom actions
Browse files Browse the repository at this point in the history
  • Loading branch information
sfnelson committed Oct 27, 2023
1 parent 3bad3de commit 9b12a26
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 92 deletions.
6 changes: 2 additions & 4 deletions app/javascript/kpop/application.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import CloseController from "../kpop/controllers/close_controller";
import FrameController from "../kpop/controllers/frame_controller";
import ModalController from "../kpop/controllers/modal_controller";
import RedirectController from "../kpop/controllers/redirect_controller";
import ScrimController from "../kpop/controllers/scrim_controller";

import "./turbo_actions";

const Definitions = [
{ identifier: "kpop--close", controllerConstructor: CloseController },
{ identifier: "kpop--frame", controllerConstructor: FrameController },
{ identifier: "kpop--modal", controllerConstructor: ModalController },
{ identifier: "kpop--redirect", controllerConstructor: RedirectController },
{ identifier: "scrim", controllerConstructor: ScrimController },
];

Expand Down
9 changes: 0 additions & 9 deletions app/javascript/kpop/controllers/close_controller.js

This file was deleted.

17 changes: 0 additions & 17 deletions app/javascript/kpop/controllers/frame_controller.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
import { Controller } from "@hotwired/stimulus";
import { Turbo } from "@hotwired/turbo-rails";

import DEBUG from "../debug";
import { ContentModal } from "../modals/content_modal";
import { FrameModal } from "../modals/frame_modal";
import { StreamModal } from "../modals/stream_modal";
import { StreamRenderer } from "../modals/stream_renderer";

Turbo.StreamActions.kpop_open = function () {
const frame = () => {
return this.targetElements[0];
};
const animate = !frame?.kpop?.openValue;

frame()
.kpop.dismiss({ animate, reason: "before-turbo-stream" })
.then(() => {
new StreamRenderer(frame(), this).render();
frame().kpop.open(new StreamModal(this.target, this), { animate });
});
};

export default class Kpop__FrameController extends Controller {
static outlets = ["scrim"];
Expand Down
22 changes: 0 additions & 22 deletions app/javascript/kpop/controllers/redirect_controller.js

This file was deleted.

33 changes: 33 additions & 0 deletions app/javascript/kpop/turbo_actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Turbo } from "@hotwired/turbo-rails";

import { StreamModal } from "./modals/stream_modal";
import { StreamRenderer } from "./utils/stream_renderer";

function kpop(action) {
return action.targetElements[0]?.kpop;
}

Turbo.StreamActions.kpop_open = function () {
const animate = !kpop(this).openValue;

kpop(this)
?.dismiss({ animate, reason: "before-turbo-stream" })
.then(() => {
new StreamRenderer(this.targetElements[0], this).render();
kpop(this)?.open(new StreamModal(this.target, this), { animate });
});
};

Turbo.StreamActions.kpop_dismiss = function () {
kpop(this)?.dismiss({ reason: "turbo_stream.kpop.dismiss" });
};

Turbo.StreamActions.kpop_redirect_to = function () {
if (this.dataset.turboFrame === this.target) {
this.targetElements[0].src = this.getAttribute("href");
} else {
Turbo.visit(this.getAttribute("href"), {
action: this.dataset.turboAction,
});
}
};
8 changes: 4 additions & 4 deletions lib/katalyst/kpop/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require "katalyst/kpop/matchers/chained_matcher"
require "katalyst/kpop/matchers/frame_matcher"
require "katalyst/kpop/matchers/modal_matcher"
require "katalyst/kpop/matchers/redirect_finder"
require "katalyst/kpop/matchers/redirect_matcher"
require "katalyst/kpop/matchers/response_matcher"
require "katalyst/kpop/matchers/stream_matcher"
Expand All @@ -22,7 +21,9 @@ module Matchers
# @example
# expect(response).to kpop_dismiss
def kpop_dismiss(id: "kpop")
ChainedMatcher.new(ResponseMatcher, CapybaraParser, StreamMatcher.new(id:, action: "append"))
ChainedMatcher.new(ResponseMatcher,
CapybaraParser,
StreamMatcher.new(id:, action: "kpop_dismiss"))
end

# @api public
Expand All @@ -36,8 +37,7 @@ def kpop_redirect_to(target, id: "kpop")

ChainedMatcher.new(ResponseMatcher,
CapybaraParser,
StreamMatcher.new(id:, action: "append"),
RedirectFinder,
StreamMatcher.new(id:, action: "kpop_redirect_to"),
RedirectMatcher.new(target))
end

Expand Down
16 changes: 0 additions & 16 deletions lib/katalyst/kpop/matchers/redirect_finder.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/katalyst/kpop/matchers/redirect_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Matchers
# @api private
class RedirectMatcher < Base
def match(expected, actual)
actual["data-kpop--redirect-path-value"].to_s.match?(expected)
actual["href"].to_s.match?(expected)
end

def description
Expand Down
32 changes: 13 additions & 19 deletions lib/katalyst/kpop/turbo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Katalyst
module Kpop
module Turbo
class TagBuilder
delegate :action, :append, :tag, to: :@builder
delegate :action, :turbo_stream_action_tag, to: :@builder

def initialize(builder)
@builder = builder
Expand All @@ -24,31 +24,25 @@ def initialize(builder)
# <% end %>
# <% end %>
def open(content = nil, id: "kpop", **, &)
@builder.action(:kpop_open, id, content, **, &)
action(:kpop_open, id, content, **, &)
end

# Render a turbo stream action that will dismiss any open kpop modal.
def dismiss(id: "kpop")
append(id) do
tag.div("", data: {
controller: "kpop--close",
kpop__close_kpop__frame_outlet: "##{id}",
turbo_temporary: "",
})
end
turbo_stream_action_tag(:kpop_dismiss, target: id)
end

# Renders a kpop redirect controller response that will escape the frame and navigate to the given URL.
def redirect_to(url, id: "kpop", target: nil)
append(id) do
tag.div("", data: {
controller: "kpop--redirect",
kpop__redirect_kpop__frame_outlet: "##{id}",
kpop__redirect_path_value: url,
kpop__redirect_target_value: target,
turbo_temporary: "",
})
end
def redirect_to(href, id: "kpop", action: "replace", target: nil)
turbo_stream_action_tag(
:kpop_redirect_to,
target: id,
href:,
data: {
turbo_action: action,
turbo_frame: target,
},
)
end
end
end
Expand Down

0 comments on commit 9b12a26

Please sign in to comment.