Add vendor pre-population for bulk code and individual edit forms
- Add vendor-changed HTMX handlers for both bulk code and individual edit - Pre-populate default account at 100% when vendor is selected and no accounts exist - Fix render-accounts-section to render from step-params correctly - Change bulk code vendor-changed from hx-get to hx-post to include form data - Add routes for vendor-changed endpoints - Update e2e tests to cover vendor pre-population - Run lein cljfmt fix across codebase
This commit is contained in:
@@ -142,6 +142,12 @@
|
||||
true (dissoc :vendor/account-overrides :vendor/terms-overrides))]
|
||||
vendor)))
|
||||
|
||||
(defn vendor-default-account [vendor-id client-id]
|
||||
(when vendor-id
|
||||
(let [vendor (get-vendor vendor-id)
|
||||
clientized (clientize-vendor vendor client-id)]
|
||||
(:vendor/default-account clientized))))
|
||||
|
||||
(defn location-select*
|
||||
[{:keys [name account-location client-locations value]}]
|
||||
(let [options (into (cond account-location
|
||||
@@ -904,18 +910,23 @@
|
||||
(transaction-rules-view request)]
|
||||
[:div {:x-show "activeForm === 'manual'", :x-transition:enter "transition ease-out duration-500", :x-transition:enter-start "opacity-0 transform scale-95", :x-transition:enter-end "opacity-100 transform scale-100"}
|
||||
[:div {}
|
||||
(fc/with-field :transaction/vendor
|
||||
(com/validated-field
|
||||
{:label "Vendor"
|
||||
:errors (fc/field-errors)}
|
||||
[:div.w-96
|
||||
(com/typeahead {:name (fc/field-name)
|
||||
:error? (fc/error?)
|
||||
:class "w-96"
|
||||
:placeholder "Search..."
|
||||
:url (bidi/path-for ssr-routes/only-routes :vendor-search)
|
||||
:value (fc/field-value)
|
||||
:content-fn (fn [c] (pull-attr (dc/db conn) :vendor/name c))})]))
|
||||
[:div {:hx-trigger "change"
|
||||
:hx-get (bidi/path-for ssr-routes/only-routes ::route/edit-vendor-changed)
|
||||
:hx-target "#account-grid-body"
|
||||
:hx-swap "outerHTML"
|
||||
:hx-include "closest form"}
|
||||
(fc/with-field :transaction/vendor
|
||||
(com/validated-field
|
||||
{:label "Vendor"
|
||||
:errors (fc/field-errors)}
|
||||
[:div.w-96
|
||||
(com/typeahead {:name (fc/field-name)
|
||||
:error? (fc/error?)
|
||||
:class "w-96"
|
||||
:placeholder "Search..."
|
||||
:url (bidi/path-for ssr-routes/only-routes :vendor-search)
|
||||
:value (fc/field-value)
|
||||
:content-fn (fn [c] (pull-attr (dc/db conn) :vendor/name c))})]))]
|
||||
|
||||
;; Memo field
|
||||
|
||||
@@ -1381,6 +1392,35 @@
|
||||
[]
|
||||
entity)))
|
||||
|
||||
(defn edit-vendor-changed-handler [request]
|
||||
(let [snapshot (:snapshot (:multi-form-state request))
|
||||
client-id (or (:transaction/client snapshot)
|
||||
(-> request :entity :transaction/client :db/id))
|
||||
vendor-id (:transaction/vendor snapshot)
|
||||
total (Math/abs (or (:transaction/amount snapshot) 0.0))
|
||||
amount-mode (or (:amount-mode snapshot) "$")]
|
||||
(if (and (empty? (:transaction/accounts snapshot))
|
||||
vendor-id
|
||||
client-id)
|
||||
(if-let [default-account (vendor-default-account vendor-id client-id)]
|
||||
(let [new-account {:db/id (str (java.util.UUID/randomUUID))
|
||||
:transaction-account/account (:db/id default-account)
|
||||
:transaction-account/location (or (:account/location default-account) "Shared")}
|
||||
new-account (if (= amount-mode "%")
|
||||
(assoc new-account :transaction-account/amount 100.0)
|
||||
(assoc new-account :transaction-account/amount total))
|
||||
updated-snapshot (assoc snapshot :transaction/accounts [new-account])
|
||||
updated-request (assoc-in request [:multi-form-state :snapshot] updated-snapshot)]
|
||||
(html-response
|
||||
[:div#account-grid-body
|
||||
(account-grid-body* updated-request)]))
|
||||
(html-response
|
||||
[:div#account-grid-body
|
||||
(account-grid-body* request)]))
|
||||
(html-response
|
||||
[:div#account-grid-body
|
||||
(account-grid-body* request)]))))
|
||||
|
||||
(def key->handler
|
||||
(apply-middleware-to-all-handlers
|
||||
{::route/edit-wizard (-> mm/open-wizard-handler
|
||||
@@ -1399,6 +1439,9 @@
|
||||
(wrap-entity [:multi-form-state :snapshot :db/id] d-transactions/default-read)
|
||||
(mm/wrap-wizard edit-wizard)
|
||||
(mm/wrap-decode-multi-form-state))
|
||||
::route/edit-vendor-changed (-> edit-vendor-changed-handler
|
||||
(mm/wrap-wizard edit-wizard)
|
||||
(mm/wrap-decode-multi-form-state))
|
||||
::route/location-select (-> location-select
|
||||
(wrap-schema-enforce :query-schema [:map
|
||||
[:name :string]
|
||||
|
||||
Reference in New Issue
Block a user