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

@@ -0,0 +1,30 @@
(ns auto-ap.client-selection
(:require [clojure.string :as str]
[malli.core :as mc]
[malli.transform :as mt2]))
;; TODO remove this eventuall
(defn str->keyword [s]
(if (string? s)
(let [[ns k] (str/split s #"/")]
(if (and ns k)
(keyword ns k)
(keyword s)))
s))
(defn keyword->str [k]
(subs (str k) 1))
(def client-selection-schema
(mc/schema
[:orn
[:global [:enum :all :mine]]
[:group-name [:map [:group :string]]]
[:specific [:map [:selected [:vector nat-int?]]]]]))
(def client-selection-transformer
(mt2/transformer
mt2/json-transformer
mt2/string-transformer
(mt2/key-transformer {:encode keyword->str :decode str->keyword})))
;; END TODO

View File

@@ -1,27 +1,25 @@
(ns auto-ap.effects
(:require-macros [cljs.core.async.macros :refer [go]])
(:require
[auto-ap.history :as p]
[auto-ap.status :as status]
[auto-ap.views.utils :refer [date->str standard]]
[cemerick.url :as url]
[cljs-http.client :as http]
[cljs-time.coerce :as c]
[cljs-time.core :as time]
[cljs-time.format :as format]
[cljs.core.async :refer [<!] :as async]
[clojure.string :as str]
[clojure.walk :as walk]
[pushy.core :as pushy]
[re-frame.core :as re-frame]
[venia.core :as v]))
(:require [auto-ap.client-selection :refer [client-selection-schema]]
[auto-ap.history :as p]
[auto-ap.status :as status]
[auto-ap.views.utils :refer [date->str standard]]
[cemerick.url :as url]
[cljs-http.client :as http]
[cljs-time.coerce :as c]
[cljs-time.core :as time]
[cljs-time.format :as format]
[cljs.core.async :refer [<!] :as async]
[clojure.string :as str]
[clojure.walk :as walk]
[malli.core :as mc]
[pushy.core :as pushy]
[re-frame.core :as re-frame]
[venia.core :as v]))
(defn maybe-add-x-clients [headers]
(if (or (and (sequential? (:selected-clients @re-frame.db/app-db)) (every? int? (:selected-clients @re-frame.db/app-db)))
(and (sequential? (:selected-clients @re-frame.db/app-db)) (every? string? (:selected-clients @re-frame.db/app-db)))
(and (sequential? (:selected-clients @re-frame.db/app-db)) (= :group (first (:selected-clients @re-frame.db/app-db))))
(keyword? (:selected-clients @re-frame.db/app-db)))
(assoc headers "x-clients" (pr-str (:selected-clients @re-frame.db/app-db)))
(if (mc/validate client-selection-schema (:selected-clients @re-frame.db/app-db))
(assoc headers "x-clients" (.stringify js/JSON (clj->js (:selected-clients @re-frame.db/app-db))))
headers))
(re-frame/reg-fx

View File

@@ -1,24 +1,24 @@
(ns auto-ap.events
(:require
[auto-ap.db :as db]
[auto-ap.routes :as routes]
[auto-ap.utils :refer [by]]
[auto-ap.views.pages.data-page :as data-page]
[auto-ap.views.utils :refer [parse-jwt with-user gunzip]]
[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]
[cemerick.url :as url]
[auto-ap.subs :as subs]
[pako]))
(:require [auto-ap.client-selection :refer [client-selection-schema
client-selection-transformer]]
[auto-ap.db :as db]
[auto-ap.routes :as routes]
[auto-ap.ssr-routes :as ssr-routes]
[auto-ap.utils :refer [by]]
[auto-ap.views.pages.data-page :as data-page]
[auto-ap.views.utils :refer [gunzip parse-jwt with-user]]
[bidi.bidi :as bidi]
[cemerick.url :as url]
[clojure.string :as str]
[goog.crypt.base64 :as b64]
[malli.core :as mc]
[pako]
[re-frame.core :as re-frame]))
(defn jwt->data [token]
(let [raw (js->clj (.parse js/JSON (b64/decodeString (second (str/split token #"\." )))))
(let [raw (js->clj (.parse js/JSON (b64/decodeString (second (str/split token #"\.")))))
gz-clients (or (:gz-clients raw)
(get raw "gz-clients"))]
(cond-> raw
@@ -29,7 +29,7 @@
(defn client-query []
(cond-> [:id :name :code :email :locations :feature-flags :groups
[:emails [:id :email :description]]
[:bank-accounts [:id :code :bank-name :name :type :visible
[:bank-accounts [:id :code :bank-name :name :type :visible
:locations :include-in-reports :current-balance
:sort-order]]]))
@@ -46,18 +46,22 @@
[:plaid-account [:name :id :number]]
[:intuit-bank-account [:name :id :external-id]]
:use-date-instead-of-post-date
:locations :include-in-reports :current-balance :yodlee-balance-old] ]
:locations :include-in-reports :current-balance :yodlee-balance-old]]
[:address [:id :street1 :street2 :city :state :zip]]
[:forecasted-transactions [:id :amount :identifier :day-of-month]]]
(= "admin" (or (get (jwt->data token) "role") (get (jwt->data token) "user/role")) ) (into [[:yodlee-provider-accounts [:id [:accounts [:id :name :number :available-balance]]]]
[:plaid-items [:id [:accounts [:id :name :number :balance]]]]])))
(= "admin" (or (get (jwt->data token) "role") (get (jwt->data token) "user/role"))) (into [[:yodlee-provider-accounts [:id [:accounts [:id :name :number :available-balance]]]]
[:plaid-items [:id [:accounts [:id :name :number :balance]]]]])))
(re-frame/reg-event-fx
::initialize-db
(fn [{:keys [_]} [_ token]]
(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"))
last-selected-clients (js->clj (.parse js/JSON (.getItem js/localStorage "last-selected-clients")))
last-selected-clients (mc/decode client-selection-schema last-selected-clients client-selection-transformer)
jwt-data (some-> token jwt->data)
selected-client-assignment (cond (and token
(= "admin" (get jwt-data "user/role"))
@@ -70,9 +74,9 @@
[(js/parseInt last-client-id)]
:else
nil)]
nil) ]
(cond
(= :login handler)
{:db (cond-> (assoc db/default-db
@@ -89,7 +93,7 @@
:selected-clients last-selected-clients
:user token)}
(and token (= "none" (or (get jwt-data "role") (get jwt-data "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
@@ -112,7 +116,7 @@
:on-success [::received-initial]
:on-error [::failed-initial]}}
selected-client-assignment
(assoc :set-local-storage ["last-selected-clients" selected-client-assignment]))))))
(assoc :set-local-storage ["last-selected-clients" (.stringify js/JSON selected-client-assignment)]))))))
(re-frame/reg-event-db
@@ -125,13 +129,13 @@
::received-initial
(fn [{:keys [db]} [_ {clients :client}]]
(let [only-one-client (when (= 1 (count clients))
(->> clients first :id ))]
(->> clients first :id))]
(when only-one-client
(.setItem js/localStorage "last-client-id" only-one-client)
(.setItem js/localStorage "last-selected-clients"
(pr-str [(js/parseInt only-one-client)])))
{:db (cond-> (-> db
(assoc :clients (by :id clients) )
{:db (cond-> (-> db
(assoc :clients (by :id clients))
(assoc :is-initial-loading? false)
(assoc :client (or only-one-client
(->> clients
@@ -167,30 +171,31 @@
: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
::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"
(.stringify js/JSON
(clj->js (condp = client-identifier
:all
:all
:mine
:mine
:mine
:mine
(pr-str [(js/parseInt (:id client))])))
{:selected [(js/parseInt (:id client))]}))))
{:db (assoc db :client (:id client)
:selected-clients
(condp = client-identifier
:all
:all
{:db (assoc db :client (:id client)
:selected-clients
(condp = client-identifier
:all
:all
:mine
:mine
:mine
:mine
[(js/parseInt (:id client))]))}))
{:selected [(js/parseInt (:id client))]}))}))
(re-frame/reg-event-fx
::swap-client
@@ -210,7 +215,7 @@
(re-frame/reg-event-fx
::set-active-route
(fn [{:keys [db]} [_ handler params route-params]]
(cond
(cond
(and (not= :login handler) (not (:user db)))
{:redirect (bidi/path-for routes/routes :login)
:db (assoc db :active-route :login
@@ -256,36 +261,36 @@
(fn [{:keys [db]} _]
{:graphql {:token (:user db)
:query-obj {:venia/queries [[:yodlee-merchants
[:name :yodlee-id :id]]]}
[:name :yodlee-id :id]]]}
:on-success [::yodlee-merchants-received]}}))
(re-frame/reg-event-fx
::vendor-preferences-requested
[with-user]
(fn [{:keys [user]} [_ {:keys [ client-id vendor-id on-success on-failure owns-state]}]]
{:graphql {:token user
:query-obj {:venia/queries [[:vendor-by-id
{:id vendor-id}
[[:automatically-paid-when-due [:id]]
[:schedule-payment-dom [[:client [:id]] :dom]]
[:default-account [:id]]]]
[:account-for-vendor
{:vendor-id vendor-id
:client-id client-id}
[:name :id :numeric-code :location]]]}
:owns-state owns-state
:on-success (fn [r]
(let [schedule-payment-dom (->> r
:vendor-by-id
:schedule-payment-dom
(filter (fn [spd]
(= (-> spd :client :id)
client-id)))
first
:dom)
automatically-paid-when-due (boolean ((->> r :vendor-by-id :automatically-paid-when-due (map :id) set) client-id))]
(conj on-success {:default-account (:account-for-vendor r)
:schedule-payment-dom schedule-payment-dom
:automatically-paid-when-due automatically-paid-when-due
:vendor-autopay? (or automatically-paid-when-due (boolean schedule-payment-dom))})))
:on-failure on-failure}}))
::vendor-preferences-requested
[with-user]
(fn [{:keys [user]} [_ {:keys [client-id vendor-id on-success on-failure owns-state]}]]
{:graphql {:token user
:query-obj {:venia/queries [[:vendor-by-id
{:id vendor-id}
[[:automatically-paid-when-due [:id]]
[:schedule-payment-dom [[:client [:id]] :dom]]
[:default-account [:id]]]]
[:account-for-vendor
{:vendor-id vendor-id
:client-id client-id}
[:name :id :numeric-code :location]]]}
:owns-state owns-state
:on-success (fn [r]
(let [schedule-payment-dom (->> r
:vendor-by-id
:schedule-payment-dom
(filter (fn [spd]
(= (-> spd :client :id)
client-id)))
first
:dom)
automatically-paid-when-due (boolean ((->> r :vendor-by-id :automatically-paid-when-due (map :id) set) client-id))]
(conj on-success {:default-account (:account-for-vendor r)
:schedule-payment-dom schedule-payment-dom
:automatically-paid-when-due automatically-paid-when-due
:vendor-autopay? (or automatically-paid-when-due (boolean schedule-payment-dom))})))
:on-failure on-failure}}))

View File

@@ -17,7 +17,6 @@
::client
:<- [::selected-clients]
(fn [selected-clients]
(println "SELECTED CLIENTS ARE" selected-clients)
(when (= 1 (count selected-clients))
(first selected-clients))))
@@ -44,10 +43,6 @@
:<- [::user]
:<- [::clients]
(fn [[selected-clients user clients]]
(println "SELECTED" selected-clients
"USER" user
"CLIENTS" (count clients))
(cond (= :mine selected-clients)
(sort-by :name
(:user/clients user))
@@ -58,17 +53,15 @@
(nil? selected-clients))
clients
(= :group (and (sequential? selected-clients)
(first selected-clients)))
(let [group (second selected-clients)]
(:group selected-clients)
(let [group (:group selected-clients)]
(filterv
(fn [c]
(println "GROUP" group (:groups c))
((set (:groups c)) group))
clients))
(sequential? selected-clients)
(filter (comp (set (map coerce-string-version selected-clients)) coerce-string-version :id)
(:selected selected-clients)
(filter (comp (set (:selected selected-clients)) coerce-string-version :id)
clients)
:else