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)
(if (and (not= :login handler) (not (:user db)))
{: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
:query-params params)})))
:query-params params
:auto-ap.forms/forms nil)})))
(re-frame/reg-event-db
::imported-invoices

View File

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

View File

@@ -7,10 +7,11 @@
[clojure.spec.alpha :as s]
[clojure.string :as str]
[auto-ap.subs :as subs]
[auto-ap.forms :as forms]
[auto-ap.events :as events]
[auto-ap.entities.clients :as entity]
[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.utils :refer [login-url dispatch-event dispatch-value-change bind-field horizontal-field]]
[auto-ap.views.components.modal :refer [action-modal]]
@@ -18,39 +19,11 @@
[auto-ap.routes :as routes]
[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
::new
(fn [db [_ client-id]]
(-> db
(assoc-in [:admin :adding-client?] true)
(start-form ::new-client (assoc {} :new-account {:type :check})))))
(forms/start-form ::new-client {}))))
@@ -58,12 +31,11 @@
::edit-client-clicked
(fn [{:keys [db]} [_ client-id]]
{:db (-> db
(start-form ::new-client (assoc (get (:clients db) client-id) :new-account {:type :check}))
(assoc-in [:admin :adding-client? ] true))}))
(forms/start-form ::new-client (get (:clients db) client-id)))}))
(re-frame/reg-sub
::new-client-request
:<- [::form ::new-client]
:<- [::forms/form ::new-client]
(fn [{new-client-data :data} _]
{:id (:id new-client-data),
:name (:name new-client-data)
@@ -94,7 +66,7 @@
(re-frame/reg-event-fx
::save-new-client
[(re-frame/path [::forms ::new-client])]
[(forms/in-form ::new-client)]
(fn [{{new-client-data :data :as new-client-form} :db} _]
(let [new-client-req @(re-frame/subscribe [::new-client-request])
@@ -121,13 +93,13 @@
::save-complete
(fn [db [_ client]]
(-> db
(stop-form ::new-client)
(assoc-in [:admin :adding-client?] false)
(forms/stop-form ::new-client)
(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
::save-error
[(re-frame/path [::forms ::new-client])]
[(forms/in-form ::new-client)]
(fn [db [_ result]]
(-> db
(assoc :status :error)
@@ -135,13 +107,13 @@
(re-frame/reg-event-db
::change-new
[(re-frame/path [::forms ::new-client :data])]
(fn [db [_ path value]]
(assoc-in db path value)))
[(forms/in-form ::new-client) (re-frame/path [:data])]
(fn [client [_ path value]]
(assoc-in client path value)))
(re-frame/reg-event-db
::add-new-location
[(re-frame/path [::forms ::new-client :data])]
[(forms/in-form ::new-client) (re-frame/path [:data])]
(fn [client _]
(-> client
(update :locations conj (:location client))
@@ -149,41 +121,25 @@
(re-frame/reg-event-db
::add-new-bank-account
[(re-frame/path [::forms ::new-client :data])]
[(forms/in-form ::new-client) (re-frame/path [:data])]
(fn [client [_ type]]
(update client :bank-accounts conj {:type type :active? true :new? true :visible true :sort-order (count (:bank-accounts client))})))
(re-frame/reg-event-db
::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]]
(update bank-accounts index assoc :active? true)))
(re-frame/reg-event-db
::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]]
(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
::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]]
(vec (concat (take index bank-accounts)
(drop (inc index) bank-accounts)))
@@ -193,7 +149,7 @@
(re-frame/reg-event-db
::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]]
(->> (-> bank-accounts
(assoc-in [source :sort-order] (get-in bank-accounts [dest :sort-order]))
@@ -205,7 +161,7 @@
(re-frame/reg-event-db
::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]]
(-> (->> bank-accounts
(sort-by :sort-order)
@@ -245,7 +201,7 @@
(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?]
@@ -373,7 +329,7 @@
)
(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 {}
[:form
@@ -455,11 +411,11 @@
""
"disabled")
: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 []
(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 {}]
:main [admin-clients-content]
:right-side-bar-visible? adding-client?
:right-side-bar [new-client-form]}]))
:right-side-bar [appearing-side-bar {:visible? active?} [new-client-form]] }]))