adds permissions to page.

This commit is contained in:
2023-01-21 21:21:45 -08:00
parent 221adb24f8
commit 696e147fa5
2 changed files with 143 additions and 0 deletions

View File

@@ -0,0 +1,103 @@
(ns auto-ap.ssr.company-dropdown
(:require
[auto-ap.datomic :refer [conn]]
[auto-ap.graphql.utils :refer [assert-can-see-client can-see-client?]]
[auto-ap.ssr-routes :as ssr-routes]
[auto-ap.ssr.utils :refer [html-response]]
[bidi.bidi :as bidi]
[datomic.api :as d]
[hiccup2.core :as hiccup]))
(defn dropdown-contents [{:keys [identity]}]
(let [options (->> (d/q '[:find ?c ?n
:in $ ?user
:where [?c :client/name ?n]
[(auto-ap.graphql.utils/can-see-client? ?user ?c)]]
(d/db conn)
identity)
(map (fn [[k v]]
{"key" k
"value" v})))]
(html-response
[:div.navbar-dropdown {:style {:width "20em"}}
[:div.navbar-item "All"]
[:hr.navbar-divider]
[:input#company-search.input.navbar-item {:placeholder "Company name"
:name "search"
:autoFocus true} ]
[:input#company-search-value {:type "hidden"
:name "search"
:hx-put (bidi/path-for ssr-routes/only-routes
:active-client
:request-method :put)
:hx-target "#company-dropdown"
:hx-swap "outerHTML"
:hx-trigger "change"} ]
[:script
(hiccup/raw
(str "
var z = new autoComplete({
selector:\"#company-search\",
placeholder: \"Company Name....\",
data: {
keys: [\"value\"],
src: " (cheshire.core/encode
options)
"
},
resultItem: {
highlight:true,
class: \"autocomplete-suggestion\",
selected: \"highlighted\"
},
resultsList: {
tabSelect: true
},
submit: true
});
z.input.addEventListener(\"selection\", function (event) {
z.input.blur();
z.input.value = event.detail.selection.value.value;
document.getElementById(\"company-search-value\").value= event.detail.selection.value.key;
document.getElementById(\"company-search-value\").dispatchEvent(new Event('change'));
});
"))]])))
(defn dropdown [request]
(let [client (get-in request [:session :client])]
[:div#company-dropdown.navbar-item.has-dropdown {:tabIndex 0 "_" (hiccup/raw
"on click elsewhere
remove .is-active from <#company-dropdown />
end "
)} ;; remove .is-active from <#company-dropdown />
[:a.navbar-link {:hx-get
(bidi/path-for ssr-routes/only-routes
:company-dropdown-contents)
:hx-target "#company-dropdown .navbar-dropdown"
:hx-swap "outerHTML"
"_" (hiccup/raw
"on click
toggle .is-active on <#company-dropdown />
then focus() on next <#company-dropdown input/>
end
")
}
(if client
(str "Company: " (:client/name client))
"Company")
[:div.navbar-dropdown]]
]))
;; TODO PERMS
(defn active-client [{:keys [identity params] :as request}]
(assert-can-see-client identity (Long/parseLong (:search params)))
(let [new-session (assoc (:session request) :client
(d/pull (d/db conn) [:db/id :client/name :client/code] (Long/parseLong (:search params))))]
(assoc
(html-response
(dropdown (assoc request :session new-session)))
:session
new-session)))

View File

@@ -0,0 +1,40 @@
(ns auto-ap.ssr.utils
(:require
[auto-ap.logging :as alog]
[config.core :refer [env]]
[hiccup2.core :as hiccup]))
(defn html-response [hiccup & {:keys [status] :or {status 200}}]
{:status status
:headers {"Content-Type" "text/html"}
:body (str
(hiccup/html
{}
hiccup))})
(defn wrap-error-response [handler]
(fn [request]
(try
(handler request)
(catch Exception e
(if-let [v (or (:validation-error (ex-data e))
(:validation-error (ex-data (.getCause e))))]
(do
(alog/warn ::request-validation-error
:exception e)
(html-response
[:div.notification.is-warning.is-light
v]
:status 400))
(do
(alog/error ::request-error
:exception e)
(when (= "dev" (:dd-env env))
(println e))
(html-response
[:div.notification.is-danger.is-light
"Server error occured."
(ex-message e)]
:status 500)))))))