Revisions on grid page to make parameters easier to grok

This commit is contained in:
2023-09-25 16:26:42 -07:00
parent e23cc6e8fd
commit d5565f7cf8
9 changed files with 107 additions and 89 deletions

View File

@@ -59,7 +59,7 @@
))
"default sort"))
(defn table* [grid-spec user {:keys [start per-page clients flash-id sort] :as params}]
(defn table* [grid-spec user {{:keys [start per-page clients flash-id sort]} :parsed-query-params :as params}]
(let [start (or start 0)
per-page (or per-page 30)
[entities total] ((:fetch-page grid-spec)
@@ -119,16 +119,20 @@
(defn parse-sort [grid-spec q]
(if (not-empty q)
(into []
(map (fn [k]
(let [[k v] (str/split k #":")]
{:sort-key (str k)
:asc (boolean (= "asc" v))
:name (:name (first (filter #(= (str k) (:sort-key %)) (:headers grid-spec))))
:sort-icon (if (= (boolean (= "asc" v)) true)
svg/sort-down
svg/sort-up)}))
(str/split 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]
@@ -159,36 +163,38 @@
(defn params->query-string [q]
(-> q
(dissoc :client :session :client-selection :clients :query-params)
:parsed-query-params
(update :sort sort->query)
(url/map->query)))
(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"} query-params]
(cond-> {}
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) )
(seq query-params) (assoc :query-params query-params)
(:session request) (assoc :session (:session request))
(:client-selection (:session request)) (assoc :client-selection (:client-selection (:session request)))
(:clients request) (assoc :clients (:clients request))
(:client request) (assoc :client (:client request)))))
{q-start "start" q-per-page "per-page" q-sort "sort" q-toggle-sort "toggle-sort"} 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 (doto (parse-sort grid-spec hx-sort) println))
q-sort (assoc :sort (doto (parse-sort grid-spec q-sort) println ))
(not-empty q-toggle-sort) (update :sort #(toggle-sort grid-spec % q-toggle-sort) )
true (dissoc :toggle-sort))]
{: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 [query-params hx-query-params identity session] :as request}]
(let [params (extract-params grid-spec request)
query-string (params->query-string params)]
(defn table [grid-spec {:keys [identity] :as request}]
(let [params (extract-params grid-spec request)]
(html-response (table*
grid-spec
identity
params
)
:headers {"hx-push-url" (str "?" query-string)})))
params)
:headers {"hx-push-url" (str "?" (params->query-string params))})))
(defn page [grid-spec {:keys [identity] :as request}]
(let [params (extract-params grid-spec request)]