adds csv capability
This commit is contained in:
@@ -1,25 +1,27 @@
|
||||
(ns auto-ap.ssr.grid-page-helper
|
||||
(:require [auto-ap.graphql.utils :refer [extract-client-ids]]
|
||||
[auto-ap.logging :as alog]
|
||||
[auto-ap.query-params :as query-params]
|
||||
[auto-ap.routes.utils
|
||||
:refer [wrap-client-redirect-unauthenticated wrap-secure]]
|
||||
[auto-ap.ssr-routes :as ssr-routes]
|
||||
[auto-ap.ssr.components :as com]
|
||||
[auto-ap.ssr.hiccup-helper :as hh]
|
||||
[auto-ap.ssr.hx :as hx]
|
||||
[auto-ap.ssr.svg :as svg]
|
||||
[auto-ap.ssr.ui :refer [base-page]]
|
||||
[auto-ap.ssr.utils :refer [html-response main-transformer]]
|
||||
[auto-ap.time :as atime]
|
||||
[bidi.bidi :as bidi]
|
||||
[cemerick.url :as url]
|
||||
[clojure.string :as str]
|
||||
[hiccup2.core :as hiccup]
|
||||
[malli.core :as m]
|
||||
[malli.transform :as mt2]
|
||||
[malli.transform :as mt]
|
||||
[taoensso.encore :refer [filter-vals]]))
|
||||
(ns auto-ap.ssr.grid-page-helper
|
||||
(:require
|
||||
[auto-ap.graphql.utils :refer [extract-client-ids]]
|
||||
[auto-ap.logging :as alog]
|
||||
[auto-ap.query-params :as query-params]
|
||||
[auto-ap.routes.utils
|
||||
:refer [wrap-client-redirect-unauthenticated wrap-secure]]
|
||||
[auto-ap.ssr-routes :as ssr-routes]
|
||||
[auto-ap.ssr.components :as com]
|
||||
[auto-ap.ssr.hiccup-helper :as hh]
|
||||
[auto-ap.ssr.hx :as hx]
|
||||
[auto-ap.ssr.svg :as svg]
|
||||
[auto-ap.ssr.ui :refer [base-page]]
|
||||
[auto-ap.ssr.utils :refer [html-response main-transformer]]
|
||||
[auto-ap.time :as atime]
|
||||
[bidi.bidi :as bidi]
|
||||
[cemerick.url :as url]
|
||||
[clojure.string :as str]
|
||||
[hiccup.util :as hu]
|
||||
[malli.core :as m]
|
||||
[malli.transform :as mt2]
|
||||
[taoensso.encore :refer [filter-vals]]
|
||||
[clojure.java.io :as io]
|
||||
[clojure.data.csv :as csv]))
|
||||
|
||||
|
||||
|
||||
@@ -40,6 +42,8 @@
|
||||
[])
|
||||
cells (->> gridspec
|
||||
:headers
|
||||
(filter (fn [h]
|
||||
((:render-for h #{:html :csv}) :html)))
|
||||
(filter (fn [h]
|
||||
(if (and (:hide? h)
|
||||
((:hide? h) request))
|
||||
@@ -96,6 +100,12 @@
|
||||
(:check-boxes? grid-spec) inc)}
|
||||
[:span.font-bold.text-gray-600.text-lg break-table-value])))))))
|
||||
|
||||
(defn sort->query [s]
|
||||
(str/join "," (map (fn [k] (format "%s:%s" (:sort-key k) (if (= true (:asc k))
|
||||
"asc"
|
||||
"desc")))
|
||||
s)))
|
||||
|
||||
(defn table* [grid-spec user {{:keys [start per-page flash-id sort]} :parsed-query-params :as request}]
|
||||
(alog/info ::TABLE-QP
|
||||
:qp (:query-params request)
|
||||
@@ -109,9 +119,19 @@
|
||||
|
||||
(com/data-grid-card {:id (:id grid-spec)
|
||||
:raw? (:raw? grid-spec)
|
||||
:title (if (string? (:title grid-spec))
|
||||
(:title grid-spec)
|
||||
((:title grid-spec) request))
|
||||
:title [:div.flex.gap-2 (if (string? (:title grid-spec))
|
||||
(:title grid-spec)
|
||||
((:title grid-spec) request))
|
||||
(when (:csv-route grid-spec)
|
||||
(com/a-button {:href (hu/url (bidi/path-for ssr-routes/only-routes (:csv-route grid-spec))
|
||||
(dissoc (update (filter-vals #(not (nil? %))
|
||||
(m/encode (:query-schema grid-spec)
|
||||
(:query-params request)
|
||||
main-transformer))
|
||||
"sort" sort->query)
|
||||
"selected" "all-selected"))
|
||||
:color :secondary-light}
|
||||
[:div.w-4.h-4 svg/download]))]
|
||||
:route (:route grid-spec)
|
||||
:start start
|
||||
:per-page per-page
|
||||
@@ -149,6 +169,8 @@
|
||||
(conj
|
||||
(->> grid-spec
|
||||
:headers
|
||||
(filter (fn [h]
|
||||
((:render-for h #{:html :csv}) :html)))
|
||||
(map
|
||||
(fn [h]
|
||||
(cond
|
||||
@@ -179,11 +201,7 @@
|
||||
(com/data-grid-header {}))})))
|
||||
|
||||
|
||||
(defn sort->query [s]
|
||||
(str/join "," (map (fn [k] (format "%s:%s" (:sort-key k) (if (= true (:asc k))
|
||||
"asc"
|
||||
"desc")))
|
||||
s)))
|
||||
|
||||
|
||||
(defn default-unparse-query-params [query-params]
|
||||
(reduce
|
||||
@@ -267,8 +285,30 @@
|
||||
true (wrap-secure)
|
||||
true (wrap-client-redirect-unauthenticated)))
|
||||
|
||||
(defn page-route [grid-spec & {:keys [parse-query-params?] :or {parse-query-params? true}}]
|
||||
(defn csv-route [{:keys [fetch-page headers page->csv-entities]} & {:keys [parse-query-params?] :or {parse-query-params? true}}]
|
||||
(cond-> (fn csv-route [{:keys [identity] :as request}]
|
||||
|
||||
(let [page-results (fetch-page (assoc-in request [:query-params :per-page] Long/MAX_VALUE))
|
||||
csv-entities ((or page->csv-entities (fn [[entities]] entities)) page-results)
|
||||
csv-content (with-open [i (java.io.StringWriter.)]
|
||||
(csv/write-csv i
|
||||
(into [(for [h headers
|
||||
:when ((:render-for h #{:html :csv}) :csv)]
|
||||
(:name h))]
|
||||
(for [e csv-entities ]
|
||||
(for [h headers
|
||||
:when ((:render-for h #{:html :csv})
|
||||
:csv)]
|
||||
((or (:render-csv h) (comp str (:render h))) e)))))
|
||||
(.toString i))]
|
||||
|
||||
{:headers {"Content-Type" "text/csv"}
|
||||
:body csv-content}))
|
||||
true (wrap-trim-client-ids)
|
||||
true (wrap-secure)
|
||||
true (wrap-client-redirect-unauthenticated)))
|
||||
|
||||
(defn page-route [grid-spec & {:keys [parse-query-params?] :or {parse-query-params? true}}]
|
||||
(cond-> (fn page [{:keys [identity] :as request}]
|
||||
(alog/info ::page-route
|
||||
:pqp (:parsed-query-params request)
|
||||
@@ -312,7 +352,7 @@
|
||||
[:name :string]
|
||||
[:header-class {:optional true} [:maybe :string]]
|
||||
[:sort-key {:optional true} :string]
|
||||
[:render [:=> [:cat entity-spec] :any]]
|
||||
[:render {:optional true} [:=> [:cat entity-spec] :any]]
|
||||
[:hide? {:optional true} [:=> [:cat entity-spec] :boolean]]]))
|
||||
(def grid-spec (m/schema [:map
|
||||
[:id :string]
|
||||
@@ -356,6 +396,7 @@
|
||||
:string]]]
|
||||
[:entity-name :string]
|
||||
[:route :keyword]
|
||||
[:csv-route {:optional true} [:maybe :keyword]]
|
||||
[:action-buttons
|
||||
{:default (fn [request])
|
||||
:optional true}
|
||||
|
||||
Reference in New Issue
Block a user