can merge again.

This commit is contained in:
2024-01-09 23:23:51 -08:00
parent 810c3aacb2
commit e33cc24003
2 changed files with 133 additions and 13 deletions

View File

@@ -17,6 +17,7 @@
[auto-ap.routes.admin.vendors :as route]
[auto-ap.routes.utils
:refer [wrap-admin wrap-client-redirect-unauthenticated]]
[auto-ap.solr :as solr]
[auto-ap.ssr-routes :as ssr-routes]
[auto-ap.ssr.components :as com]
[auto-ap.ssr.components.timeline :as timeline]
@@ -28,28 +29,25 @@
[auto-ap.ssr.utils
:refer [apply-middleware-to-all-handlers
entity-id
form-validation-error
html-response
main-transformer
many-entity
modal-response
ref->enum-schema
wrap-schema-decode
ref->select-options
schema-enforce-request
strip
temp-id
wrap-entity
wrap-form-4xx-2
wrap-schema-decode
wrap-schema-enforce]]
[bidi.bidi :as bidi]
[clojure.string :as str]
[datomic.api :as dc]
[hiccup.util :as hu]
[malli.core :as mc]
[malli.util :as mut]
[slingshot.slingshot :refer [try+]]
[malli.error :as me]
[auto-ap.solr :as solr]))
[malli.util :as mut]))
(defn filters [request]
[:form {"hx-trigger" "change delay:500ms, keyup changed from:.hot-filter delay:1000ms"
@@ -162,10 +160,16 @@
(query-params/parse-key :code query-params/parse-long)
(helper/default-parse-query-params grid-page))
:action-buttons (fn [_]
[(com/button {:hx-get (str (bidi/path-for ssr-routes/only-routes
[
(com/button {:hx-get (str (bidi/path-for ssr-routes/only-routes
::route/merge))
:color :secondary}
"Merge")
(com/button {:hx-get (str (bidi/path-for ssr-routes/only-routes
::route/new))
:color :primary}
"New Vendor")])
"New Vendor")
])
:row-buttons (fn [_ entity]
[(com/icon-button {:hx-get (str (bidi/path-for ssr-routes/only-routes
::route/edit
@@ -232,6 +236,29 @@
"hx-reswap" "afterbegin")
(= :put request-method) (assoc "hx-retarget" (format "#entity-table tr[data-id=\"%d\"]" (:db/id updated-vendor)))))))
(defn merge-submit [{:keys [form-params request-method identity] :as request}]
(if (= (:source-vendor form-params)
(:target-vendor form-params))
(form-validation-error "Please select two different vendors"
:form form-params))
(let [transaction (->> (dc/q {:find '[?x ?a2]
:in '[$ ?vendor-from ]
:where ['[?x ?a ?vendor-from]
'[?a :db/ident ?a2]]}
(dc/db conn)
(:source-vendor form-params))
(mapcat (fn [[src attr]]
[[:db/retract src attr (:source-vendor form-params)]
[:db/add src attr (:target-vendor form-params)]])))]
(alog/peek transaction)
(audit-transact-batch transaction identity)
(audit-transact [[:db/retractEntity (:source-vendor form-params)]] identity))
(html-response
[:div]
:headers {"hx-trigger" (hx/json {"modalclose" ""
"notification" "Vendor merge successful."})}))
(defn back-button []
[:a {"@click" "$dispatch('modalprevious')"
"class" "text-sm font-medium text-gray-700 cursor-pointer"}
@@ -258,7 +285,6 @@
n)))))
(defn info-modal [{:keys [form-params form-errors entity]}]
(println "FORM ERRORS" form-errors)
(com/stacked-modal-card-2
0
{"@keydown.enter.prevent.stop" "$refs.next.click()"
@@ -312,7 +338,6 @@
[:div.flex.justify-end
(com/form-errors {:errors (:errors fc/*form-errors*)})
[:div.flex.items-baseline.gap-x-4
(println "HERE" form-errors)
(com/validated-save-button {:errors (seq form-errors)
:x-ref "next"
:class "w-48"
@@ -436,7 +461,6 @@
:hx-get (bidi/path-for ssr-routes/only-routes ::route/new-automatic-payment)
:index (count (fc/field-value))}
"New automatic payment for client"))))])
(println "errors" (seq form-errors))
(com/modal-footer
{}
[:div.flex.justify-end
@@ -782,6 +806,11 @@
[:vendor/legal-entity-tin-type {:optional true} [:maybe (ref->enum-schema "legal-entity-tin-type")]]
[:vendor/legal-entity-1099-type {:optional true} [:maybe (ref->enum-schema "legal-entity-1099-type")]]]))
(def merge-form-schema (mc/schema
[:map
[:source-vendor {:optional false} entity-id]
[:target-vendor {:optional false} entity-id]]))
(defn dialog [{:keys [entity form-params form-errors]}]
(modal-response (dialog* {:entity entity
:form-params (or (when (seq form-params)
@@ -791,6 +820,93 @@
(mc/decode form-schema {} main-transformer))
:form-errors form-errors})))
(defn merge-dialog [{:keys [entity form-params form-errors]}]
(modal-response
(fc/start-form form-params form-errors
[:div {:class "w-full h-full"}
[:form#my-form (-> {:hx-swap "outerHTML"
:hx-trigger "submit"
:class "h-full w-full"
:hx-put (str (bidi/path-for ssr-routes/only-routes ::route/merge-submit))})
(com/modal
{}
(com/modal-card
{}
(com/modal-header {}
[:div.m-2 "Merge Vendors"])
(com/modal-body {}
[:div.space-y-6
(fc/with-field :source-vendor
(com/validated-field {:label "Source vendor (to be deleted)"
:errors (fc/field-errors)}
(com/typeahead {:name (fc/field-name)
:placeholder "Search..."
:url (bidi/path-for ssr-routes/only-routes
:vendor-search)
:id (str "vendor-search")
:value (fc/field-value)
:content-fn (fn [c] (pull-attr (dc/db conn) :vendor/name c))})))
(fc/with-field :target-vendor
(com/validated-field {:label "Target vendor"
:errors (fc/field-errors)}
(com/typeahead {:name (fc/field-name)
:placeholder "Search..."
:url (bidi/path-for ssr-routes/only-routes
:vendor-search)
:id (str "vendor-search")
:value (fc/field-value)
:content-fn (fn [c] (pull-attr (dc/db conn) :vendor/name c))})))
]
#_[:div.space-y-1.mt-4 {:class "w-[600px] h-[350px]"}
(when-let [id (:db/id entity)]
(com/hidden {:name "db/id"
:value id}))
(fc/with-field :vendor/name
(com/validated-field {:label "Name"
:errors (fc/field-errors)
}
(com/text-input {:name (fc/field-name)
:value (fc/field-value)
:x-model "vendorName"
:autofocus true
:class "w-96"})))
(com/validated-field
{}
[:div (com/checkbox
{:x-model "showPrintAs" "@change" "if (!showPrintAs) { printAs = ''; } "}
"Use different name for checks")])
(fc/with-field :vendor/print-as
(com/validated-field (-> {:label "Print as"
:errors (fc/field-errors)
:x-show "showPrintAs"}
hx/alpine-appear
hx/alpine-disappear)
(com/text-input {:name (fc/field-name)
:x-model "printAs"
:value (fc/field-value)
:class "w-96"})))
(fc/with-field :vendor/hidden
(alog/peek (cursor/path fc/*current*))
(com/checkbox {:name (fc/field-name)
:value (boolean (fc/field-value)) ;
:checked (alog/peek :checked (fc/field-value))}
"Admin-only"))])
(com/modal-footer
{}
[:div.flex.justify-end
(com/form-errors {:errors (:errors fc/*form-errors*)})
[:div.flex.items-baseline.gap-x-4
(com/validated-save-button {:errors (seq form-errors)
:class "w-48"}
"Merge")]])))]])))
(defn new-terms-override [{{:keys [index]} :query-params}]
@@ -826,14 +942,12 @@
"hx-reswap" "none"}))
(defn single-modal [which {:keys [form-params form-errors] :as request}]
(println "ERRORS ARE" form-errors)
(fc/start-form form-params form-errors
(html-response (which request))))
(defn branch-handler [f & {:as matches}]
(fn branch-handler* [request]
(let [c (f request)]
(println matches)
(some->> matches
(filter (fn [[matching-key matching-handler]]
(= c matching-key)))
@@ -854,6 +968,10 @@
{::route/page (helper/page-route grid-page)
::route/table (helper/table-route grid-page)
::route/new dialog
::route/merge merge-dialog
::route/merge-submit (-> merge-submit
(wrap-schema-enforce :form-schema merge-form-schema)
(wrap-form-4xx-2 merge-dialog))
::route/validate (-> (branch-handler (comp :step :query-params)
"Info" (-> modal-next
(wrap-schema-enforce :form-schema (mut/select-keys form-schema #{:vendor/name :vendor/print-as}))

View File

@@ -10,5 +10,7 @@
"/account-typeahead" ::account-typeahead
"/validate" ::validate
"/new" {:get ::new}
"/merge" {:get ::merge
:put ::merge-submit}
["/" [#"\d+" :db/id] "/edit"] {:get ::edit
}})