From ad4054d820ae05b3551638cd9ac2e3e540ecbe9c Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Sat, 30 May 2020 12:28:35 -0700 Subject: [PATCH] simplified client form. --- src/clj/auto_ap/graphql.clj | 2 +- .../auto_ap/views/pages/admin/clients.cljs | 323 +++++++----------- 2 files changed, 127 insertions(+), 198 deletions(-) diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index cd9f323a..f3670b3c 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -904,7 +904,7 @@ (defn get-cash-flow [context {:keys [client_id]} value] (when client_id - (let [{:client/keys [weekly-credits weekly-debits ]} (doto (d/pull (d/db (d/connect uri)) '[*] client_id ) println) + (let [{:client/keys [weekly-credits weekly-debits ]} (d/pull (d/db (d/connect uri)) '[*] client_id ) total-cash (reduce (fn [total [credit debit]] (- (+ total credit) diff --git a/src/cljs/auto_ap/views/pages/admin/clients.cljs b/src/cljs/auto_ap/views/pages/admin/clients.cljs index c8559059..177ee6eb 100644 --- a/src/cljs/auto_ap/views/pages/admin/clients.cljs +++ b/src/cljs/auto_ap/views/pages/admin/clients.cljs @@ -11,7 +11,7 @@ [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 appearing-side-bar]] + [auto-ap.views.components.layouts :refer [side-bar-layout appearing-side-bar 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]] @@ -23,7 +23,7 @@ ::new (fn [db [_ client-id]] (-> db - (forms/start-form ::new-client {:bank-accounts []})))) + (forms/start-form ::form {:bank-accounts []})))) @@ -31,12 +31,19 @@ ::edit-client-clicked (fn [{:keys [db]} [_ client-id]] {:db (-> db - (forms/stop-form ::new-client) - (forms/start-form ::new-client (get (:clients db) client-id)))})) + (forms/stop-form ::form) + (forms/start-form ::form (get (:clients db) client-id)))})) + +(re-frame/reg-sub + ::can-submit + :<- [::forms/form ::form] + (fn [{:keys [data status]} _] + (s/valid? ::entity/client data))) + (re-frame/reg-sub ::new-client-request - :<- [::forms/form ::new-client] + :<- [::forms/form ::form] (fn [{new-client-data :data} _] {:id (:id new-client-data), :name (:name new-client-data) @@ -74,7 +81,7 @@ (re-frame/reg-event-fx ::save-new-client - [(forms/in-form ::new-client)] + [(forms/in-form ::form)] (fn [{{new-client-data :data :as new-client-form} :db} _] (let [new-client-req @(re-frame/subscribe [::new-client-request]) @@ -104,13 +111,13 @@ ::save-complete (fn [db [_ client]] (-> db - (forms/stop-form ::new-client) + (forms/stop-form ::form) (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 ::add-new-location - [(forms/in-form ::new-client) (re-frame/path [:data])] + [(forms/in-form ::form) (re-frame/path [:data])] (fn [client _] (-> client (update :locations conj (:location client)) @@ -118,7 +125,7 @@ (re-frame/reg-event-db ::add-location-to-bank-account - [(forms/in-form ::new-client) (re-frame/path [:data])] + [(forms/in-form ::form) (re-frame/path [:data])] (fn [client [_ which-account]] (println client which-account) (-> client @@ -127,7 +134,7 @@ (re-frame/reg-event-db ::add-new-match - [(forms/in-form ::new-client) (re-frame/path [:data])] + [(forms/in-form ::form) (re-frame/path [:data])] (fn [client _] (-> client (update :matches conj (:match client)) @@ -136,7 +143,7 @@ (re-frame/reg-event-db ::remove-match - [(forms/in-form ::new-client) (re-frame/path [:data])] + [(forms/in-form ::form) (re-frame/path [:data])] (fn [client [_ which]] (-> client (update :matches set) @@ -144,7 +151,7 @@ (re-frame/reg-event-db ::add-new-location-match - [(forms/in-form ::new-client) (re-frame/path [:data])] + [(forms/in-form ::form) (re-frame/path [:data])] (fn [client _] (-> client (update :location-matches conj (:location-match client)) @@ -152,7 +159,7 @@ (re-frame/reg-event-db ::remove-location-match - [(forms/in-form ::new-client) (re-frame/path [:data])] + [(forms/in-form ::form) (re-frame/path [:data])] (fn [client [_ i]] (-> client (update :location-matches (fn [lm] @@ -164,25 +171,25 @@ (re-frame/reg-event-db ::add-new-bank-account - [(forms/in-form ::new-client) (re-frame/path [:data])] + [(forms/in-form ::form) (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 - [(forms/in-form ::new-client) (re-frame/path [:data :bank-accounts])] + [(forms/in-form ::form) (re-frame/path [:data :bank-accounts])] (fn [bank-accounts [_ index]] (update (vec (sort-by :sort-order bank-accounts)) index assoc :active? true))) (re-frame/reg-event-db ::bank-account-deactivated - [(forms/in-form ::new-client) (re-frame/path [:data :bank-accounts])] + [(forms/in-form ::form) (re-frame/path [:data :bank-accounts])] (fn [bank-accounts [_ index]] (update (vec (sort-by :sort-order bank-accounts)) index assoc :active? false))) (re-frame/reg-event-db ::bank-account-removed - [(forms/in-form ::new-client) (re-frame/path [:data :bank-accounts])] + [(forms/in-form ::form) (re-frame/path [:data :bank-accounts])] (fn [bank-accounts [_ index]] (vec (concat (take index bank-accounts) (drop (inc index) bank-accounts))) @@ -192,7 +199,7 @@ (re-frame/reg-event-db ::sort-swapped - [(forms/in-form ::new-client) (re-frame/path [:data :bank-accounts])] + [(forms/in-form ::form) (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])) @@ -204,7 +211,7 @@ (re-frame/reg-event-db ::toggle-visible - [(forms/in-form ::new-client) (re-frame/path [:data :bank-accounts])] + [(forms/in-form ::form) (re-frame/path [:data :bank-accounts])] (fn [bank-accounts [_ account]] (-> (->> bank-accounts (sort-by :sort-order) @@ -242,9 +249,14 @@ [:a.button.is-primary.is-large {:on-click (dispatch-event [::new])} "New client"]] [clients-table]])]) +(def client-form + (forms/vertical-form {:can-submit [::can-submit] + :change-event [::forms/change ::form] + :submit-event [::save-new-client ] + :id ::form})) (defn bank-account-card [new-client {:keys [active? new? type visible code name number check-number id sort-order] :as bank-account} first? last?] - (let [change-event [::forms/change ::new-client]] + (let [{:keys [form field raw-field error-notification submit-button ]} client-form] [:div.card {:style {:margin-bottom "1em"}} [:header.card-header [:p.card-header-title {:style {:text-overflow "ellipsis"}} @@ -287,117 +299,76 @@ [:div.field.has-addons.is-extended [:p.control [:a.button.is-static (:code new-client) "-" ]] [:p.control - [bind-field + [raw-field [:input.input {:type "code" :field [:bank-accounts sort-order :code] - :spec ::entity/code - :event change-event - :subscription new-client}]]]] + :spec ::entity/code}]]]] [:div.field [:p.control code]])] - [:div.control - [:p.help "Nickname"] - [bind-field - [:input.input {:placeholder "BOA Checking #1" - :type "text" - :field [:bank-accounts sort-order :name] - :event change-event - :subscription new-client}]]]] + [field "Nickname" + [:input.input {:placeholder "BOA Checking #1" + :type "text" + :field [:bank-accounts sort-order :name]}]]] (when (#{:check ":check"} type ) [:div [:label.label "Bank"] [horizontal-field nil - [:div.control - [:p.help "Bank Name"] - [bind-field - [:input.input {:placeholder "Bank of America" - :type "text" - :field [:bank-accounts sort-order :bank-name] - :event change-event - :subscription new-client}]]] - [:div.control - [:p.help "Routing #"] - [bind-field - [:input.input {:placeholder "104819123" - :type "text" - :field [:bank-accounts sort-order :routing] - :event change-event - :subscription new-client}]]] - [:div.control - [:p.help "Bank code"] - [bind-field - [:input.input {:placeholder "12/10123" - :type "text" - :field [:bank-accounts sort-order :bank-code] - :event change-event - :subscription new-client}]]]] + [field "Bank Name" + [:input.input {:placeholder "Bank of America" + :type "text" + :field [:bank-accounts sort-order :bank-name]}]] + [field "Routing #" + [:input.input {:placeholder "104819123" + :type "text" + :field [:bank-accounts sort-order :routing]}]] + [field "Bank code" + [:input.input {:placeholder "12/10123" + :type "text" + :field [:bank-accounts sort-order :bank-code]}]]] [:label.label "Checking account"] [horizontal-field nil - [:div.control - [:p.help "Account #"] - [bind-field - [:input.input {:placeholder "123456789" - :type "text" - :field [:bank-accounts sort-order :number] - :event change-event - :subscription new-client}]]] - [:div.control - [:p.help "Check Number"] - [bind-field - [:input.input {:placeholder "10000" - :type "text" - :field [:bank-accounts sort-order :check-number] - :event change-event - :subscription new-client}]]]] - [:div.field - [:label.label "Yodlee Account"] - [:div.control - [bind-field - [:input.input {:placeholder "Yodlee Account #" - :type "text" - :field [:bank-accounts sort-order :yodlee-account-id] - :event change-event - :subscription new-client}]]]] + [field "Account #" + [:input.input {:placeholder "123456789" + :type "text" + :field [:bank-accounts sort-order :number]}]] + + [field "Check Number" + [:input.input {:placeholder "10000" + :type "text" + :field [:bank-accounts sort-order :check-number]}]]] + [field "Yodlee Account" + [:input.input {:placeholder "Yodlee Account #" + :type "text" + :field [:bank-accounts sort-order :yodlee-account-id]}]] ]) (when (#{:credit ":credit"} type ) [:div + [:label.label "Account"] [horizontal-field nil - [:div.control - [:p.help "Bank Name"] - [bind-field - [:input.input {:placeholder "Bank of America" - :type "text" - :field [:bank-accounts sort-order :bank-name] - :event change-event - :subscription new-client}]]]] + [field "Bank Name" + [:input.input {:placeholder "Bank of America" + :type "text" + :field [:bank-accounts sort-order :bank-name]}]]] [horizontal-field nil - [:div.control - [:p.help "Account #"] - [bind-field - [:input.input {:placeholder "123456789" - :type "text" - :field [:bank-accounts sort-order :number] - :event change-event - :subscription new-client}]]]] - [:div.field - [:p.help "Yodlee Account"] - [:div.control - [bind-field - [:input.input {:placeholder "Yodlee Account #" - :type "text" - :field [:bank-accounts sort-order :yodlee-account-id] - :event change-event - :subscription new-client}]]]]]) + [field "Account #" + [:input.input {:placeholder "123456789" + :type "text" + :field [:bank-accounts sort-order :number]}]]] + + [field "Yodlee Account" + [:input.input {:placeholder "Yodlee Account #" + :type "text" + :field [:bank-accounts sort-order :yodlee-account-id]}]]]) [:div.field [:label.label "Locations"] [:div.control @@ -405,14 +376,12 @@ [:div.field.has-addons [:p.control [:div.select - [bind-field + [raw-field [:select {:type "select" :style {:width "7em"} :field [:bank-accounts sort-order :location-select] :allow-nil? true - :spec (set (get-in new-client [:locations])) - :event change-event - :subscription new-client} + :spec (set (get-in new-client [:locations]))} (map (fn [l] ^{:key l} [:option {:value l} l]) (get-in new-client [:locations]))]]]] [:p.control {:on-click (dispatch-event [::add-location-to-bank-account sort-order]) } [:a.button "Add"]]]] @@ -423,13 +392,10 @@ [:i "This account applies to all locations"])] [:div.field [:label.checkbox - [bind-field + [raw-field [:input {:type "checkbox" - :field [:bank-accounts sort-order :include-in-reports] - :event change-event - :subscription new-client}]] - " Include in reports"] - ]]) + :field [:bank-accounts sort-order :include-in-reports]}]] + " Include in reports"]]]) (when active? [:footer.card-footer @@ -438,56 +404,46 @@ [:a.card-footer-item.is-warning {:href "#" :on-click (dispatch-event [::bank-account-removed sort-order])} "Remove"])])]) ) + + (defn new-client-form [] - (let [{error :error new-client :data } @(re-frame/subscribe [::forms/form ::new-client]) - change-event [::forms/change ::new-client]] + (let [{new-client :data } @(re-frame/subscribe [::forms/form ::form]) + {:keys [form field raw-field error-notification submit-button ]} client-form] - [forms/side-bar-form {:form ::new-client} - [:form - [:h1.title.is-2 "Add client"] - [:div.field - [:p.help "Name"] - [:div.control - [bind-field - [:input.input {:type "text" - :field :name - :spec ::entity/name - :event change-event - :subscription new-client}]]]] + [side-bar {:on-close (dispatch-event [::forms/form-closing ::form ])} + [form {:title "Add client"} + [field "Name" + [:input.input {:type "text" + :field [:name] + :spec ::entity/name + }]] [:div.field - [:p.help "Client code"] + [:p.help "Client code" + ] (if (:id new-client) [:div.control - (:code new-client) - ] + (:code new-client)] [:div.control - [bind-field + [raw-field [:input.input {:type "code" :field :code - :spec ::entity/code - :event change-event - :subscription new-client}]]])] + :spec ::entity/code}]]])] + + + [field "Email" + [:input.input {:type "email" + :field :email + :spec ::entity/email}]] - [:div.field - [:p.help "Email"] - [:div.control - [bind-field - [:input.input {:type "email" - :field :email - :spec ::entity/email - :event change-event - :subscription new-client}]]]] [:div.field [:p.help "Matches"] [:div.control [:div.field.has-addons [:p.control - [bind-field + [raw-field [:input.input {:type "text" - :field :match - :event change-event - :subscription new-client}]]] + :field :match}]]] [:p.control [:button.button.is-primary {:on-click (dispatch-event [::add-new-match])} "Add"]]]] [:ul (for [match (:matches new-client)] @@ -500,11 +456,9 @@ [:div.control [:div.field.has-addons [:p.control - [bind-field + [raw-field [:input.input {:type "text" - :field :location - :event change-event - :subscription new-client}]]] + :field :location}]]] [:p.control [:button.button.is-primary {:on-click (dispatch-event [::add-new-location])} "Add"]]] [:ul (for [location (:locations new-client)] @@ -517,19 +471,15 @@ [:p.control - [bind-field + [raw-field [:input.input {:type "text" :placeholder "San Jose" - :field [:location-match :match] - :event change-event - :subscription new-client}]]] + :field [:location-match :match]}]]] [:p.control - [bind-field + [raw-field [:input.input {:type "text" :placeholder "DT" - :field [:location-match :location] - :event change-event - :subscription new-client}]]] + :field [:location-match :location]}]]] [:p.control [:button.button.is-primary {:on-click (dispatch-event [::add-new-location-match])} "Add"]]] [:ul @@ -540,7 +490,7 @@ [:div {:style {:padding-bottom "0.75em" :padding-top "0.75em"}} [:h2.subtitle "Address"] [address-field {:field [:address] - :event change-event + :event [::forms/change ::form] :subscription new-client}]] [:h2.subtitle "Bank accounts"] @@ -556,44 +506,23 @@ [:div.column.is-third [:a.button.is-primary.is-outlined.is-fullwidth {:on-click (dispatch-event [::add-new-bank-account :cash])} "Add Cash Account"]]] - #_[:h2.subtitle "Add bank account"] - #_(when (:saving? new-client) [:div.is-overlay {:style {"backgroundColor" "rgba(150,150,150, 0.5)"}}]) - #_(println (s/explain-data ::entity/client new-client)) + [field "Weekly credits" + [:input.input {:type "number" + :placeholder "250.00" + :field [:weekly-credits] + :step "0.01"}]] + [field "Weekly debits" + [:input.input {:type "number" + :placeholder "250.00" + :field [:weekly-debits] + :step "0.01"}]] - [:div.field - [:p.help "Weekly credits"] - [:div.control - [bind-field - [:input.input {:type "number" - :placeholder "250.00" - :field [:weekly-credits] - :step "0.01" - :event change-event - :subscription new-client}]]]] - [:div.field - [:p.help "Weekly debits"] - [:div.control - [bind-field - [:input.input {:type "number" - :placeholder "250.00" - :field [:weekly-debits] - :step "0.01" - :event change-event - :subscription new-client}]]]] - - (when error - [:div.notification.is-warning.animated.fadeInUp - error]) - - [:submit.button.is-large.is-primary {:disabled (if (s/valid? ::entity/client @(re-frame/subscribe [::new-client-request])) - "" - "disabled") - :on-click (dispatch-event [::save-new-client]) - :class (str @(re-frame/subscribe [::forms/loading-class ::new-client]) (when error " animated shake"))} "Save"]]])) + [error-notification] + [submit-button "Save"]]])) (defn admin-clients-page [] - (let [{:keys [active?]} @(re-frame/subscribe [::forms/form ::new-client])] + (let [{:keys [active?]} @(re-frame/subscribe [::forms/form ::form])] [side-bar-layout {:side-bar [admin-side-bar {}] :main [admin-clients-content] :right-side-bar [appearing-side-bar {:visible? active?} [new-client-form]] }]))