makes client selection not contanimate other tabs

This commit is contained in:
Bryce
2024-04-09 23:27:35 -07:00
parent 10a60dcee2
commit ff2bf4c2b3
20 changed files with 228 additions and 183 deletions

View File

@@ -2,7 +2,6 @@
(:require [amazonica.core :refer [defcredential]]
[auto-ap.client-routes :as client-routes]
[auto-ap.datomic :refer [conn pull-many]]
[auto-ap.datomic.clients :as d-clients]
[auto-ap.graphql.utils :refer [assert-can-see-client
limited-clients]]
[auto-ap.logging :as alog]
@@ -16,6 +15,7 @@
[auto-ap.routes.yodlee2 :as yodlee2]
[auto-ap.ssr-routes :as ssr-routes]
[auto-ap.ssr.core :as ssr]
[auto-ap.ssr.utils :refer [entity-id main-transformer]]
[bidi.bidi :as bidi]
[bidi.ring :refer [->ResourcesMaybe make-handler]]
[buddy.auth.backends.session :refer [session-backend]]
@@ -26,13 +26,14 @@
[cheshire.core :as cheshire]
[clj-time.coerce :as coerce]
[clj-time.core :as time]
[clojure.edn :as edn]
[clojure.data.json :as json]
[clojure.set :as set]
[clojure.string :as str]
[com.brunobonacci.mulog :as mu]
[config.core :refer [env]]
[datomic.api :as dc]
[hiccup2.core :as hiccup]
[malli.core :as mc]
[ring.middleware.edn :refer [wrap-edn-params]]
[ring.middleware.multipart-params :as mp]
[ring.middleware.params :refer [wrap-params]]
@@ -110,10 +111,10 @@
uri
:request-method request-method))
matched-hx-current-url-route (some->> (get headers "hx-current-url")
url/url
:path
(bidi/match-route ssr-routes/only-routes)
:handler)]
url/url
:path
(bidi/match-route ssr-routes/only-routes)
:handler)]
(handler (assoc request
:matched-route
matched-route
@@ -135,6 +136,7 @@
(:uri request)
:request-method (:request-method request)))
:client-selection (:client-selection request)
:source "request"
:query (:uri request)
:request-method (:request-method request)
@@ -185,10 +187,17 @@
request (assoc request :hx-query-params query-params)]
(handler request))))
(def client-selection-schema
(mc/schema
[:orn
[:global [:enum :all :mine]]
[:group-name [:map [:group :string]]]
[:specific [:map [:selected [:vector entity-id]]]]]))
(defn wrap-hydrate-clients
[handler]
(fn [request]
(let [x-clients (-> request :session :client-selection)
(let [x-clients (-> request :client-selection)
identity (or (-> request :identity)
(-> request :session :identity))
ideal-ids (set (cond
@@ -202,28 +211,21 @@
(= :mine x-clients)
(map :db/id (:user/clients identity))
(= :group (first x-clients))
(:group x-clients)
(->>
(dc/q '[:find ?c
:in $ ?g
:where [?c :client/groups ?g]]
(dc/db conn)
(str/upper-case (or (second x-clients) "INVALID")))
(str/upper-case (or (:group x-clients) "INVALID")))
(map first)
set)
(seq x-clients)
(seq (:selected x-clients))
(->> x-clients
(map (fn [c]
(if (string? c)
(try
(Long/parseLong c)
(catch Exception e
nil))
c)))
:selected
(filter #(not (nil? %)))
set)))
limited-clients (some->> (limited-clients identity)
(map :db/id)
set)
@@ -236,12 +238,11 @@
(pull-many (dc/db conn)
'[:db/id :client/name :client/code :client/locations
:client/matches :client/feature-flags
{:client/bank-accounts [:db/id
{:bank-account/type [:db/ident]}
{:client/bank-accounts [:db/id
{:bank-account/type [:db/ident]}
:bank-account/number
:bank-account/name
:bank-account/code]}]))]
(mu/with-context {:clients (take 10 (map :client/code clients))}
(handler (assoc request
:clients clients
@@ -251,33 +252,22 @@
(defn wrap-store-client-in-session
[handler]
(fn [{:keys [headers identity] :as request}]
(let [x-clients (edn/read-string (get headers "x-clients"))
x-clients (try (if-let [client-id (and x-clients
(sequential? x-clients)
(first x-clients)
(not= :group (first x-clients))
(first x-clients))]
(do
(assert-can-see-client identity (cond-> client-id
(string? client-id) (Long/parseLong)))
[(if (string? client-id)
(Long/parseLong client-id)
client-id)])
x-clients)
(catch Exception e
(alog/warn ::cant-access :error e
:identity identity
:x-clients (pr-str x-clients))
:all))
new-request (if x-clients
(assoc-in request [:session :client-selection] x-clients)
request)]
(let [client-selection (try (mc/decode client-selection-schema (some-> (get headers "x-clients") not-empty json/read-str) main-transformer)
(catch Exception e
(alog/warn ::cant-access :error e
:identity identity
:x-clients (pr-str (get headers "x-clients")))
nil))
new-request (if client-selection
(assoc-in request [:client-selection] client-selection)
(assoc-in request [:client-selection] (get-in request [:session :client-selection] :all)))]
(cond-> (handler new-request)
x-clients (update :session
(fn [new-session]
(-> (:session request)
(into new-session)
(assoc :client-selection x-clients))))))))
client-selection (update :session
(fn [new-session]
(-> (:session request)
(into new-session)
(assoc :client-selection client-selection))))))))
(defn wrap-gunzip-jwt
[handler]
@@ -317,9 +307,9 @@
(-> route-handler
(wrap-hx-current-url-params)
(wrap-guess-route)
(wrap-logging)
(wrap-hydrate-clients)
(wrap-store-client-in-session)
(wrap-logging)
(wrap-gunzip-jwt)
(wrap-authorization auth-backend)
(wrap-authentication auth-backend

View File

@@ -193,8 +193,9 @@
(base-page
request
(com/page {:nav com/admin-aside-nav
:client-selection (:client-selection (:session request))
:client-selection (:client-selection request)
:client (:client request)
:clients (:clients request)
:identity (:identity request)
:request request
:app-params {:hx-get (bidi/path-for ssr-routes/only-routes

View File

@@ -45,7 +45,7 @@
(base-page
request
(com/page {:nav com/admin-aside-nav
:client-selection (:client-selection (:session request))
:client-selection (:client-selection request)
:clients (:clients request)
:client (:client request)
:identity (:identity request)}

View File

@@ -243,7 +243,7 @@
(base-page
request
(com/page {:nav com/admin-aside-nav
:client-selection (:client-selection (:session request))
:client-selection (:client-selection request)
:clients (:clients request)
:client (:client request)
:identity (:identity request)

View File

@@ -166,8 +166,9 @@
(some-> route-params (get :entity-id) Long/parseLong))]
(base-page request
(com/page {:nav com/admin-aside-nav
:client-selection (:client-selection (:session request))
:client-selection (:client-selection request)
:client (:client request)
:clients (:clients request)
:identity (:identity request)
:request request
:app-params {:hx-get (bidi/path-for ssr-routes/only-routes

View File

@@ -131,8 +131,9 @@
(base-page
request
(com/page {:nav com/company-aside-nav
:client-selection (:client-selection (:session request))
:client-selection (:client-selection request)
:client (:client request)
:clients (:clients request)
:identity (:identity request)
:app-params {:hx-get (bidi/path-for ssr-routes/only-routes
:company)

View File

@@ -1,6 +1,7 @@
(ns auto-ap.ssr.company-dropdown
(:require [auto-ap.datomic :refer [conn pull-attr pull-many]]
(:require [auto-ap.datomic :refer [conn pull-many]]
[auto-ap.graphql.utils :refer [cleanse-query]]
[auto-ap.logging :as alog]
[auto-ap.solr :as solr]
[auto-ap.ssr-routes :as ssr-routes]
[auto-ap.ssr.hx :as hx]
@@ -10,7 +11,9 @@
[clojure.string :as str]
[datomic.api :as dc]
[hiccup2.core :as hiccup]
[iol-ion.query :refer [can-see-client?]]))
[iol-ion.query :refer [can-see-client?]]
[clojure.data.json :as json]))
(defn dropdown-search-results* [{:keys [options]}]
[:ul
@@ -25,6 +28,7 @@
:request-method :put)
:hx-target "#company-dropdown"
:hx-headers (hx/json {"x-clients" (pr-str [:group group])})
"@click" (format "globalClientSelection={group: %s}" (hx/json group))
:hx-swap "outerHTML"
:hx-trigger "click"}
name]
@@ -34,6 +38,7 @@
:request-method :put)
:hx-target "#company-dropdown"
:hx-headers (format "{\"x-clients\": \"[%d]\"}" id)
"@click" (format "globalClientSelection={selected: [%d]}" id)
:hx-swap "outerHTML"
:hx-trigger "click"}
name])]])])
@@ -64,11 +69,18 @@
(dropdown-search-results* {:options (get-clients identity (get (:query-params request) "search-text"))})))
(defn dropdown [{:keys [client-selection client identity clients]}]
(alog/peek ::clients clients)
[:div#company-dropdown
[:script
(hiccup/raw
"localStorage.setItem(\"last-client-id\", \"" (:db/id client) "\")" "\n"
"localStorage.setItem(\"last-selected-clients\", " (pr-str (pr-str client-selection)) ")")]
"localStorage.setItem(\"last-selected-clients\", " (json/write-str (json/write-str client-selection))
#_(cond (:group client-selection)
(:group client-selection)
(:selected client-selection)
(:selected client-selection)
:else
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"}
@@ -80,7 +92,7 @@
(and client
(= 1 (count clients)))
( :client/name client)
(:client/name client)
:else
(str (count clients) " Companies"))
@@ -116,6 +128,8 @@
:active-client
:request-method :put)
:hx-target "#company-dropdown"
"@click" "globalClientSelection=\"mine\""
:hx-headers "{\"x-clients\": \":mine\"}"
:hx-swap "outerHTML"
:hx-trigger "click"}
@@ -127,6 +141,7 @@
:active-client
:request-method :put)
:hx-target "#company-dropdown"
"@click" "globalClientSelection=\"all\""
:hx-headers "{\"x-clients\": \":all\"}"
:hx-swap "outerHTML"
:hx-trigger "click"}
@@ -161,7 +176,7 @@ function initCompanyDropdown() {
(defn active-client [{:keys [identity params] :as request}]
(assoc
(html-response
(dropdown {:client-selection (:client-selection (:session request))
(dropdown {:client-selection (:client-selection request)
:clients (:clients request)
:client (:client request)
:identity identity}))

View File

@@ -27,7 +27,8 @@
[:li
[:a {:href (bidi/path-for ssr-routes/only-routes :company), :class "block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 dark:text-gray-300 dark:hover:bg-gray-600 dark:hover:text-white", :role "menuitem"} "My Company"]]
(when (= "admin" (:user/role identity))
[:a {:href (bidi/path-for ssr-routes/only-routes :auto-ap.routes.admin/page), :class "block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 dark:text-gray-300 dark:hover:bg-gray-600 dark:hover:text-white", :role "menuitem"} "Admin"])
[:a {:href (bidi/path-for ssr-routes/only-routes :auto-ap.routes.admin/page),
:class "block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 dark:text-gray-300 dark:hover:bg-gray-600 dark:hover:text-white", :role "menuitem"} "Admin"])
[:li
[:a {:href "#", :class "block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 dark:text-gray-300 dark:hover:bg-gray-600 dark:hover:text-white", :role "menuitem"
"_" (hiccup/raw "on click toggle .dark on <body />")}

View File

@@ -245,7 +245,7 @@
(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))
:client-selection (:client-selection request)
:clients (:clients request)
:client (:client request)
:identity (:identity request)

View File

@@ -421,8 +421,9 @@ invoice_dropzone = new Dropzone(\"#invoice\", {
(base-page
request
(com/page {:nav com/admin-aside-nav
:client-selection (:client-selection (:session request))
:client-selection (:client-selection request)
:client (:client request)
:clients (:clients request)
:identity (:identity request)
:app-params {:hx-get (bidi/path-for ssr-routes/only-routes
:invoice-glimpse)

View File

@@ -120,8 +120,6 @@
(exact-match-id* request)]])
(defn fetch-ids [db {:keys [query-params route-params] :as request}]
(let [valid-clients (extract-client-ids (:clients request)
(:client-id request)
@@ -556,10 +554,10 @@
_ (audit-transact tx identity)]
(alog/info ::unvoiding-invoice :transaction :tx)
(html-response
(row* identity (dc/pull (dc/db conn) default-read id) {:flash? true
:request request})
:headers (cond-> {"hx-retarget" (format "#entity-table tr[data-id=\"%d\"]" id)
"hx-reswap" "outerHTML"}))))
(row* identity (dc/pull (dc/db conn) default-read id) {:flash? true
:request request})
:headers (cond-> {"hx-retarget" (format "#entity-table tr[data-id=\"%d\"]" id)
"hx-reswap" "outerHTML"}))))
(defn delete [{invoice :entity :as request identity :identity}]
(exception->notification
@@ -1137,7 +1135,7 @@
(->> (dc/q '[:find ?i
:in $ [?i ...]
:where [?i :invoice/status :invoice-status/unpaid]
[?i :invoice/client ?c] ]
[?i :invoice/client ?c]]
(dc/db conn)
ids)
(map first)))

View File

@@ -251,7 +251,7 @@
(base-page
request
(com/page {:nav com/main-aside-nav
:client-selection (:client-selection (:session request))
:client-selection (:client-selection request)
:clients (:clients request)
:client (:client request)
:identity (:identity request)

View File

@@ -318,8 +318,9 @@
(base-page
request
(com/page {:nav com/main-aside-nav
:client-selection (:client-selection (:session request))
:client-selection (:client-selection request)
:client (:client request)
:clients (:clients request)
:identity (:identity request)
:app-params {:hx-get (bidi/path-for ssr-routes/only-routes
:transaction-insights)

View File

@@ -69,9 +69,10 @@ input::-webkit-inner-spin-button {
input[type=number] {
-moz-appearance:textfield; /* Firefox */
} "]
[:body {:hx-ext "disable-submit, class-tools"}
[:body {:hx-ext "disable-submit, class-tools"
:x-data (hx/json {:globalClientSelection (or (:client-selection request)
:all )}) ;; TODO remove once session is used
:x-hx-header:x-clients "JSON.stringify(globalClientSelection)"}
contents
[:script {:src "/js/flowbite.min.js"}]

View File

@@ -107,7 +107,7 @@
(mt2/transformer
{:decoders
{:map (fn [m]
(if (not (seq (filter identity (vals m))))
(if (and (map? m) (not (seq (filter identity (vals m)))))
nil
m))
:string empty->nil