Introduce a simple stimulus controller for the magic link form

which avoids the double-submit problem from:

- https://app.fizzy.do/5986089/cards/2980
- https://app.fizzy.do/5986089/cards/2771
This commit is contained in:
Mike Dalessio
2025-11-17 14:43:43 -05:00
parent a7ff18c437
commit 6a3135b67a
2 changed files with 21 additions and 2 deletions
@@ -0,0 +1,19 @@
import { Controller } from "@hotwired/stimulus"
import { onNextEventLoopTick } from "helpers/timing_helpers"
export default class extends Controller {
static targets = [ "input" ]
submit(event) {
onNextEventLoopTick(() => {
this.inputTarget.disabled = true
})
}
paste(event) {
onNextEventLoopTick(() => {
this.element.submit()
this.inputTarget.disabled = true
})
}
}
+2 -2
View File
@@ -6,11 +6,11 @@
<p class="margin-none-block-start txt-medium">Then enter the verification code included in the email below:</p>
</header>
<%= form_with url: session_magic_link_path, method: :post, html: { data: { controller: token_list("form", "auto-submit" => params[:code].present?)} } do |form| %>
<%= form_with url: session_magic_link_path, method: :post, html: { data: { controller: "magic-link" } } do |form| %>
<%= form.text_field :code, required: true, class: "input center txt-align-enter txt-large",
autofocus: true, autocorrect: "off", autocapitalize: "off", spellcheck: "false", "data-1p-ignore": true,
autocomplete: "one-time-code", maxlength: "6", placeholder: "••••••", value: params[:code],
data: { form_target: "input", action: "paste->form#debouncedSubmit" } %>
data: { magic_link_target: "input", action: "keydown.enter->magic-link#submit paste->magic-link#paste" } %>
<% end %>
</div>