fixes
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
[auto-ap.views.utils :refer [parse-jwt with-user]]
|
||||
[bidi.bidi :as bidi]
|
||||
[clojure.string :as str]
|
||||
[clojure.edn :as edn]
|
||||
[goog.crypt.base64 :as b64]
|
||||
[re-frame.core :as re-frame]
|
||||
[auto-ap.ssr-routes :as ssr-routes]
|
||||
@@ -46,39 +47,64 @@
|
||||
(re-frame/reg-event-fx
|
||||
::initialize-db
|
||||
(fn [{:keys [_]} [_ token]]
|
||||
(let [handler (:handler (bidi/match-route routes/routes (.. js/window -location -pathname)))]
|
||||
(let [handler (:handler (bidi/match-route routes/routes (.. js/window -location -pathname)))
|
||||
last-client-id (.getItem js/localStorage "last-client-id")
|
||||
last-selected-clients (edn/read-string (.getItem js/localStorage "last-selected-clients"))
|
||||
jwt-data (jwt->data token)
|
||||
selected-client-assignment (cond (and token
|
||||
(= "admin" (get jwt-data "user/role"))
|
||||
(not last-selected-clients))
|
||||
:mine
|
||||
|
||||
(and token
|
||||
last-client-id
|
||||
(not last-selected-clients))
|
||||
[(js/parseInt last-client-id)]
|
||||
|
||||
:else
|
||||
nil)]
|
||||
|
||||
|
||||
(cond
|
||||
(= :login handler)
|
||||
{:db (assoc db/default-db
|
||||
:active-route :login
|
||||
:last-client-id (.getItem js/localStorage "last-client-id")
|
||||
:user nil)}
|
||||
{:db (cond-> (assoc db/default-db
|
||||
:active-route :login
|
||||
:last-client-id last-client-id
|
||||
:selected-clients last-selected-clients
|
||||
:user nil))}
|
||||
|
||||
(and (not= :login handler) (not token))
|
||||
{:redirect "/login"
|
||||
:db (assoc db/default-db
|
||||
:active-route :login
|
||||
:last-client-id (.getItem js/localStorage "last-client-id")
|
||||
:last-client-id last-client-id
|
||||
:selected-clients last-selected-clients
|
||||
:user token)}
|
||||
|
||||
(and token (= "none" (or (get (jwt->data token) "role") (get (jwt->data token) "user/role")) ))
|
||||
(and token (= "none" (or (get jwt-data "role") (get jwt-data "user/role")) ))
|
||||
{:redirect "/needs-activation"
|
||||
:db (assoc db/default-db
|
||||
:active-route :needs-activation
|
||||
:last-client-id (.getItem js/localStorage "last-client-id")
|
||||
:last-client-id last-client-id
|
||||
:selected-clients last-selected-clients
|
||||
:user token)}
|
||||
|
||||
:else
|
||||
{:db (assoc db/default-db
|
||||
:active-route handler
|
||||
:is-initial-loading? true
|
||||
:last-client-id (.getItem js/localStorage "last-client-id")
|
||||
:query-params (auto-ap.views.utils/query-params)
|
||||
:user token)
|
||||
:graphql {:token token
|
||||
:query-obj {:venia/queries [[:client (client-query)]]}
|
||||
:on-success [::received-initial]
|
||||
:on-error [::failed-initial]}}))))
|
||||
(cond-> {:db (cond-> (assoc db/default-db
|
||||
:active-route handler
|
||||
:is-initial-loading? true
|
||||
:last-client-id last-client-id
|
||||
:selected-clients last-selected-clients
|
||||
:query-params (auto-ap.views.utils/query-params)
|
||||
:user token)
|
||||
selected-client-assignment
|
||||
(assoc :selected-clients selected-client-assignment))
|
||||
:graphql {:token token
|
||||
:query-obj {:venia/queries [[:client (client-query)]]}
|
||||
:on-success [::received-initial]
|
||||
:on-error [::failed-initial]}}
|
||||
selected-client-assignment
|
||||
(assoc :set-local-storage ["last-selected-clients" selected-client-assignment]))))))
|
||||
|
||||
|
||||
(re-frame/reg-event-db
|
||||
@@ -124,40 +150,46 @@
|
||||
:is-initial-loading? false
|
||||
:active-route :initial-error)))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::swapped-client
|
||||
(fn [{:keys [db]} [_ client client-identifier]]
|
||||
(when (:id client)
|
||||
(.setItem js/localStorage "last-client-id" (:id client)))
|
||||
(.setItem js/localStorage "last-selected-clients"
|
||||
(condp = client-identifier
|
||||
:all
|
||||
:all
|
||||
|
||||
:mine
|
||||
:mine
|
||||
|
||||
(pr-str [(js/parseInt (:id client))])))
|
||||
|
||||
{:db (assoc db :client (:id client)
|
||||
:selected-clients
|
||||
(condp = client-identifier
|
||||
:all
|
||||
:all
|
||||
|
||||
:mine
|
||||
:mine
|
||||
|
||||
[(js/parseInt (:id client))]))}))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::swap-client
|
||||
[with-user]
|
||||
(fn [{:keys [db user]} [_ client]]
|
||||
(let [client-identifier (or (:id client) client)]
|
||||
(when (:id client)
|
||||
(.setItem js/localStorage "last-client-id" (:id client)))
|
||||
(.setItem js/localStorage "last-selected-clients"
|
||||
(condp = client-identifier
|
||||
:all
|
||||
:all
|
||||
|
||||
:mine
|
||||
:mine
|
||||
|
||||
(pr-str [(js/parseInt (:id client))])))
|
||||
|
||||
{:db (assoc db :client (:id client)
|
||||
:selected-clients
|
||||
(condp = client-identifier
|
||||
:all
|
||||
:all
|
||||
|
||||
:mine
|
||||
:mine
|
||||
|
||||
[(js/parseInt (:id client))]))
|
||||
:http {:token user
|
||||
{:http {:token user
|
||||
:method :put
|
||||
:uri (str (bidi/path-for ssr-routes/only-routes
|
||||
:active-client
|
||||
:request-method :put)
|
||||
"?"
|
||||
(url/map->query {:search-client client-identifier}))}})))
|
||||
(url/map->query {:search-client client-identifier}))
|
||||
:on-success [::swapped-client client client-identifier]}})))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::set-active-route
|
||||
|
||||
Reference in New Issue
Block a user