Fix toggle-amount-mode: avoid cursor context entirely for HTMX re-render

The previous attempts to set up form cursor context in toggle-amount-mode
were failing because the cursor library's dynamic binding model is complex
and requires specific initialization through fc/start-form.

Instead of trying to recreate the cursor context, this fix:
1. Creates transaction-account-row-no-cursor* that renders rows with explicit
   field names and values (no cursor functions)
2. Rewrites toggle-amount-mode to directly construct the data-grid HTML
   using map-indexed over the accounts vector
3. Removes the broken manual cursor binding attempts
4. Removes unused auto-ap.cursor import

This ensures the toggle handler works independently of the wizard's cursor
context while still producing identical HTML output.
This commit is contained in:
2026-05-21 07:44:43 -07:00
parent 857a1536ef
commit acd4184ef0

View File

@@ -23,7 +23,6 @@
[auto-ap.ssr.grid-page-helper :as helper]
[auto-ap.ssr.transaction.common :refer [grid-page]]
[auto-ap.ssr.components.multi-modal :as mm]
[auto-ap.cursor :as cursor]
[auto-ap.ssr.form-cursor :as fc]
[auto-ap.ssr.hx :as hx]
[auto-ap.ssr.svg :as svg]
@@ -229,6 +228,52 @@
(com/data-grid-cell {:class "align-top"}
(com/a-icon-button {"@click.prevent.stop" "show=false; setTimeout(() => $refs.p.remove(), 500)"} svg/x))))
(defn- account-field-name [index field]
(str "step-params[transaction/accounts][" index "][" (name field) "]"))
(defn transaction-account-row-no-cursor* [{:keys [account index client-id amount-mode total]}]
(com/data-grid-row
(-> {:x-data (hx/json {:show true
:accountId (:transaction-account/account account)})
:data-key "show"
:x-ref "p"}
hx/alpine-mount-then-appear)
(com/hidden {:name (account-field-name index :db/id)
:value (or (:db/id account) "")})
(com/data-grid-cell
{}
(com/validated-field
{}
(account-typeahead* {:value (:transaction-account/account account)
:client-id client-id
:name (account-field-name index :transaction-account/account)
:x-model "accountId"})))
(com/data-grid-cell
{}
(com/validated-field
{}
(location-select* {:name (account-field-name index :transaction-account/location)
:account-location (:account/location (cond->> (:transaction-account/account account)
(nat-int? (:transaction-account/account account)) (dc/pull (dc/db conn)
'[:account/location])))
:client-locations (pull-attr (dc/db conn) :client/locations client-id)
:value (:transaction-account/location account)})))
(com/data-grid-cell
{}
(com/validated-field
{}
(if (= "%" amount-mode)
(com/text-input {:name (account-field-name index :transaction-account/amount)
:class "w-16"
:value (:transaction-account/amount account)
:type "number"
:step "0.01"})
(com/money-input {:name (account-field-name index :transaction-account/amount)
:class "w-16"
:value (:transaction-account/amount account)}))))
(com/data-grid-cell {:class "align-top"}
(com/a-icon-button {"@click.prevent.stop" "show=false; setTimeout(() => $refs.p.remove(), 500)"} svg/x))))
(defn location-select [{{:keys [name account-id client-id value] :as qp} :query-params}]
(html-response (location-select* {:name name
:value value
@@ -363,15 +408,61 @@
accounts (convert-accounts-mode (:transaction/accounts snapshot) old-mode new-mode total)
updated-request (-> request
(assoc-in [:multi-form-state :snapshot :transaction/accounts] accounts)
(assoc-in [:multi-form-state :snapshot :amount-mode] new-mode))
mfs-cursor (cursor/cursor (:multi-form-state updated-request))
accounts-cursor (get (get (get mfs-cursor :snapshot) :step-params) :transaction/accounts)]
(binding [fc/*form-data* (:multi-form-state updated-request)
fc/*form-errors* {}
fc/*current* accounts-cursor]
(html-response
[:div#account-grid-body
(account-grid-body* updated-request)]))))
(assoc-in [:multi-form-state :snapshot :amount-mode] new-mode))]
(html-response
[:div#account-grid-body
(com/data-grid {:headers [(com/data-grid-header {} "Account")
(com/data-grid-header {:class "w-32"} "Location")
(com/data-grid-header {:class "w-16"}
(com/radio-card {:options [{:value "$" :content "$"}
{:value "%" :content "%"}]
:value new-mode
:name "step-params[amount-mode]"
:orientation :horizontal
:hx-post (bidi/path-for ssr-routes/only-routes ::route/toggle-amount-mode)
:hx-target "#account-grid-body"
:hx-swap "outerHTML"
:hx-include "closest form"}))
(com/data-grid-header {:class "w-16"})]}
(map-indexed (fn [idx account]
(transaction-account-row-no-cursor*
{:account account
:index idx
:client-id (-> updated-request :entity :transaction/client :db/id)
:amount-mode new-mode
:total total}))
accounts)
(com/data-grid-new-row {:colspan 4
:hx-get (bidi/path-for ssr-routes/only-routes
::route/edit-wizard-new-account)
:row-offset 0
:index (count accounts)
:tr-params {:hx-vals (hx/json {:client-id (:transaction/client snapshot)})}}
"New account")
(com/data-grid-row {}
(com/data-grid-cell {})
(com/data-grid-cell {:class "text-right"} [:span.font-bold.text-right "TOTAL"])
(com/data-grid-cell {:id "total"
:class "text-right"}
(format "$%,.2f" (reduce + 0 (map :transaction-account/amount accounts))))
(com/data-grid-cell {}))
(com/data-grid-row {}
(com/data-grid-cell {})
(com/data-grid-cell {:class "text-right"} [:span.font-bold.text-right "BALANCE"])
(com/data-grid-cell {:id "total"
:class "text-right"}
(let [account-total (reduce + 0 (map :transaction-account/amount accounts))
balance (- total account-total)]
[:span {:class (when-not (dollars= 0.0 balance)
"text-red-300")}
(format "$%,.2f" balance)]))
(com/data-grid-cell {}))
(com/data-grid-row {}
(com/data-grid-cell {})
(com/data-grid-cell {:class "text-right"} [:span.font-bold.text-right "TRANSACTION TOTAL"])
(com/data-grid-cell {:class "text-right"}
(format "$%,.2f" total))
(com/data-grid-cell {})))])))
(defrecord BasicDetailsStep [linear-wizard]
mm/ModalWizardStep