Made accounts page look great
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
audit-transact
|
||||
conn
|
||||
merge-query
|
||||
pull-attr
|
||||
pull-many
|
||||
query2]]
|
||||
[auto-ap.query-params :as query-params]
|
||||
@@ -17,17 +18,19 @@
|
||||
[auto-ap.ssr.components :as com]
|
||||
[auto-ap.ssr.form-cursor :as fc]
|
||||
[auto-ap.ssr.grid-page-helper :as helper]
|
||||
[auto-ap.ssr.hx :as hx]
|
||||
[auto-ap.ssr.nested-form-params :refer [wrap-nested-form-params]]
|
||||
[auto-ap.ssr.svg :as svg]
|
||||
[auto-ap.ssr.utils
|
||||
:refer [apply-middleware-to-all-handlers
|
||||
entity-id
|
||||
field-validation-error
|
||||
form-validation-error
|
||||
html-response
|
||||
many-entity
|
||||
ref->enum-schema
|
||||
ref->select-options
|
||||
temp-id
|
||||
validation-error
|
||||
wrap-form-4xx-2
|
||||
wrap-schema-decode]]
|
||||
[bidi.bidi :as bidi]
|
||||
@@ -181,7 +184,28 @@
|
||||
(= :post request-method) (assoc :db/id "new"))
|
||||
_ (cond (= :post request-method)
|
||||
(when-let [extant (seq (dc/q '[:find ?x :in $ ?nc :where [?x :account/numeric-code ?nc]] (dc/db conn) (:account/numeric-code entity)))]
|
||||
(validation-error (format "The code %d is already in use." (:account/numeric-code entity)))))
|
||||
(field-validation-error (format "The code %d is already in use." (:account/numeric-code entity))
|
||||
[:account/numeric-code]
|
||||
:form form-params))
|
||||
)
|
||||
;; TODO the following would work better if the schema was hydrated automatically with needed values
|
||||
_ (some->> form-params
|
||||
:account/client-overrides
|
||||
(group-by :account-client-override/client)
|
||||
(filter (fn [[client overrides]]
|
||||
(> (count overrides) 1)))
|
||||
(map first)
|
||||
seq
|
||||
(#(form-validation-error (format "Client(s) %s have more than one override."
|
||||
(str/join ", "
|
||||
(map (fn [client]
|
||||
(format "'%s'" (pull-attr (dc/db conn)
|
||||
:client/name
|
||||
(-> client)))
|
||||
) %)))
|
||||
:form form-params))
|
||||
|
||||
)
|
||||
{:keys [tempids]} (audit-transact [[:upsert-entity (cond-> entity
|
||||
(:account/numeric-code entity) (assoc :account/code (str (:account/numeric-code entity))))]]
|
||||
(:identity request))
|
||||
@@ -213,11 +237,21 @@
|
||||
;; TODO use cursor
|
||||
;; TODO index based list not dbid
|
||||
|
||||
;; TODO lots of weird edge cases with indexes
|
||||
;; for example, what happens when you have 0, 1, 2, but then delete 1?
|
||||
;; what happens when you delete 2 but then add another one?
|
||||
;; preference is that the field parsing logic does better grouping of what
|
||||
;; goes with what, building index on the server side
|
||||
;; not needing to pass index in
|
||||
|
||||
(defn client-override* [override]
|
||||
[:div.flex.gap-2.mb-2.client-override
|
||||
[:div.flex.gap-2.mb-2.client-override (-> {"x-ref" "p"
|
||||
:data-key "show"
|
||||
}
|
||||
hx/alpine-mount-then-appear)
|
||||
[:div.w-96
|
||||
(fc/with-field :db/id
|
||||
(com/hidden {:name (fc/field-name)
|
||||
(com/hidden {:name (fc/field-name)
|
||||
:value (fc/field-value)}))
|
||||
(fc/with-field :account-client-override/client
|
||||
(com/validated-field {:errors (fc/field-errors)}
|
||||
@@ -226,15 +260,17 @@
|
||||
:url (bidi/path-for ssr-routes/only-routes
|
||||
:company-search)
|
||||
:value (fc/field-value)
|
||||
:value-fn :db/id ;; TODO hydration something here
|
||||
:content-fn :client/name})))]
|
||||
:value-fn (some-fn :db/id identity) ;; TODO better hydration
|
||||
:content-fn (fn [value]
|
||||
(:client/name (cond->> value
|
||||
(nat-int? value) (dc/pull (dc/db conn) [:client/name]))))})))]
|
||||
(fc/with-field :account-client-override/name
|
||||
[:div.w-96
|
||||
(com/validated-field {:errors (fc/field-errors)}
|
||||
(com/text-input {:name (fc/field-name)
|
||||
:class "w-full"
|
||||
:value (fc/field-value)}))])
|
||||
[:div (com/a-icon-button {"_" (hiccup/raw "on click halt the event then transition the closest <.client-override />'s opacity to 0 then remove closest <.client-override />")} svg/x)]])
|
||||
[:div (com/a-icon-button {"@click.prevent.stop" "show=false; setTimeout(() => $refs.p.remove(), 500)"} svg/x)]])
|
||||
|
||||
;; TODO each form:
|
||||
;; elimante typeahead1
|
||||
@@ -246,94 +282,108 @@
|
||||
;; componentize
|
||||
;; ensure all dependency oriented stuff works the same way
|
||||
;; make sure that "new row index" stuff works ok
|
||||
;; TODO figure out when hx-targets are decided
|
||||
;; ensure that adding a new one results in a new row
|
||||
|
||||
|
||||
(defn dialog* [& {:keys [account form-params form-errors]}]
|
||||
(com/modal
|
||||
{}
|
||||
[:form#edit-form (merge {:hx-ext "response-targets"
|
||||
:hx-swap "outerHTML swap:300ms"
|
||||
:hx-target-400 "#form-errors .error-content"}
|
||||
form-params)
|
||||
[:fieldset {:class "hx-disable"}
|
||||
(com/modal-card
|
||||
{}
|
||||
[:div.flex [:div.p-2 "Account"] [:p.ml-2.rounded.bg-gray-200.p-2.dark:bg-gray-600 (:account/numeric-code account) " - " (:account/name account)]]
|
||||
(defn dialog* [& {:keys [entity form-params form-errors]}]
|
||||
(fc/start-form entity form-errors
|
||||
[:div {:x-data (hx/json {"accountName" (:account/name entity)
|
||||
"accountCode" (:account/numeric-code entity)})}
|
||||
(com/modal
|
||||
{}
|
||||
[:form#edit-form (merge {:hx-ext "response-targets"
|
||||
:hx-swap "outerHTML swap:300ms"
|
||||
:hx-target-400 "#form-errors .error-content"}
|
||||
form-params)
|
||||
[:fieldset {:class "hx-disable"}
|
||||
(com/modal-card
|
||||
{}
|
||||
[:div.flex [:div.p-2 "Account"] [:p.ml-2.rounded.bg-gray-200.p-2.dark:bg-gray-600
|
||||
[:span {:x-text "accountCode"}]
|
||||
" - "
|
||||
[:span {:x-text "accountName"}]]]
|
||||
[:div.space-y-1
|
||||
(when-let [id (:db/id entity)]
|
||||
(com/hidden {:name "db/id"
|
||||
:value id}))
|
||||
|
||||
(fc/start-form
|
||||
account form-errors
|
||||
[:div.space-y-1
|
||||
(when-let [id (:db/id account)]
|
||||
(com/hidden {:name "db/id"
|
||||
:value id}))
|
||||
(fc/with-field :account/numeric-code
|
||||
(if (nil? (:db/id entity))
|
||||
(com/validated-field {:label "Numeric code"
|
||||
:errors (fc/field-errors)}
|
||||
(com/text-input {:name (fc/field-name)
|
||||
:x-model "accountCode"
|
||||
:autofocus true
|
||||
:class "w-32"}))
|
||||
(com/hidden {:name (fc/field-name)
|
||||
:value (fc/field-value)})))
|
||||
(fc/with-field :account/name
|
||||
(com/validated-field {:label "Name"
|
||||
:errors (fc/field-errors)}
|
||||
(com/text-input {:name (fc/field-name)
|
||||
:x-model "accountName"
|
||||
|
||||
(fc/with-field :account/numeric-code
|
||||
(when (nil? (fc/field-value))
|
||||
(com/validated-field {:label "Numeric code"
|
||||
:errors (fc/field-errors)}
|
||||
(com/text-input {:name (fc/field-name)
|
||||
:autofocus true
|
||||
:class "w-32"}))))
|
||||
(fc/with-field :account/name
|
||||
(com/validated-field {:label "Name"
|
||||
:errors (fc/field-errors)}
|
||||
(com/text-input {:name (fc/field-name)
|
||||
:autofocus true
|
||||
:class "w-32"
|
||||
:value (fc/field-value)})))
|
||||
(fc/with-field :account/type
|
||||
(com/validated-field {:label "Account Type"
|
||||
:errors (fc/field-errors)}
|
||||
(com/select {:name (fc/field-name)
|
||||
:class "w-36"
|
||||
:id "type"
|
||||
:value (some-> (fc/field-value) name)
|
||||
:options (ref->select-options "account-type")})))
|
||||
(fc/with-field :account/location
|
||||
(com/validated-field {:label "Location"
|
||||
:errors (fc/field-errors)}
|
||||
(com/text-input {:name (fc/field-name)
|
||||
:class "w-16"
|
||||
:value (fc/field-value)})))
|
||||
:class "w-64"
|
||||
:value (fc/field-value)})))
|
||||
(fc/with-field :account/type
|
||||
(com/validated-field {:label "Account Type"
|
||||
:errors (fc/field-errors)}
|
||||
(com/select {:name (fc/field-name)
|
||||
:class "w-36"
|
||||
:id "type"
|
||||
:value (some-> (fc/field-value) name)
|
||||
:options (ref->select-options "account-type")})))
|
||||
(fc/with-field :account/location
|
||||
(com/validated-field {:label "Location"
|
||||
:errors (fc/field-errors)}
|
||||
(com/text-input {:name (fc/field-name)
|
||||
:class "w-16"
|
||||
:value (fc/field-value)})))
|
||||
|
||||
(fc/with-field :account/invoice-allowance
|
||||
(com/validated-field {:label "Invoice Allowance"
|
||||
:errors (fc/field-errors)}
|
||||
(com/select {:name (fc/field-name)
|
||||
:value (some-> (fc/field-value) name)
|
||||
:class "w-36"
|
||||
:options (ref->select-options "allowance")})))
|
||||
(fc/with-field :account/vendor-allowance
|
||||
(com/validated-field {:label "Vendor Allowance"
|
||||
:errors (fc/field-errors)}
|
||||
(com/select {:name (fc/field-name)
|
||||
:class "w-36"
|
||||
:value (some-> (fc/field-value) name)
|
||||
:options (ref->select-options "allowance")})))
|
||||
(fc/with-field :account/applicability
|
||||
(com/validated-field {:label "Applicability"
|
||||
:errors (fc/field-errors)}
|
||||
(com/select {:name (fc/field-name)
|
||||
:class "w-36"
|
||||
:value (some-> (fc/field-value) name)
|
||||
:options (ref->select-options "account-applicability")})))
|
||||
[:div.flex.flex-wrap.gap-4
|
||||
(fc/with-field :account/invoice-allowance
|
||||
(com/validated-field {:label "Invoice Allowance"
|
||||
:errors (fc/field-errors)}
|
||||
(com/select {:name (fc/field-name)
|
||||
:value (some-> (fc/field-value) name)
|
||||
:class "w-36"
|
||||
:options (ref->select-options "allowance")})))
|
||||
(fc/with-field :account/vendor-allowance
|
||||
(com/validated-field {:label "Vendor Allowance"
|
||||
:errors (fc/field-errors)}
|
||||
(com/select {:name (fc/field-name)
|
||||
:class "w-36"
|
||||
:value (some-> (fc/field-value) name)
|
||||
:options (ref->select-options "allowance")})))]
|
||||
(fc/with-field :account/applicability
|
||||
(com/validated-field {:label "Applicability"
|
||||
:errors (fc/field-errors)}
|
||||
(com/select {:name (fc/field-name)
|
||||
:class "w-36"
|
||||
:value (some-> (fc/field-value) name)
|
||||
:options (ref->select-options "account-applicability")})))
|
||||
|
||||
(fc/with-field :account/client-overrides
|
||||
(com/field {:label "Client Overrides" :id "client-overrides"}
|
||||
(when (fc/field-value)
|
||||
(doall
|
||||
(for [override fc/*current*]
|
||||
(fc/with-cursor override
|
||||
(client-override* override)))))))
|
||||
(com/a-button {:hx-get (bidi/path-for ssr-routes/only-routes
|
||||
:admin-account-client-override-new)
|
||||
:hx-vals (hiccup/raw "js:{index: document.getElementById('client-overrides').children.length - 1}")
|
||||
:hx-target "#client-overrides"
|
||||
:hx-swap "beforeend"}
|
||||
"New override")
|
||||
[:div#form-errors [:span.error-content]]])
|
||||
(com/validated-save-button {:errors []} ;; TODO
|
||||
"Save account"))]]))
|
||||
(fc/with-field :account/client-overrides
|
||||
(com/field {:label "Client Overrides" :id "client-overrides"}
|
||||
(when (fc/field-value)
|
||||
(doall
|
||||
(for [override fc/*current*]
|
||||
(fc/with-cursor override
|
||||
(client-override* override)))))))
|
||||
(com/a-button {:hx-get (bidi/path-for ssr-routes/only-routes
|
||||
:admin-account-client-override-new)
|
||||
:hx-vals (hiccup/raw "js:{index: document.getElementById('client-overrides').children.length - 1}")
|
||||
:hx-target "#client-overrides"
|
||||
:hx-swap "beforeend"}
|
||||
"New override")
|
||||
]
|
||||
[:div
|
||||
[:div [:div#form-errors (when (:errors fc/*form-errors*)
|
||||
[:span.error-content
|
||||
(com/errors {:errors (:errors fc/*form-errors*)})])]]
|
||||
(com/validated-save-button {:errors (seq form-errors)}
|
||||
"Save account")])]])]))
|
||||
|
||||
(defn new-client-override [{ {:keys [index]} :query-params}]
|
||||
(let [index (or index 0)
|
||||
@@ -346,14 +396,15 @@
|
||||
|
||||
(defn account-edit-dialog [request]
|
||||
(let [account (some-> request :route-params :db/id (#(dc/pull (dc/db conn) default-read %)))]
|
||||
(html-response (dialog* :account account
|
||||
(html-response (dialog* :entity account
|
||||
:form-params {:hx-put (str (bidi/path-for ssr-routes/only-routes
|
||||
:admin-account-edit-save))})
|
||||
:headers {"hx-trigger" "modalopen"})))
|
||||
|
||||
|
||||
(defn account-new-dialog [_]
|
||||
(html-response (dialog* :account nil
|
||||
(html-response (dialog* :entity {}
|
||||
:form-errors {}
|
||||
:form-params {:hx-post (str (bidi/path-for ssr-routes/only-routes
|
||||
:admin-account-new-save))})
|
||||
:headers {"hx-trigger" "modalopen"}))
|
||||
@@ -362,7 +413,7 @@
|
||||
;; TODO hydration
|
||||
;; TODO consistency of error handling and passing, on all form examples
|
||||
(let [entity (some-> request :last-form)]
|
||||
(html-response (dialog* :account entity
|
||||
(html-response (dialog* :entity entity
|
||||
:form-errors (:form-errors request)
|
||||
:form-params (if (:db/id entity)
|
||||
{:hx-put (str (bidi/path-for ssr-routes/only-routes
|
||||
|
||||
Reference in New Issue
Block a user