This commit is contained in:
2023-08-31 23:24:42 -07:00
parent 1d82ec29e0
commit 7d251c8398
40 changed files with 333 additions and 286 deletions

View File

@@ -1,8 +1,7 @@
(ns auto-ap.ssr.company-dropdown
(:require
[auto-ap.datomic :refer [conn pull-many]]
[auto-ap.graphql.utils
:refer [assert-can-see-client cleanse-query is-admin?]]
[auto-ap.datomic :refer [conn pull-attr pull-many]]
[auto-ap.graphql.utils :refer [assert-can-see-client cleanse-query]]
[auto-ap.solr :as solr]
[auto-ap.ssr-routes :as ssr-routes]
[auto-ap.ssr.svg :as svg]
@@ -17,10 +16,14 @@
(for [{:keys [id name]} options]
[:li
[:div {:class "flex items-center pl-2 rounded hover:bg-green-100 dark:hover:bg-green-600"}
[:a {:href "#" :class "w-full py-2 ml-2 text-sm font-medium text-gray-900 rounded dark:text-gray-300"
"_" (hiccup/raw "on click set value of <#company-search-value/> to @data-value then send selected to #company-dropdown")
:data-value id}
[:a {:href "#" :class "w-full py-2 ml-2 text-sm font-medium text-gray-900 rounded dark:text-gray-300"
:hx-put (bidi/path-for ssr-routes/only-routes
:active-client
:request-method :put)
:hx-target "#company-dropdown"
:hx-headers (format "{\"x-clients\": \"[%d]\"}" id)
:hx-swap "outerHTML"
:hx-trigger "click"}
name]]])])
@@ -41,27 +44,29 @@
(defn dropdown-search-results [{:keys [identity] :as request}]
(html-response
(dropdown-search-results* {:options (get-clients identity (get (:query-params request) "search-text"))
:client (:client (:session request))})))
(dropdown-search-results* {:options (get-clients identity (get (:query-params request) "search-text"))})))
(defn dropdown [{:keys [client]}]
(defn dropdown [{:keys [client-selection client identity]}]
[:div#company-dropdown
{:hx-put (bidi/path-for ssr-routes/only-routes
:active-client
:request-method :put)
:hx-target "#company-dropdown"
:hx-include "#company-search-value"
:hx-swap "outerHTML"
:hx-trigger "selected"}
[:script
(hiccup/raw
"localStorage.setItem(\"last-client-id\", \""(:db/id client)"\")")]
"localStorage.setItem(\"last-client-id\", \"" (:db/id client) "\")" "\n"
"localStorage.setItem(\"last-selected-clients\", \"" client-selection "\")"
)]
[:div
[:button#company-dropdown-button { :class "text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-4 py-2.5 text-center inline-flex items-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"
:type "button"}
(if client
(:client/name client)
"All Companies")
(cond
(= :mine client-selection)
"My Companies"
(= :all client-selection)
"All Companies"
(and (sequential? client-selection)
(= 1 (count client-selection)))
(pull-attr (dc/db conn) :client/name (first client-selection))
:else
(str (count client-selection) " Companies"))
[:div.w-4.h-4.ml-2
svg/drop-down]]
[:div#company-dropdown-list.hidden {"_" (hiccup/raw "init call initCompanyDropdown()")
@@ -84,9 +89,21 @@
:hx-target "#company-search-results"
:hx-swap "innerHTML"} ]]
[:input#company-search-value {:type "hidden"
:name "search-client"}]]
:name "x-clients"}]]
[:div.divide-y.divide-gray-100
[:div#company-search-results {:class "h-48 px-3 pb-3 overflow-y-auto text-sm text-gray-700 dark:text-gray-200"}]
(when (= "admin" (:user/role identity))
[:div {:class "flex items-center pl-2 rounded hover:bg-green-100 dark:hover:bg-green-600"}
[:button {:class "w-full py-2 ml-2 text-sm font-medium text-gray-900 rounded dark:text-gray-300"
:hx-put (bidi/path-for ssr-routes/only-routes
:active-client
:request-method :put)
:hx-target "#company-dropdown"
:hx-headers "{\"x-clients\": \":mine\"}"
:hx-swap "outerHTML"
:hx-trigger "click"}
"Mine"]])
[:div {:class "flex items-center pl-2 rounded hover:bg-green-100 dark:hover:bg-green-600"}
[:button {:class "w-full py-2 ml-2 text-sm font-medium text-gray-900 rounded dark:text-gray-300"
@@ -94,6 +111,7 @@
:active-client
:request-method :put)
:hx-target "#company-dropdown"
:hx-headers "{\"x-clients\": \":all\"}"
:hx-swap "outerHTML"
:hx-trigger "click"}
"All"]]]
@@ -126,48 +144,10 @@ function initCompanyDropdown() {
")]]])
(defn active-client [{:keys [identity params] :as request}]
(let [client-search (or (:search-client params)
(get params "search-client"))
client-id (try (some-> client-search
(not-empty )
Long/parseLong)
(catch Exception _
nil))]
(when client-id
(assert-can-see-client identity client-id))
(let [new-session (assoc (:session request)
:client
(when client-id
(dc/pull (dc/db conn) [:db/id :client/name :client/code] client-id))
:clients
(cond (int? client-id)
[(dc/pull (dc/db conn) [:db/id :client/name :client/code] client-id)]
(= ":mine" client-search)
(->> (dc/q '[:find (pull ?c [:db/id :client/name :client/code])
:in $ ?u
:where [?u :user/clients ?c]]
(dc/db conn)
(:db/id identity))
(map first))
(and (or (empty? client-search)
(= ":all" client-search))
(is-admin? identity))
(->> (dc/q '[:find (pull ?c [:db/id :client/name :client/code])
:in $ ?u
:where [?c :client/code]]
(dc/db conn)
(:db/id identity))
(map first))))]
(assoc
(html-response
(dropdown {:client (:client new-session)
:identity identity}))
:session
new-session
:headers
{"hx-trigger" "clientSelected"}))))
(assoc
(html-response
(dropdown {:client-selection (:client-selection (:session request))
:client (:client request)
:identity identity}))
:headers
{"hx-trigger" "clientSelected"}))