refactoring.

This commit is contained in:
BC
2019-02-18 15:35:46 -08:00
parent 9734b3a490
commit f1a6ada3b8
3 changed files with 33 additions and 73 deletions

View File

@@ -116,9 +116,11 @@
(println "PARAMS" params) (println "PARAMS" params)
(if (and (not= :login handler) (not (:user db))) (if (and (not= :login handler) (not (:user db)))
{:redirect "/login" {:redirect "/login"
:db (assoc db :active-page :login)} :db (assoc db :active-page :login
:auto-ap.forms/forms nil)}
{:db (assoc db :active-page handler {:db (assoc db :active-page handler
:query-params params)}))) :query-params params
:auto-ap.forms/forms nil)})))
(re-frame/reg-event-db (re-frame/reg-event-db
::imported-invoices ::imported-invoices

View File

@@ -90,7 +90,7 @@
[:div.sub-main {} children ]] [:div.sub-main {} children ]]
[:div])]))) [:div])])))
(defn side-bar-layout [{:keys [side-bar main ap bottom right-side-bar right-side-bar-visible?]}] (defn side-bar-layout [{:keys [side-bar main ap bottom right-side-bar]}]
(let [ap @(re-frame/subscribe [::subs/active-page]) (let [ap @(re-frame/subscribe [::subs/active-page])
client @(re-frame/subscribe [::subs/client])] client @(re-frame/subscribe [::subs/client])]
[:div [:div
@@ -103,7 +103,9 @@
[:div {:class "column messages hero " :style { :overflow "auto" }, :id "message-feed"} [:div {:class "column messages hero " :style { :overflow "auto" }, :id "message-feed"}
^{:key (str "active-page-" (:name client))} ^{:key (str "active-page-" (:name client))}
[:div.inbox-messages main]] [:div.inbox-messages main]]
[appearing-side-bar {:visible? right-side-bar-visible?} right-side-bar]] (when right-side-bar
right-side-bar)
]
#_[footer] #_[footer]
bottom bottom
[:div#dz-hidden]])) [:div#dz-hidden]]))

View File

@@ -7,10 +7,11 @@
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
[clojure.string :as str] [clojure.string :as str]
[auto-ap.subs :as subs] [auto-ap.subs :as subs]
[auto-ap.forms :as forms]
[auto-ap.events :as events] [auto-ap.events :as events]
[auto-ap.entities.clients :as entity] [auto-ap.entities.clients :as entity]
[auto-ap.views.components.address :refer [address-field]] [auto-ap.views.components.address :refer [address-field]]
[auto-ap.views.components.layouts :refer [side-bar-layout]] [auto-ap.views.components.layouts :refer [side-bar-layout appearing-side-bar]]
[auto-ap.views.components.admin.side-bar :refer [admin-side-bar]] [auto-ap.views.components.admin.side-bar :refer [admin-side-bar]]
[auto-ap.views.utils :refer [login-url dispatch-event dispatch-value-change bind-field horizontal-field]] [auto-ap.views.utils :refer [login-url dispatch-event dispatch-value-change bind-field horizontal-field]]
[auto-ap.views.components.modal :refer [action-modal]] [auto-ap.views.components.modal :refer [action-modal]]
@@ -18,39 +19,11 @@
[auto-ap.routes :as routes] [auto-ap.routes :as routes]
[bidi.bidi :as bidi])) [bidi.bidi :as bidi]))
(re-frame/reg-sub
::form
(fn [db [_ x]]
(-> db ::forms x)))
(re-frame/reg-sub
::loading-class
(fn [db [_ x]]
(if (= (get-in db [::forms x :status]) "loading")
"is-loading"
"")))
(defn start-form [db form data]
(assoc-in db [::forms form] {:error nil
:status nil
:data data}))
(defn stop-form [db form]
(update db ::forms dissoc form))
(re-frame/reg-event-db
::form-closing
(fn [db [_ f]]
(-> db
(assoc-in [:admin :adding-client?] false)
(stop-form f))))
(re-frame/reg-event-db (re-frame/reg-event-db
::new ::new
(fn [db [_ client-id]] (fn [db [_ client-id]]
(-> db (-> db
(assoc-in [:admin :adding-client?] true) (forms/start-form ::new-client {}))))
(start-form ::new-client (assoc {} :new-account {:type :check})))))
@@ -58,12 +31,11 @@
::edit-client-clicked ::edit-client-clicked
(fn [{:keys [db]} [_ client-id]] (fn [{:keys [db]} [_ client-id]]
{:db (-> db {:db (-> db
(start-form ::new-client (assoc (get (:clients db) client-id) :new-account {:type :check})) (forms/start-form ::new-client (get (:clients db) client-id)))}))
(assoc-in [:admin :adding-client? ] true))}))
(re-frame/reg-sub (re-frame/reg-sub
::new-client-request ::new-client-request
:<- [::form ::new-client] :<- [::forms/form ::new-client]
(fn [{new-client-data :data} _] (fn [{new-client-data :data} _]
{:id (:id new-client-data), {:id (:id new-client-data),
:name (:name new-client-data) :name (:name new-client-data)
@@ -94,7 +66,7 @@
(re-frame/reg-event-fx (re-frame/reg-event-fx
::save-new-client ::save-new-client
[(re-frame/path [::forms ::new-client])] [(forms/in-form ::new-client)]
(fn [{{new-client-data :data :as new-client-form} :db} _] (fn [{{new-client-data :data :as new-client-form} :db} _]
(let [new-client-req @(re-frame/subscribe [::new-client-request]) (let [new-client-req @(re-frame/subscribe [::new-client-request])
@@ -121,13 +93,13 @@
::save-complete ::save-complete
(fn [db [_ client]] (fn [db [_ client]]
(-> db (-> db
(stop-form ::new-client) (forms/stop-form ::new-client)
(assoc-in [:admin :adding-client?] false)
(assoc-in [:clients (:id (:edit-client client))] (update (:edit-client client) :bank-accounts (fn [bas] (->> bas (sort-by :sort-order) vec))))))) (assoc-in [:clients (:id (:edit-client client))] (update (:edit-client client) :bank-accounts (fn [bas] (->> bas (sort-by :sort-order) vec)))))))
(re-frame/reg-event-db (re-frame/reg-event-db
::save-error ::save-error
[(re-frame/path [::forms ::new-client])] [(forms/in-form ::new-client)]
(fn [db [_ result]] (fn [db [_ result]]
(-> db (-> db
(assoc :status :error) (assoc :status :error)
@@ -135,13 +107,13 @@
(re-frame/reg-event-db (re-frame/reg-event-db
::change-new ::change-new
[(re-frame/path [::forms ::new-client :data])] [(forms/in-form ::new-client) (re-frame/path [:data])]
(fn [db [_ path value]] (fn [client [_ path value]]
(assoc-in db path value))) (assoc-in client path value)))
(re-frame/reg-event-db (re-frame/reg-event-db
::add-new-location ::add-new-location
[(re-frame/path [::forms ::new-client :data])] [(forms/in-form ::new-client) (re-frame/path [:data])]
(fn [client _] (fn [client _]
(-> client (-> client
(update :locations conj (:location client)) (update :locations conj (:location client))
@@ -149,41 +121,25 @@
(re-frame/reg-event-db (re-frame/reg-event-db
::add-new-bank-account ::add-new-bank-account
[(re-frame/path [::forms ::new-client :data])] [(forms/in-form ::new-client) (re-frame/path [:data])]
(fn [client [_ type]] (fn [client [_ type]]
(update client :bank-accounts conj {:type type :active? true :new? true :visible true :sort-order (count (:bank-accounts client))}))) (update client :bank-accounts conj {:type type :active? true :new? true :visible true :sort-order (count (:bank-accounts client))})))
(re-frame/reg-event-db (re-frame/reg-event-db
::bank-account-activated ::bank-account-activated
[(re-frame/path [::forms ::new-client :data :bank-accounts])] [(forms/in-form ::new-client) (re-frame/path [:data :bank-accounts])]
(fn [bank-accounts [_ index]] (fn [bank-accounts [_ index]]
(update bank-accounts index assoc :active? true))) (update bank-accounts index assoc :active? true)))
(re-frame/reg-event-db (re-frame/reg-event-db
::bank-account-deactivated ::bank-account-deactivated
[(re-frame/path [::forms ::new-client :data :bank-accounts])] [(forms/in-form ::new-client) (re-frame/path [:data :bank-accounts])]
(fn [bank-accounts [_ index]] (fn [bank-accounts [_ index]]
(update bank-accounts index assoc :active? false))) (update bank-accounts index assoc :active? false)))
(re-frame/reg-event-db
::save-new-bank-account
[(re-frame/path [::forms ::new-client :data])]
(fn [{:keys [new-account] :as client} _]
(let [new-account (-> new-account
(update :check-number #(if (seq %) (js/parseInt %) nil))
(update :yodlee-account-id #(if (seq %) (js/parseInt %) nil)))]
(if ((->> client (:bank-accounts)
(map :code)
set) (:code new-account))
client
(-> client
(update :bank-accounts conj new-account )
(assoc :new-account {:type :check}))))))
(re-frame/reg-event-db (re-frame/reg-event-db
::bank-account-removed ::bank-account-removed
[(re-frame/path [::forms ::new-client :data :bank-accounts ])] [(forms/in-form ::new-client) (re-frame/path [:data :bank-accounts])]
(fn [bank-accounts [_ index]] (fn [bank-accounts [_ index]]
(vec (concat (take index bank-accounts) (vec (concat (take index bank-accounts)
(drop (inc index) bank-accounts))) (drop (inc index) bank-accounts)))
@@ -193,7 +149,7 @@
(re-frame/reg-event-db (re-frame/reg-event-db
::sort-swapped ::sort-swapped
[(re-frame/path [::forms ::new-client :data :bank-accounts ])] [(forms/in-form ::new-client) (re-frame/path [:data :bank-accounts])]
(fn [bank-accounts [_ source dest]] (fn [bank-accounts [_ source dest]]
(->> (-> bank-accounts (->> (-> bank-accounts
(assoc-in [source :sort-order] (get-in bank-accounts [dest :sort-order])) (assoc-in [source :sort-order] (get-in bank-accounts [dest :sort-order]))
@@ -205,7 +161,7 @@
(re-frame/reg-event-db (re-frame/reg-event-db
::toggle-visible ::toggle-visible
[(re-frame/path [::forms ::new-client :data :bank-accounts ])] [(forms/in-form ::new-client) (re-frame/path [:data :bank-accounts])]
(fn [bank-accounts [_ account]] (fn [bank-accounts [_ account]]
(-> (->> bank-accounts (-> (->> bank-accounts
(sort-by :sort-order) (sort-by :sort-order)
@@ -245,7 +201,7 @@
(defn side-bar-form [_ children] (defn side-bar-form [_ children]
[:div [:a.delete.is-pulled-right {:on-click (dispatch-event [::form-closing ::new-client])}] [:div children]]) [:div [:a.delete.is-pulled-right {:on-click (dispatch-event [::forms/form-closing ::new-client])}] [:div children]])
(defn bank-account-card [new-client {:keys [active? new? type visible code name number check-number id sort-order] :as bank-account} first? last?] (defn bank-account-card [new-client {:keys [active? new? type visible code name number check-number id sort-order] :as bank-account} first? last?]
@@ -373,7 +329,7 @@
) )
(defn new-client-form [] (defn new-client-form []
(let [{error :error new-client :data } @(re-frame/subscribe [::form ::new-client])] (let [{error :error new-client :data } @(re-frame/subscribe [::forms/form ::new-client])]
[side-bar-form {} [side-bar-form {}
[:form [:form
@@ -455,11 +411,11 @@
"" ""
"disabled") "disabled")
:on-click (dispatch-event [::save-new-client]) :on-click (dispatch-event [::save-new-client])
:class (str @(re-frame/subscribe [::loading-class ::new-client]) (when error " animated shake"))} "Save"]]])) :class (str @(re-frame/subscribe [::forms/loading-class ::new-client]) (when error " animated shake"))} "Save"]]]))
(defn admin-clients-page [] (defn admin-clients-page []
(let [{:keys [adding-client?]} @(re-frame/subscribe [::subs/admin])] (let [{:keys [active?]} @(re-frame/subscribe [::forms/form ::new-client])]
[side-bar-layout {:side-bar [admin-side-bar {}] [side-bar-layout {:side-bar [admin-side-bar {}]
:main [admin-clients-content] :main [admin-clients-content]
:right-side-bar-visible? adding-client? :right-side-bar [appearing-side-bar {:visible? active?} [new-client-form]] }]))
:right-side-bar [new-client-form]}]))