Makes tailwind version of history page

This commit is contained in:
Bryce
2023-06-01 09:26:52 -07:00
parent fad492e26d
commit 3540346fc8
14 changed files with 253 additions and 246 deletions

View File

@@ -2,14 +2,15 @@
(:require
[auto-ap.datomic :refer [conn]]
[auto-ap.logging :as alog]
[auto-ap.shared-views.admin.side-bar :refer [admin-side-bar]]
[auto-ap.ssr.ui :refer [base-page]]
[auto-ap.ssr.utils :refer [html-response]]
[auto-ap.time :as atime]
[clj-time.coerce :as coerce]
[clojure.string :as str]
[datomic.api :as dc]
[hiccup2.core :as hiccup]))
[auto-ap.ssr.components :as com]
[auto-ap.ssr-routes :as ssr-routes]
[bidi.bidi :as bidi]))
(defn tx-rows->changes [history]
(->> history
@@ -25,9 +26,6 @@
changes))]
[t a changes])))))
(def error-script
(hiccup/raw "on htmx:responseError from me set event.detail.target's innerHTML to event.detail.xhr.responseText end"))
(defn format-value [v]
(cond (inst? v)
(-> v
@@ -40,126 +38,27 @@
(and (integer? v)
(> v 1000000))
[:span
[:a {:hx-get (str "/admin/history/" v)
:hx-swap "innerHTML"
:hx-push-url "true"
:hx-target "#history-table"}
v]
" [" [:a
{:hx-get (str "/admin/history/inspect/" v)
:hx-swap "innerHTML"
:hx-target "#inspector"
:hx-trigger "click"
"_" error-script}
"snapshot"] "]"]
[:span
(com/link
{:href "#"
:hx-get (str "/admin/history/" v)
:hx-swap "innerHTML"
:hx-push-url "true"
:hx-select "#history-table"
:hx-target "#history-table"}
v)
" ["
(com/link
{:href "#"
:hx-get (str "/admin/history/inspect/" v)
:hx-swap "innerHTML"
:hx-target "#inspector"
:hx-trigger "click"}
"snapshot") "]"]
:else
(pr-str v)))
(defn page-template [& {:keys [table entity-id]}]
[:div
[:div.columns
[:div.column.is-4
[:form {"hx-target" "#history-table"
"hx-post" "/admin/history/search"
"hx-swap" "innerHTML"
"_" (hiccup/raw "on htmx:beforeRequest toggle @disabled on me then toggle .is-loading on <#dig/> end
on htmx:afterRequest toggle @disabled on me then toggle .is-loading on <#dig /> end")
}
[:div.field.is-grouped
[:p.control {}
[:input.input {:type "text" :name "entity-id" :placeholder "Entity id" :value entity-id}]]
[:p.control
[:button#dig.button.is-primary {}
"Dig"]]]]]]
[:div#history-table
table]])
(defn table [entity-id best-guess-entity history]
[:div [:h1.title "History for "
(str/capitalize best-guess-entity)
" "
entity-id]
[:div.columns
[:div.column.is-9
[:table.table.compact.grid {:style "width: 100%"}
[:thead
[:tr
[:td {:style "width: 14em"} "Date"]
[:td {:style "width: 14em"} "User"]
[:td {:style "width: 18em"} "Field"]
[:td "From"]
[:td "To"]]]
[:tbody
(for [[tx a c] history]
[:tr
[:td [:div [:div (some-> (:db/txInstant tx)
coerce/to-date-time
atime/localize
(atime/unparse atime/standard-time))
]
[:div.tag (:db/id tx)]]]
[:td (str (:audit/user tx))]
[:td (namespace a) ": " (name a)]
[:td
[:div.tag.is-danger.is-light
[:span
(format-value (:removed c))]]]
[:td
[:div.tag.is-primary.is-light
[:span
(format-value (:added c))]]]])]
]]
[:div.column.is-3
[:div#inspector]]]])
(defn history-search [{:keys [form-params params] :as request}]
(try
(let [entity-id (Long/parseLong (or (some-> (:entity-id form-params) not-empty)
(:entity-id params)
(get params "entity-id")
(get form-params "entity-id")))
history (->>
(dc/q '[:find ?a2 ?v (pull ?tx [:db/txInstant :audit/user :db/id]) ?ad
:in $ $$ ?i
:where
[$$ ?i ?a ?v ?tx ?ad]
[$ ?a :db/ident ?a2]]
(dc/db conn)
(dc/history (dc/db conn))
entity-id )
tx-rows->changes
(sort-by (comp :db/id first))
vec)
best-guess-entity (or (->> history
(group-by
(comp
namespace
second)
)
(map (fn [[k v]]
[k v]))
(sort-by second)
last
first)
"?")]
(if (get (:headers request) "hx-request")
(html-response
(table entity-id best-guess-entity history))
(base-page request (page-template :table (table entity-id best-guess-entity history)
:entity-id entity-id)
(admin-side-bar :admin-history))))
(catch NumberFormatException _
(html-response
[:div.notification.is-danger.is-light
"Cannot parse the entity-id " (or (:entity-id form-params)
(:entity-id params))
". It should be a number."]))))
(defn inspect [{{:keys [entity-id]} :params :as request}]
(alog/info ::inspect
@@ -167,34 +66,126 @@
(try
(let [entity-id (Long/parseLong entity-id)
data (dc/pull (dc/db conn)
'[*]
entity-id)]
'[*]
entity-id)]
(html-response
[:div.box {:style {:position "sticky"
:display "inline-block"
:vertical-align "top"
:overflow-y "auto"
:max-height "100vh"
:top "0px"
:bottom "0px"}}
[:div {:style {:display "inline-block"}}
[:h1.title "Snapshot of "
entity-id]
[:ul
(for [[k v] data]
[:li [:strong k] ":" (format-value v)]
)]]]))
(html-response
[:section.py-3.sm:py-5.max-w-lg
(com/card {:class "p-2"}
[:div {:style {:display "inline-block"}}
[:h1.title "Snapshot of "
entity-id]
[:ul
(for [[k v] data]
[:li [:strong k] ":" (format-value v)])]])]))
(catch NumberFormatException _
(html-response
[:div.notification.is-danger.is-light
"Cannot parse the entity-id " entity-id ". It should be a number."]))))
(defn history [{:keys [matched-route] :as request}]
(base-page request
(page-template )
(admin-side-bar matched-route)))
(html-response
[:div.notification.is-danger.is-light
"Cannot parse the entity-id " entity-id ". It should be a number."]))))
(defn result-table [{:keys [entity-id]}]
(try
(let [history (->>
(dc/q '[:find ?a2 ?v (pull ?tx [:db/txInstant :audit/user :db/id]) ?ad
:in $ $$ ?i
:where
[$$ ?i ?a ?v ?tx ?ad]
[$ ?a :db/ident ?a2]]
(dc/db conn)
(dc/history (dc/db conn))
entity-id)
tx-rows->changes
(sort-by (comp :db/id first))
vec)
best-guess-entity (or (->> history
(group-by
(comp
namespace
second))
(map (fn [[k v]]
[k v]))
(sort-by second)
last
first)
"?")]
(com/data-grid-card {:id "history-table"
:title (format "History for %s: %d" (str/capitalize best-guess-entity) entity-id)
:route :history-table
:start 0
:per-page (count history)
:total (count history)
:subtitle nil
:action-buttons nil
:rows (for [[tx a c] history]
(com/data-grid-row
{}
(com/data-grid-cell {} [:div [:div (some-> (:db/txInstant tx)
coerce/to-date-time
atime/localize
(atime/unparse atime/standard-time))]
[:div.tag (:db/id tx)]])
(com/data-grid-cell {} (str (:audit/user tx)))
(com/data-grid-cell {} (namespace a) ": " (name a))
(com/data-grid-cell {}
(com/pill {:color :red}
(format-value (:removed c))))
(com/data-grid-cell {}
[:div.tag.is-primary.is-light
[:span
(format-value (:added c))]])))
:headers
[(com/data-grid-header {}
"Date")
(com/data-grid-header {}
"User")
(com/data-grid-header {}
"Field")
(com/data-grid-header {}
"From")
(com/data-grid-header {}
"To")]}))
(catch NumberFormatException e
(throw e))))
(defn search-box [{:keys [entity-id]}]
[:div.mt-4
[:form.flex.gap-2 {"hx-target" "#history-table"
"hx-get" (bidi/path-for ssr-routes/only-routes
:admin-history)
"hx-select" "#history-table"
"hx-swap" "innerHTML"
"hx-ext" "debug"
"hx-push-url" "true"}
(com/text-input {:name "entity-id" :placeholder "Entity Id" :value entity-id
:style {:width "300px"}})
(com/button {:color :primary}
"DIG")]])
(defn history [{:keys [matched-route route-params query-params] :as request}]
(let [entity-id (or (some-> query-params (get "entity-id") Long/parseLong)
(some-> route-params (get :entity-id) Long/parseLong))]
(base-page request
(com/page {:nav (com/company-aside-nav)
:active-client (:client (:session request))
:identity (:identity request)
:app-params {:hx-get (bidi/path-for ssr-routes/only-routes
:admin-history)
:hx-trigger "clientSelected from:body"
:hx-select "#app-contents"
:hx-swap "outerHTML swap:300ms"}}
(com/breadcrumbs {}
[:a {:href (bidi/path-for ssr-routes/only-routes
:admin)}
"Admin"]
[:a {:href (bidi/path-for ssr-routes/only-routes
:admin-history)}
"History"])
(search-box {:entity-id entity-id})
[:div.flex.gap-4.flex-col.lg:flex-row
(if entity-id
(result-table {:entity-id entity-id})
[:div#history-table])
[:div#inspector]
])
nil)))