improvements.

This commit is contained in:
Bryce
2024-03-21 22:24:28 -07:00
parent 87952b79d1
commit 9cf81f6d15
4 changed files with 78 additions and 418 deletions

View File

@@ -2,7 +2,6 @@
(:require [auto-ap.datomic
:refer [conn pull-attr]]
[auto-ap.datomic.accounts :as d-accounts]
[auto-ap.logging :as alog]
[auto-ap.routes.invoice :as route]
[auto-ap.routes.utils
:refer [wrap-client-redirect-unauthenticated]]
@@ -15,7 +14,7 @@
[auto-ap.ssr.svg :as svg]
[auto-ap.ssr.utils
:refer [apply-middleware-to-all-handlers entity-id html-response
wrap-schema-enforce]]
money wrap-schema-enforce]]
[auto-ap.time :as atime]
[bidi.bidi :as bidi]
[datomic.api :as dc]
@@ -24,8 +23,14 @@
(def new-form-schema
[:map
[:invoice/client [:entity-map {:pull [:db/id :client/name :client/accounts]}]]
[:invoice/vendor [:entity-map {:pull [:db/id :vendor/name]}]]])
[:invoice/client entity-id]
[:invoice/vendor entity-id]
[:invoice/expense-accounts
[:vector {:coerce? true}
[:map
[:invoice-expense-account/account entity-id]
[:invoice-expense-account/location :string]
[:invoice-expense-account/amount money]]]]])
(defrecord BasicDetailsStep [linear-wizard]
mm/ModalWizardStep
@@ -160,58 +165,53 @@
(:account/name (d-accounts/clientize (dc/pull (dc/db conn) d-accounts/default-read value)
client-id)))})])
(defn- transaction-rule-account-row*
[account client-id client-locations]
(defn- invoice-expense-account-row*
[{:keys [value client-id]}]
(com/data-grid-row
(-> {:x-data (hx/json {:show (boolean (not (fc/field-value (:new? account))))
:accountId (:db/id (fc/field-value (:invoice/account account)))})
(-> {:x-data (hx/json {:show (boolean (not (fc/field-value (:new? value))))
:accountId (:db/id (fc/field-value (:invoice-expense-account/account value)))})
:data-key "show"
:x-ref "p"}
hx/alpine-mount-then-appear)
(let [account-name (fc/field-name (:transaction-rule-account/account account))]
(list
(fc/with-field :db/id
(com/hidden {:name (fc/field-name)
:value (fc/field-value)}))
(fc/with-field :transaction-rule-account/account
(com/data-grid-cell
{}
(com/validated-field
{:errors (fc/field-errors)}
(account-typeahead* {:value (fc/field-value)
:client-id client-id
:name (fc/field-name)
:x-model "accountId"}))))
(fc/with-field :transaction-rule-account/location
(com/data-grid-cell
{}
(com/validated-field
{:errors (fc/field-errors)
:x-hx-val:account-id "accountId"
:hx-vals (hx/json {:name (fc/field-name)
:clientId client-id})
:x-dispatch:changed "accountId"
:hx-trigger "changed"
:hx-get (bidi/path-for ssr-routes/only-routes ::route/location-select)
:hx-target "find *"
:hx-swap "outerHTML"}
(location-select* {:name (fc/field-name)
:account-location (:account/location (cond->> (:transaction-rule-account/account @account)
(nat-int? (:transaction-rule-account/account @account)) (dc/pull (dc/db conn)
'[:account/location])))
:client-locations client-locations
:value (fc/field-value)}))))
(fc/with-field :transaction-rule-account/percentage
(com/data-grid-cell
{}
(com/validated-field
{:errors (fc/field-errors)}
(com/money-input {:name (fc/field-name)
:class "w-16"
:value (some-> (fc/field-value)
(* 100)
(long))}))))))
(fc/with-field :db/id
(com/hidden {:name (fc/field-name)
:value (fc/field-value)}))
(fc/with-field :invoice-expense-account/account
(com/data-grid-cell
{}
(com/validated-field
{:errors (fc/field-errors)}
(account-typeahead* {:value (fc/field-value)
:client-id client-id
:name (fc/field-name)
:x-model "accountId"}))))
(fc/with-field :invoice-expense-account/location
(com/data-grid-cell
{}
(com/validated-field
{:errors (fc/field-errors)
:x-hx-val:account-id "accountId"
:hx-vals (hx/json {:name (fc/field-name)
:clientId client-id})
:x-dispatch:changed "accountId"
:hx-trigger "changed"
:hx-get (bidi/path-for ssr-routes/only-routes ::route/location-select)
:hx-target "find *"
:hx-swap "outerHTML"}
(location-select* {:name (fc/field-name)
:account-location (:account/location (cond->> (:invoice-expense-account/account @value)
(nat-int? (:invoice-expense-account/account @value)) (dc/pull (dc/db conn)
'[:account/location])))
:client-locations (pull-attr (dc/db conn) :client/locations client-id)
:value (fc/field-value)}))))
(fc/with-field :invoice-expense-account/amount
(com/data-grid-cell
{}
(com/validated-field
{:errors (fc/field-errors)}
(com/money-input {:name (fc/field-name)
:class "w-16"
:value (fc/field-value)}))))
(com/data-grid-cell {:class "align-top"}
(com/a-icon-button {"@click.prevent.stop" "show=false; setTimeout(() => $refs.p.remove(), 500)"} svg/x))))
@@ -226,7 +226,7 @@
[])
(step-schema [_]
(mut/select-keys (mm/form-schema linear-wizard) #{}))
(mut/select-keys (mm/form-schema linear-wizard) #{:invoice/expense-accounts}))
(render-step [this {{:keys [snapshot]} :multi-form-state :as request}]
(mm/default-render-step
@@ -235,7 +235,7 @@
:body (mm/default-step-body
{}
[:div {}
(:client/name (:invoice/client snapshot))
(pull-attr (dc/db conn) :client/name (:invoice/client snapshot))
(fc/with-field :invoice/expense-accounts
(com/validated-field
{:errors (fc/field-errors)}
@@ -243,14 +243,13 @@
(com/data-grid-header {:class "w-32"} "Location")
(com/data-grid-header {:class "w-16"} "%")
(com/data-grid-header {:class "w-16"})]}
(fc/cursor-map #(transaction-rule-account-row* %
(some->> snapshot :invoice/client :db/id)
(some->> snapshot :invoice/client :client/locations)))
(fc/cursor-map #(invoice-expense-account-row* {:value %
:client-id (:invoice/client snapshot)}))
(com/data-grid-new-row {:colspan 4
:hx-get (bidi/path-for ssr-routes/only-routes
::route/new-wizard-new-account)
:index (count (fc/field-value))
:tr-params {:hx-vals (hx/json {:client-id (:db/id (:invoice/client snapshot))})}}
:tr-params {:hx-vals (hx/json {:client-id (:invoice/client snapshot)})}}
"New account"))))])
:footer
(mm/default-step-footer linear-wizard this :validation-route ::route/new-wizard-navigate)
@@ -270,13 +269,12 @@
(mm/get-step this current-step)
(mm/get-step this :basic-details)))
(render-wizard [this {:keys [multi-form-state] :as request}]
(alog/peek ::MFS multi-form-state)
(mm/default-render-wizard
this request
:form-params
(-> mm/default-form-props
(assoc :hx-post
(str (bidi/path-for ssr-routes/only-routes ::route/pay-submit))))))
(str (bidi/path-for ssr-routes/only-routes ::route/new-invoice-submit))))))
(steps [_]
[:basic-details
:accounts])
@@ -288,7 +286,9 @@
:accounts (->AccountsStep this)}
step-key)))
(form-schema [_] new-form-schema)
(submit [this {:keys [multi-form-state request-method identity] :as request}]))
(submit [this {:keys [multi-form-state request-method identity] :as request}]
(html-response [:div]
:headers {"hx-trigger" "modalclose"})))
(def new-wizard (->NewWizard2 nil nil))
@@ -312,24 +312,27 @@
(mm/wrap-wizard new-wizard)
(mm/wrap-init-multi-form-state initial-new-wizard-state))
::route/location-select (-> location-select
(wrap-schema-enforce :query-schema [:map
[:name :string]
[:client-id {:optional true}
[:maybe entity-id]]
[:account-id {:optional true}
[:maybe entity-id]]]))
(wrap-schema-enforce :query-schema [:map
[:name :string]
[:client-id {:optional true}
[:maybe entity-id]]
[:account-id {:optional true}
[:maybe entity-id]]]))
::route/new-invoice-submit (-> mm/submit-handler
(mm/wrap-wizard new-wizard)
(mm/wrap-decode-multi-form-state))
::route/new-wizard-navigate (-> mm/next-handler
(mm/wrap-wizard new-wizard)
(mm/wrap-decode-multi-form-state))
::route/new-wizard-new-account (->
(add-new-entity-handler [:step-params :invoice/expense-accounts]
(fn render [cursor request]
(transaction-rule-account-row*
cursor
(:client-id (:query-params request))
(some->> (:client-id (:query-params request)) (pull-attr (dc/db conn) :client/locations))))
(invoice-expense-account-row*
{:value cursor
:client-id (:client-id (:query-params request))}))
(fn build-new-row [base _]
(assoc base :transaction-rule-account/location "Shared")))
(assoc base :invoice-expense-account/location "Shared")))
(wrap-schema-enforce :query-schema [:map
[:client-id {:optional true}
[:maybe entity-id]]]))}