migrates accounts to new dialog, adds client search to user
This commit is contained in:
@@ -16,24 +16,26 @@
|
||||
[auto-ap.ssr-routes :as ssr-routes]
|
||||
[auto-ap.ssr.components :as com]
|
||||
[auto-ap.ssr.grid-page-helper :as helper]
|
||||
[auto-ap.ssr.nested-form-params :refer [wrap-nested-form-params]]
|
||||
[auto-ap.ssr.svg :as svg]
|
||||
[auto-ap.ssr.utils
|
||||
:refer [entity-id
|
||||
:refer [apply-middleware-to-all-handlers
|
||||
entity-id
|
||||
forced-vector
|
||||
html-response
|
||||
map->db-id-decoder
|
||||
ref->enum-schema
|
||||
ref->select-options
|
||||
temp-id
|
||||
validation-error
|
||||
wrap-form-4xx
|
||||
wrap-schema-decode]]
|
||||
[bidi.bidi :as bidi]
|
||||
[clojure.string :as str]
|
||||
[datomic.api :as dc]
|
||||
[hiccup2.core :as hiccup]
|
||||
[malli.core :as mc]
|
||||
[ring.middleware.nested-params :refer [wrap-nested-params]]))
|
||||
[malli.core :as mc]))
|
||||
|
||||
;; TODO support for new account
|
||||
(defn filters [request]
|
||||
[:form {"hx-trigger" "change delay:500ms, keyup changed from:.hot-filter delay:1000ms"
|
||||
"hx-get" (bidi/path-for ssr-routes/only-routes
|
||||
@@ -80,7 +82,6 @@
|
||||
(:sort query-params) (add-sorter-fields {"name" ['[?e :account/name ?n]
|
||||
'[(clojure.string/upper-case ?n) ?sort-name]]
|
||||
"code" ['[(get-else $ ?e :account/numeric-code 0) ?sort-code]]
|
||||
|
||||
"type" ['[?e :account/type ?t]
|
||||
'[?t :db/ident ?ti]
|
||||
'[(name ?ti) ?sort-type]]}
|
||||
@@ -104,7 +105,6 @@
|
||||
(merge-query {:query {:find ['?sort-default '?e]
|
||||
:where ['[?e :account/code ?un]
|
||||
'[(clojure.string/upper-case ?un) ?sort-default]]}}))]
|
||||
|
||||
(cond->> (query2 query)
|
||||
true (apply-sort-3 query-params)
|
||||
true (apply-pagination query-params))))
|
||||
@@ -132,10 +132,17 @@
|
||||
:parse-query-params (comp
|
||||
(query-params/parse-key :code query-params/parse-long)
|
||||
(helper/default-parse-query-params grid-page))
|
||||
:action-buttons (fn [request]
|
||||
[(com/button {:hx-get (str (bidi/path-for ssr-routes/only-routes
|
||||
:admin-account-new-dialog))
|
||||
:hx-target "#modal-holder"
|
||||
:hx-swap "outerHTML"
|
||||
:color :primary}
|
||||
"New Account")])
|
||||
:row-buttons (fn [request entity]
|
||||
[(com/icon-button {:hx-get (str (bidi/path-for ssr-routes/only-routes
|
||||
:admin-account-edit-dialog
|
||||
:db/id (doto (:db/id entity) println)))
|
||||
:db/id (:db/id entity)))
|
||||
:hx-target "#modal-holder"
|
||||
:hx-swap "outerHTML"}
|
||||
svg/pencil)])
|
||||
@@ -153,7 +160,6 @@
|
||||
:name "Code"
|
||||
:sort-key "code"
|
||||
:render :account/numeric-code}
|
||||
|
||||
{:key "name"
|
||||
:name "Name"
|
||||
:sort-key "name"
|
||||
@@ -170,11 +176,18 @@
|
||||
(def row* (partial helper/row* grid-page))
|
||||
(def table* (partial helper/table* grid-page))
|
||||
|
||||
(defn account-edit-save [{:keys [params route-params] :as request}]
|
||||
(let [_ (audit-transact [[:upsert-entity (-> params (assoc :db/id (:db/id route-params)) (dissoc :id))]]
|
||||
(:identity request))
|
||||
updated-account (some-> route-params :db/id (#(dc/pull (dc/db conn) default-read %)))]
|
||||
|
||||
(defn account-save [{:keys [form-params request-method] :as request}]
|
||||
(let [entity (cond-> form-params
|
||||
(= :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)))))
|
||||
{:keys [tempids]} (audit-transact [[:upsert-entity (cond-> entity
|
||||
(:account/numeric-code entity) (assoc :account/code (str (:account/numeric-code entity))))]]
|
||||
(:identity request))
|
||||
updated-account (dc/pull (dc/db conn)
|
||||
default-read
|
||||
(or (get tempids (:db/id entity)) (:db/id entity)))]
|
||||
(solr/index-documents-raw solr/impl
|
||||
"accounts"
|
||||
(into [{"id" (:db/id updated-account)
|
||||
@@ -182,7 +195,7 @@
|
||||
"name" (:account/name updated-account)
|
||||
"numeric_code" (:account/numeric-code updated-account)
|
||||
"location" (:account/location updated-account)
|
||||
"applicability" (clojure.core/name (:account/applicability updated-account))}]
|
||||
"applicability" (some-> updated-account :account/applicability clojure.core/name)}]
|
||||
(for [o (:account/client-overrides updated-account)]
|
||||
{"id" (:db/id o)
|
||||
"account_id" (:db/id updated-account)
|
||||
@@ -191,14 +204,13 @@
|
||||
"location" (:account/location updated-account)
|
||||
"applicability" (clojure.core/name (:account/applicability updated-account))
|
||||
"client_id" (:db/id (:account-client-override/client o))
|
||||
"account_client_override_id" (:db/id o)}))
|
||||
)
|
||||
|
||||
(html-response
|
||||
"account_client_override_id" (:db/id o)})))
|
||||
(html-response
|
||||
(row* identity updated-account {:flash? true})
|
||||
:headers {"hx-trigger" "closeModal"
|
||||
:headers {"hx-trigger" "closeModal"
|
||||
"hx-retarget" (format "#account-table tr[data-id=\"%d\"]" (:db/id updated-account))})))
|
||||
|
||||
|
||||
(defn client-override* [override]
|
||||
[:div.flex.gap-2.mb-2.client-override
|
||||
[:div.w-96
|
||||
@@ -215,24 +227,28 @@
|
||||
:value (:account-client-override/name override)})]
|
||||
[: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)]])
|
||||
|
||||
(defn account-edit-dialog [request]
|
||||
(prn (:route-params request))
|
||||
(let [account (some-> request :route-params :db/id (#(dc/pull (dc/db conn) default-read %)))]
|
||||
(html-response
|
||||
(com/modal
|
||||
|
||||
(defn dialog* [& {:keys [ account form-params]}]
|
||||
(com/modal
|
||||
{:modal-class "max-w-4xl"}
|
||||
[:form#edit-form {:hx-ext "response-targets"
|
||||
:hx-post (str (bidi/path-for ssr-routes/only-routes
|
||||
:admin-account-edit-save
|
||||
:request-method :post
|
||||
:db/id (:db/id account )))
|
||||
:hx-swap "outerHTML swap:300ms"
|
||||
:hx-target-400 "#form-errors .error-content"}
|
||||
[: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)]]
|
||||
[:div.space-y-6
|
||||
(when-let [id (:db/id account)]
|
||||
(com/hidden {:name "db/id"
|
||||
:value id}))
|
||||
|
||||
(when (nil? account)
|
||||
(com/field {:label "Numeric code"}
|
||||
(com/text-input {:name "account/numeric-code"
|
||||
:autofocus true
|
||||
:class "w-32"})))
|
||||
(com/field {:label "Name"}
|
||||
(com/text-input {:name "account/name"
|
||||
:autofocus true
|
||||
@@ -251,18 +267,18 @@
|
||||
|
||||
(com/field {:label "Invoice Allowance"}
|
||||
(com/select {:name "account/invoice-allowance"
|
||||
:value (name (:account/invoice-allowance account))
|
||||
:value (some-> account :account/invoice-allowance name)
|
||||
:class "w-36"
|
||||
:options (ref->select-options "allowance")}))
|
||||
(com/field {:label "Vendor Allowance"}
|
||||
(com/select {:name "account/vendor-allowance"
|
||||
:class "w-36"
|
||||
:value (name (:account/vendor-allowance account))
|
||||
:value (some-> account :account/vendor-allowance name)
|
||||
:options (ref->select-options "allowance")}))
|
||||
(com/field {:label "Applicability"}
|
||||
(com/select {:name "account/applicability"
|
||||
:class "w-36"
|
||||
:value (name (:account/applicability account))
|
||||
:value (some-> account :account/applicability name)
|
||||
:options (ref->select-options "account-applicability")}))
|
||||
|
||||
(com/field {:label "Client Overrides" :id "client-overrides"}
|
||||
@@ -276,37 +292,56 @@
|
||||
[:div#form-errors [:span.error-content]]
|
||||
(com/button {:color :primary :form "edit-form" :type "submit"}
|
||||
"Save")]
|
||||
[:div])]]))))
|
||||
[:div])]]))
|
||||
|
||||
(defn new-client-override [request]
|
||||
(defn new-client-override [_]
|
||||
(html-response
|
||||
(client-override* {:db/id (str (java.util.UUID/randomUUID))})))
|
||||
|
||||
(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
|
||||
:form-params {:hx-put (str (bidi/path-for ssr-routes/only-routes
|
||||
:admin-account-edit-save))}))))
|
||||
|
||||
|
||||
(defn account-new-dialog [_]
|
||||
(html-response (dialog* :account nil
|
||||
:form-params {:hx-post (str (bidi/path-for ssr-routes/only-routes
|
||||
:admin-account-new-save))})))
|
||||
|
||||
(def account-schema (mc/schema
|
||||
[:map
|
||||
[:db/id {:optional true} [:maybe entity-id]]
|
||||
[:account/numeric-code {:optional true} [:maybe :int]]
|
||||
[:account/name [:string {:min 1}]]
|
||||
[:account/location [:maybe :string]]
|
||||
[:account/type (ref->enum-schema "account-type")]
|
||||
[:account/applicability (ref->enum-schema "account-applicability")]
|
||||
[:account/invoice-allowance (ref->enum-schema "allowance")]
|
||||
[:account/vendor-allowance (ref->enum-schema "allowance")]
|
||||
[:account/client-overrides {:decode/json map->db-id-decoder
|
||||
:optional true}
|
||||
[:maybe
|
||||
(forced-vector [:map
|
||||
[:db/id [:or entity-id temp-id]]
|
||||
[:account-client-override/client [:or entity-id :string]]
|
||||
[:account-client-override/name :string]])]]]))
|
||||
|
||||
(def key->handler
|
||||
{:admin-accounts (wrap-admin (helper/page-route grid-page))
|
||||
:admin-account-table (wrap-admin (helper/table-route grid-page))
|
||||
:admin-account-client-override-new (-> new-client-override wrap-admin wrap-client-redirect-unauthenticated)
|
||||
:admin-account-edit-save (-> account-edit-save
|
||||
wrap-admin
|
||||
wrap-client-redirect-unauthenticated
|
||||
(wrap-schema-decode
|
||||
:route-schema (mc/schema [:map [:db/id entity-id]])
|
||||
:params-schema (mc/schema
|
||||
[:map
|
||||
[:account/name :string]
|
||||
[:account/location [:maybe :string]]
|
||||
[:account/type (ref->enum-schema "account-type")]
|
||||
[:account/applicability (ref->enum-schema "account-applicability")]
|
||||
[:account/invoice-allowance (ref->enum-schema "allowance")]
|
||||
[:account/vendor-allowance (ref->enum-schema "allowance")]
|
||||
[:account/client-overrides {:decode/json map->db-id-decoder}
|
||||
(forced-vector [:map
|
||||
[:db/id [:or entity-id temp-id]]
|
||||
[:account-client-override/client [:or entity-id :string]]
|
||||
[:account-client-override/name :string]])]]))
|
||||
(wrap-nested-params))
|
||||
:admin-account-edit-dialog (-> account-edit-dialog
|
||||
wrap-admin
|
||||
wrap-client-redirect-unauthenticated
|
||||
(wrap-schema-decode
|
||||
:route-schema (mc/schema [:map [:db/id entity-id]])))})
|
||||
(apply-middleware-to-all-handlers
|
||||
(->>
|
||||
{:admin-accounts (helper/page-route grid-page)
|
||||
:admin-account-table (helper/table-route grid-page)
|
||||
:admin-account-client-override-new (-> new-client-override wrap-admin wrap-client-redirect-unauthenticated)
|
||||
:admin-account-save (-> account-save
|
||||
(wrap-schema-decode :form-schema account-schema)
|
||||
(wrap-nested-form-params)
|
||||
(wrap-form-4xx))
|
||||
:admin-account-edit-dialog (-> account-edit-dialog
|
||||
(wrap-schema-decode :route-schema [:map [:db/id entity-id]]))
|
||||
:admin-account-new-dialog account-new-dialog})
|
||||
(fn [h]
|
||||
(-> h
|
||||
(wrap-admin)
|
||||
(wrap-client-redirect-unauthenticated)))))
|
||||
|
||||
Reference in New Issue
Block a user