further cleans up POS

This commit is contained in:
2023-09-28 20:41:00 -07:00
parent c73c7619d8
commit d1798f6b5c
13 changed files with 684 additions and 381 deletions

View File

@@ -1,23 +1,24 @@
(ns auto-ap.ssr.grid-page-helper
(:require
[auto-ap.ssr-routes :as ssr-routes]
[auto-ap.ssr.components :as com]
[auto-ap.ssr.svg :as svg]
[auto-ap.ssr.ui :refer [base-page]]
[auto-ap.ssr.utils :refer [html-response]]
[hiccup2.core :as hiccup]
[bidi.bidi :as bidi]
[auto-ap.ssr-routes :as ssr-routes]
[cemerick.url :as url]
[clojure.string :as str]
[auto-ap.ssr.svg :as svg]
[hiccup2.core :as hiccup]
[clj-time.core :as time]
[auto-ap.time :as atime]
[clj-time.core :as time]))
[auto-ap.query-params :as query-params]))
(defn row* [gridspec user entity {:keys [flash? delete-after-settle? params] :as options}]
(defn row* [gridspec user entity {:keys [flash? delete-after-settle? request] :as options}]
(let [cells (->> gridspec
:headers
(filter (fn [h]
(if (and (:hide? h)
((:hide? h) params))
((:hide? h) request))
nil
h)))
(mapv (fn [header]
@@ -28,7 +29,7 @@
cells (conj cells (com/data-grid-right-stack-cell {}
(into [:form
[:input {:type :hidden :name "id" :value ((:id-fn gridspec) entity)}]]
((:row-buttons gridspec) user entity))))]
((:row-buttons gridspec) request entity))))] ;; TODO double check usage of row buttons user and identity in callers
(apply com/data-grid-row
{:class (when flash?
"live-added")
@@ -66,12 +67,11 @@
))
"default sort"))
(defn table* [grid-spec user {{:keys [start per-page clients flash-id sort]} :parsed-query-params :as params}]
(defn table* [grid-spec user {{:keys [start per-page flash-id sort]} :parsed-query-params :as request}]
(let [start (or start 0)
per-page (or per-page 30)
[entities total] ((:fetch-page grid-spec)
user
params)]
request)]
(com/data-grid-card {:id (:id grid-spec)
:title (:title grid-spec)
@@ -82,9 +82,9 @@
:subtitle [:div.flex.items-center.gap-2
[:span (format "Total %s: %d, " (:entity-name grid-spec) total)]
(sort-by-list grid-spec sort)]
:action-buttons ((:action-buttons grid-spec) user params)
:action-buttons ((:action-buttons grid-spec) request)
:rows (for [entity entities]
(row* grid-spec user entity {:flash? (= flash-id ((:id-fn grid-spec) entity)) :params params}))
(row* grid-spec user entity {:flash? (= flash-id ((:id-fn grid-spec) entity)) :request request}))
:thead-params {:hx-get (bidi/path-for ssr-routes/only-routes
(:route grid-spec))
:hx-target (str "#" (:id grid-spec))
@@ -99,7 +99,7 @@
(fn [h]
(cond
(and (:hide? h)
((:hide? h) params))
((:hide? h) request))
nil
(:sort-key h)
@@ -126,43 +126,9 @@
(defn parse-sort [grid-spec q]
(if (not-empty q)
(->>
(str/split q #",")
(map (fn [k]
(let [[key asc?] (str/split k #":")
matching-header (first (filter #(= (str key) (:sort-key %)) (:headers grid-spec)))]
{:sort-key (str key)
:asc (boolean (= "asc" asc?))
:matching-header matching-header
:name (:name matching-header)
:sort-icon (if (= (boolean (= "asc" asc?)) true)
svg/sort-down
svg/sort-up)})))
(filter :matching-header)
(into []))
[]))
(defn toggle-sort [grid-spec q k]
(if ((set (map :sort-key q)) k)
(mapv
(fn [s]
(if (= (:sort-key s)
k)
(-> s
(update :asc
#(boolean (not %)))
(update :sort-icon (fn [x]
(if (= x svg/sort-down)
svg/sort-up
svg/sort-down))))
s))
q)
(conj q {:sort-key k
:asc true
:name (:name (first (filter #(= (str k) (:sort-key %)) (:headers grid-spec))))
:sort-icon svg/sort-down})))
(defn sort->query [s]
(str/join "," (map (fn [k] (format "%s:%s" (:sort-key k) (if (= true (:asc k))
@@ -176,77 +142,72 @@
(update :sort sort->query)
(url/map->query)))
(defn use-date-range [query-params date-range]
(condp = date-range
"week"
(assoc query-params
:start-date (atime/unparse-local (time/plus (time/now) (time/days -7)) atime/normal-date)
:end-date (atime/unparse-local (time/now) atime/normal-date))
(defn default-unparse-query-params [query-params]
(reduce
(fn [query-params [k value]]
(assoc query-params k
(cond (= k :sort)
(sort->query value)
"month"
(assoc query-params
:start-date (atime/unparse-local (time/plus (time/now) (time/months -1)) atime/normal-date)
:end-date (atime/unparse-local (time/now) atime/normal-date))
(instance? org.joda.time.base.AbstractInstant value)
(atime/unparse-local value atime/normal-date)
"year"
(assoc query-params
:start-date (atime/unparse-local (time/plus (time/now) (time/years -1)) atime/normal-date)
:end-date (atime/unparse-local (time/now) atime/normal-date))
(instance? Long value)
(str value)
"all"
(assoc query-params
:start-date (atime/unparse-local (time/plus (time/now) (time/years -3)) atime/normal-date)
:end-date (atime/unparse-local (time/now) atime/normal-date))
(instance? Double value)
(format "%.2f" value)
(instance? Float value)
(format "%.2f" value)
(keyword? value)
(name value)
:else
value)))
query-params
query-params))
(defn extract-params [grid-spec {:keys [query-params hx-query-params identity session] :as request}]
(let [{hx-start "start" hx-per-page "per-page" hx-sort "sort" } hx-query-params
{q-start "start" q-per-page "per-page" q-sort "sort" q-toggle-sort "toggle-sort" q-remove-sort "remove-sort" date-range "date-range"} query-params
raw-query-params (merge (or hx-query-params {}) query-params)
parsed-query-params (cond-> (into {} (map (fn [[k v]] [(keyword k) v]) raw-query-params))
hx-start (assoc :start (some-> hx-start not-empty (Long/parseLong )))
q-start (assoc :start (some-> q-start not-empty (Long/parseLong )))
hx-per-page (assoc :per-page (some-> hx-per-page not-empty (Long/parseLong )))
q-per-page (assoc :per-page (some-> q-per-page not-empty (Long/parseLong )))
hx-sort (assoc :sort (parse-sort grid-spec hx-sort))
q-sort (assoc :sort (parse-sort grid-spec q-sort))
(not-empty q-toggle-sort) (update :sort #(toggle-sort grid-spec % q-toggle-sort) )
(not-empty q-remove-sort) (update :sort (fn [s] (filter (comp (complement #{q-remove-sort}) :sort-key) s)) )
date-range (use-date-range date-range)
true (dissoc :toggle-sort :remove-sort :date-range))]
{:raw-query-params raw-query-params
:parsed-query-params parsed-query-params
:client-selection (:client-selection (:session request))
:clients (:clients request)
:client (:client request)
:request request}))
(defn table [grid-spec {:keys [identity] :as request}]
(let [params (extract-params grid-spec request)]
(let [unparse-query-params (or (:unparse-query grid-spec)
default-unparse-query-params)]
(html-response (table*
grid-spec
identity
params)
:headers {"hx-push-url" (str "?" (params->query-string params))}
request)
:headers {"hx-push-url" (str "?" (url/map->query (unparse-query-params (:parsed-query-params request))))}
:oob (when-let [oob-render (:oob-render grid-spec)]
(oob-render identity params)))))
(oob-render request)))))
(defn page [grid-spec {:keys [identity] :as request}]
(let [params (extract-params grid-spec request)]
(base-page
request
(com/page {:nav (:nav grid-spec)
:page-specific (when-let [page-specific-nav (:page-specific-nav grid-spec)]
[:div#page-specific-nav (page-specific-nav params)])
:client-selection (:client-selection (:session request))
:clients (:clients request)
:client (:client request)
:identity (:identity request)}
(apply com/breadcrumbs {} (:breadcrumbs grid-spec))
(table* grid-spec
identity
params
))
(:title grid-spec))))
(base-page
request
(com/page {:nav (:nav grid-spec)
:page-specific (when-let [page-specific-nav (:page-specific-nav grid-spec)]
[:div#page-specific-nav (page-specific-nav request)])
:client-selection (:client-selection (:session request))
:clients (:clients request)
:client (:client request)
:identity (:identity request)}
(apply com/breadcrumbs {} (:breadcrumbs grid-spec))
(table* grid-spec
identity
request))
(:title grid-spec)))
(defn default-parse-query-params [grid-spec]
(comp
(query-params/apply-remove-sort)
(query-params/apply-toggle-sort grid-spec)
(query-params/apply-date-range :date-range :start-date :end-date)
(query-params/parse-key :exact-match-id query-params/parse-long)
(query-params/parse-key :sort #(query-params/parse-sort grid-spec %))
(query-params/parse-key :per-page query-params/parse-long)
(query-params/parse-key :start query-params/parse-long)
(query-params/parse-key :start-date query-params/parse-date)
(query-params/parse-key :end-date query-params/parse-date)))