a lot of streamlining for validation
This commit is contained in:
@@ -31,12 +31,12 @@
|
||||
ref->enum-schema
|
||||
ref->select-options
|
||||
temp-id
|
||||
wrap-entity
|
||||
wrap-form-4xx-2
|
||||
wrap-schema-decode]]
|
||||
[bidi.bidi :as bidi]
|
||||
[clojure.string :as str]
|
||||
[datomic.api :as dc]
|
||||
[hiccup2.core :as hiccup]
|
||||
[malli.core :as mc]))
|
||||
|
||||
(defn filters [request]
|
||||
@@ -291,16 +291,21 @@
|
||||
;; ensure that adding a new one results in a new row
|
||||
|
||||
|
||||
(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)})}
|
||||
(defn dialog* [{:keys [entity form-params form-errors]}]
|
||||
(fc/start-form form-params form-errors
|
||||
[:div {:x-data (hx/json {"accountName" (or (:account/name form-params) (:account/numeric-code entity))
|
||||
"accountCode" (or (:account/numeric-code form-params) (:account/numeric-code entity) )})
|
||||
:hx-target "this"}
|
||||
(com/modal
|
||||
{}
|
||||
[:form#edit-form (merge {:hx-ext "response-targets"
|
||||
:hx-swap "outerHTML swap:300ms"
|
||||
:hx-target-400 "#form-errors .error-content"}
|
||||
form-params)
|
||||
[:form#edit-form (-> {:hx-ext "response-targets"
|
||||
:hx-swap "outerHTML swap:300ms"
|
||||
:hx-target-400 "#form-errors .error-content"}
|
||||
(assoc (if (:db/id entity)
|
||||
:hx-put
|
||||
:hx-post)
|
||||
(str (bidi/path-for ssr-routes/only-routes
|
||||
:admin-transaction-rule-edit-save))))
|
||||
[:fieldset {:class "hx-disable"}
|
||||
(com/modal-card
|
||||
{}
|
||||
@@ -318,6 +323,7 @@
|
||||
(com/validated-field {:label "Numeric code"
|
||||
:errors (fc/field-errors)}
|
||||
(com/text-input {:name (fc/field-name)
|
||||
:value (fc/field-value)
|
||||
:x-model "accountCode"
|
||||
:autofocus true
|
||||
:class "w-32"}))
|
||||
@@ -388,9 +394,7 @@
|
||||
|
||||
]
|
||||
[:div
|
||||
[:div [:div#form-errors (when (:errors fc/*form-errors*)
|
||||
[:span.error-content
|
||||
(com/errors {:errors (:errors fc/*form-errors*)})])]]
|
||||
(com/form-errors {:errors (:errors fc/*form-errors*)})
|
||||
(com/validated-save-button {:errors (seq form-errors)}
|
||||
"Save account")])]])]))
|
||||
|
||||
@@ -403,34 +407,15 @@
|
||||
[]
|
||||
(client-override* fc/*current*))))
|
||||
|
||||
(defn account-edit-dialog [request]
|
||||
(let [account (some-> request :route-params :db/id (#(dc/pull (dc/db conn) default-read %)))]
|
||||
(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* :entity {}
|
||||
:form-errors {}
|
||||
:form-params {:hx-post (str (bidi/path-for ssr-routes/only-routes
|
||||
:admin-account-new-save))})
|
||||
(defn account-dialog [{:keys [entity form-params form-errors]}]
|
||||
(html-response (dialog* {:entity entity
|
||||
:form-params (or (when (seq form-params)
|
||||
form-params)
|
||||
entity
|
||||
{})
|
||||
:form-errors form-errors})
|
||||
:headers {"hx-trigger" "modalopen"}))
|
||||
|
||||
(defn account-save-error [request]
|
||||
;; TODO hydration
|
||||
;; TODO consistency of error handling and passing, on all form examples
|
||||
(let [entity (some-> request :last-form)]
|
||||
(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
|
||||
:admin-transaction-rule-edit-save))}
|
||||
{:hx-post (str (bidi/path-for ssr-routes/only-routes
|
||||
:admin-transaction-rule-edit-save))}))
|
||||
:headers {"hx-retarget" "#edit-form fieldset"
|
||||
"hx-reselect" "#edit-form fieldset"})))
|
||||
|
||||
(def account-schema (mc/schema
|
||||
[:map
|
||||
@@ -439,14 +424,14 @@
|
||||
[:account/name [:string {:min 1}]]
|
||||
[:account/location [:maybe :string]]
|
||||
[:account/type (ref->enum-schema "account-type")]
|
||||
[:account/applicability (ref->enum-schema "account-applicability")]
|
||||
[:account/applicability (ref->enum-schema "account-applicability")] ;
|
||||
[:account/invoice-allowance (ref->enum-schema "allowance")]
|
||||
[:account/vendor-allowance (ref->enum-schema "allowance")]
|
||||
[:account/client-overrides {:optional true}
|
||||
[:maybe
|
||||
(many-entity {}
|
||||
[:db/id [:or entity-id temp-id]]
|
||||
[:account-client-override/client [:or entity-id :string]]
|
||||
[:account-client-override/client entity-id]
|
||||
[:account-client-override/name [:string {:min 2}]])]]]))
|
||||
|
||||
(def key->handler
|
||||
@@ -460,12 +445,14 @@
|
||||
:default 0} [nat-int? {:default 0}]]])
|
||||
wrap-admin wrap-client-redirect-unauthenticated)
|
||||
:admin-account-save (-> account-save
|
||||
(wrap-entity [:form-params :db/id] default-read)
|
||||
(wrap-schema-decode :form-schema account-schema)
|
||||
(wrap-nested-form-params)
|
||||
(wrap-form-4xx-2 account-save-error))
|
||||
:admin-account-edit-dialog (-> account-edit-dialog
|
||||
(wrap-form-4xx-2 (wrap-entity account-dialog [:form-params :db/id] default-read)))
|
||||
:admin-account-edit-dialog (-> account-dialog
|
||||
(wrap-entity [:route-params :db/id] default-read)
|
||||
(wrap-schema-decode :route-schema [:map [:db/id entity-id]]))
|
||||
:admin-account-new-dialog account-new-dialog})
|
||||
:admin-account-new-dialog account-dialog})
|
||||
(fn [h]
|
||||
(-> h
|
||||
(wrap-admin)
|
||||
|
||||
@@ -18,9 +18,9 @@
|
||||
[auto-ap.ssr-routes :as ssr-routes]
|
||||
[auto-ap.ssr.company :refer [bank-account-typeahead*]]
|
||||
[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.form-cursor :as fc]
|
||||
[auto-ap.ssr.nested-form-params :refer [wrap-nested-form-params]]
|
||||
[auto-ap.ssr.svg :as svg]
|
||||
[auto-ap.ssr.utils
|
||||
@@ -31,24 +31,20 @@
|
||||
html-response
|
||||
many-entity
|
||||
money
|
||||
path->name2
|
||||
percentage
|
||||
ref->enum-schema
|
||||
ref->radio-options
|
||||
regex
|
||||
temp-id
|
||||
wrap-entity
|
||||
wrap-form-4xx-2
|
||||
wrap-schema-decode]]
|
||||
[auto-ap.utils :refer [dollars=]]
|
||||
[bidi.bidi :as bidi]
|
||||
[cheshire.core :as cheshire]
|
||||
[clojure.string :as str]
|
||||
[datomic.api :as dc]
|
||||
[hiccup2.core :as hiccup]
|
||||
[iol-ion.query :refer [ident]]
|
||||
[malli.core :as mc]
|
||||
[auto-ap.cursor :as cursor]
|
||||
[auto-ap.ssr.hiccup-helper :as hh]))
|
||||
[malli.core :as mc]))
|
||||
|
||||
;; TODO with dependencies, I really don't like that you have to be ultra specific in what
|
||||
;; you want to include, and generating the routes and interconnection is weird too.
|
||||
@@ -401,25 +397,29 @@
|
||||
(com/a-icon-button {"@click.prevent.stop" "show=false; setTimeout(() => $refs.p.remove(), 500)"} svg/x))))
|
||||
|
||||
;; TODO dialog is no longer closeable
|
||||
(defn dialog* [& {:keys [entity form-params form-errors]}]
|
||||
(fc/start-form entity form-errors
|
||||
;; TODO might not need #edit-form id
|
||||
(defn dialog* [{:keys [entity form-params form-errors]}]
|
||||
(fc/start-form form-params form-errors
|
||||
(com/modal
|
||||
{:modal-class "max-w-2xl"}
|
||||
{:modal-class "max-w-2xl"
|
||||
:hx-target "this"}
|
||||
|
||||
[:form#edit-form (merge {:hx-ext "response-targets"
|
||||
:hx-swap "outerHTML swap:300ms"
|
||||
:hx-target "#modal-holder" ;; TODO sort
|
||||
:hx-target-400 "#form-errors .error-content"
|
||||
:x-trap "true"
|
||||
:class "group/form"}
|
||||
form-params)
|
||||
[:form#edit-form {:hx-ext "response-targets"
|
||||
:hx-swap "outerHTML swap:300ms"
|
||||
:hx-target-400 "#form-errors .error-content"
|
||||
:x-trap "true"
|
||||
:class "group/form"
|
||||
(if (:db/id entity)
|
||||
:hx-put
|
||||
:hx-post) (str (bidi/path-for ssr-routes/only-routes :admin-transaction-rule-edit-save))}
|
||||
(com/modal-card
|
||||
{}
|
||||
[:div.flex [:div.p-2 "Transaction Rule"]]
|
||||
[:fieldset {:class "hx-disable"
|
||||
:hx-disinherit "hx-target" ;; TODO why disinherit
|
||||
:x-data (hx/json {:clientId (or (:db/id (:transaction-rule/client entity))
|
||||
(:transaction-rule/client entity))})}
|
||||
:x-data (hx/json {:clientId (or (:db/id (:transaction-rule/client form-params))
|
||||
(:transaction-rule/client form-params)
|
||||
(:db/id (:transaction-rule/client entity)))})}
|
||||
|
||||
[:div.space-y-1
|
||||
(when-let [id (:db/id entity)]
|
||||
@@ -486,7 +486,7 @@
|
||||
:hx-vals (format "js:{name: '%s', 'client-id': event.detail.clientId}" (fc/field-name))
|
||||
:x-init "$watch('clientId', cid => $dispatch('changed', $data))"}]
|
||||
|
||||
(bank-account-typeahead* {:client-id ((some-fn :db/id identity) (:transaction-rule/client entity))
|
||||
(bank-account-typeahead* {:client-id ((some-fn :db/id identity) (:transaction-rule/client form-params))
|
||||
:name (fc/field-name)
|
||||
:value (fc/field-value)})]))
|
||||
|
||||
@@ -543,18 +543,20 @@
|
||||
:content-fn (some-fn :vendor/name #(pull-attr (dc/db conn) :vendor/name %))})]))
|
||||
|
||||
(fc/with-field :transaction-rule/accounts
|
||||
(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/data-grid-header {:class "w-16"})]
|
||||
:id "transaction-rule-account-table"}
|
||||
(fc/cursor-map #(transaction-rule-account-row* entity %))
|
||||
(com/data-grid-new-row {:colspan 4
|
||||
:hx-get (bidi/path-for ssr-routes/only-routes
|
||||
:admin-transaction-rule-new-account)
|
||||
:index (count (fc/field-value))
|
||||
:tr-params (hx/bind-alpine-vals {} {:client-id "clientId"})}
|
||||
"New account")))
|
||||
(com/validated-field
|
||||
{:errors (fc/field-errors)}
|
||||
(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/data-grid-header {:class "w-16"})]
|
||||
:id "transaction-rule-account-table"}
|
||||
(fc/cursor-map #(transaction-rule-account-row* form-params %))
|
||||
(com/data-grid-new-row {:colspan 4
|
||||
:hx-get (bidi/path-for ssr-routes/only-routes
|
||||
:admin-transaction-rule-new-account)
|
||||
:index (count (fc/field-value))
|
||||
:tr-params (hx/bind-alpine-vals {} {:client-id "clientId"})}
|
||||
"New account"))))
|
||||
|
||||
(fc/with-field :transaction-rule/transaction-approval-status
|
||||
(com/validated-field {:label "Approval status"
|
||||
@@ -567,10 +569,8 @@
|
||||
|
||||
]]
|
||||
[:div
|
||||
[:div#form-errors (when (:errors fc/*form-errors*)
|
||||
[:span.error-content
|
||||
(com/errors {:errors (:errors fc/*form-errors*)})])]
|
||||
(com/validated-save-button {:errors form-errors} "Save rule")])])))
|
||||
(com/form-errors {:errors (:errors fc/*form-errors*)})
|
||||
(com/validated-save-button {:errors (:errors form-errors)} "Save rule")])])))
|
||||
|
||||
|
||||
;; TODO Should forms have some kind of helper to render an individual field? saving
|
||||
@@ -619,34 +619,14 @@
|
||||
:value account
|
||||
:client-id client-id}))))
|
||||
|
||||
(defn transaction-rule-edit-dialog [request]
|
||||
(let [entity (or
|
||||
(some-> request :last-form)
|
||||
(some-> request :route-params :db/id (#(dc/pull (dc/db conn) default-read %))))]
|
||||
(html-response (dialog* :entity entity
|
||||
:form-params {:hx-put (str (bidi/path-for ssr-routes/only-routes
|
||||
:admin-transaction-rule-edit-save))})
|
||||
:headers {"hx-trigger-after-settle" "modalopen"})))
|
||||
|
||||
(defn transaction-rule-error [request]
|
||||
(let [entity (some-> request :last-form)]
|
||||
(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
|
||||
:admin-transaction-rule-edit-save))}
|
||||
{:hx-post (str (bidi/path-for ssr-routes/only-routes
|
||||
:admin-transaction-rule-edit-save))}))
|
||||
:headers {"hx-retarget" "#edit-form fieldset"
|
||||
"hx-reselect" "#edit-form fieldset"})))
|
||||
|
||||
|
||||
(defn transaction-rule-new-dialog [_]
|
||||
(html-response (dialog* :entity {}
|
||||
:form-errors {}
|
||||
:form-params {:hx-post (str (bidi/path-for ssr-routes/only-routes
|
||||
:admin-transaction-rule-edit-save))})
|
||||
:headers {"hx-trigger-after-settle" "modalopen"}))
|
||||
(defn transaction-dialog [{:keys [entity form-params form-errors]}]
|
||||
(html-response (dialog* {:entity entity
|
||||
:form-params (or (when (seq form-params)
|
||||
form-params)
|
||||
entity ;; TODO coerce into form params
|
||||
{})
|
||||
:form-errors form-errors})
|
||||
:headers {"hx-trigger" "modalopen"}))
|
||||
|
||||
(def transaction-rule-schema (mc/schema
|
||||
[:map
|
||||
@@ -695,12 +675,15 @@
|
||||
[:value {:optional true}
|
||||
[:maybe entity-id]]]))
|
||||
:admin-transaction-rule-save (-> transaction-rule-save
|
||||
(wrap-entity [:form-params :db/id] default-read)
|
||||
(wrap-schema-decode :form-schema transaction-rule-schema)
|
||||
(wrap-nested-form-params)
|
||||
(wrap-form-4xx-2 transaction-rule-error))
|
||||
:admin-transaction-rule-edit-dialog (-> transaction-rule-edit-dialog
|
||||
(wrap-form-4xx-2 (-> transaction-dialog
|
||||
(wrap-entity [:form-params :db/id] default-read))))
|
||||
:admin-transaction-rule-edit-dialog (-> transaction-dialog
|
||||
(wrap-entity [:route-params :db/id] default-read)
|
||||
(wrap-schema-decode :route-schema [:map [:db/id entity-id]]))
|
||||
:admin-transaction-rule-new-dialog transaction-rule-new-dialog})
|
||||
:admin-transaction-rule-new-dialog transaction-dialog})
|
||||
(fn [h]
|
||||
(-> h
|
||||
(wrap-admin)
|
||||
|
||||
Reference in New Issue
Block a user