minor refactoriing.

This commit is contained in:
Bryce Covert
2019-04-16 13:25:24 -07:00
parent 3fd9a1c964
commit 917ec33e40

View File

@@ -14,12 +14,10 @@
[re-frame.core :as re-frame])) [re-frame.core :as re-frame]))
;; SUBS ;; SUBS
(re-frame/reg-sub (re-frame/reg-sub
::can-submit-edit-invoice ::can-submit-edit-invoice
:<- [::forms/form ::new-invoice] :<- [::forms/form ::form]
(fn [{:keys [data status]} _] (fn [{:keys [data status]} _]
(let [min-total (if (= (:total (:original data)) (:outstanding-balance (:original data))) (let [min-total (if (= (:total (:original data)) (:outstanding-balance (:original data)))
nil nil
(- (:total (:original data)) (:outstanding-balance (:original data))))] (- (:total (:original data)) (:outstanding-balance (:original data))))]
@@ -29,11 +27,52 @@
(or (not (:id data)) (or (not (:id data))
(< (.abs js/Math (- (js/parseFloat (:total data)) (reduce + 0 (map (fn [ea] (js/parseFloat (:amount ea))) (:expense-accounts data))))) 0.001)))))) (< (.abs js/Math (- (js/parseFloat (:total data)) (reduce + 0 (map (fn [ea] (js/parseFloat (:amount ea))) (:expense-accounts data))))) 0.001))))))
(re-frame/reg-sub
::submit-query
(fn [db [_ command bank-account-id type]]
(let [{:keys [data] {:keys [id invoice-number date location total expense-accounts vendor-id client-id]} :data} @(re-frame/subscribe [::forms/form ::form])]
(condp = command
:create {:venia/operation {:operation/type :mutation
:operation/name "AddInvoice"}
:venia/queries [{:query/data [:add-invoice
{:invoice {:date date
:vendor-id vendor-id
:client-id client-id
:invoice-number invoice-number
:location location
:total total
}}
invoice-read]}]}
:edit {:venia/operation {:operation/type :mutation
:operation/name "EditInvoice"}
:venia/queries [{:query/data [:edit-invoice
{:invoice {:id id
:invoice-number invoice-number
:date date
:total total
:expense-accounts (map (fn [ea]
{:id (:id ea)
:amount (:amount ea)})
expense-accounts)}}
invoice-read]}]}
:add-and-print {:venia/operation {:operation/type :mutation
:operation/name "AddAndPrintInvoice"}
:venia/queries [{:query/data [:add-and-print-invoice
{:invoice {:date date
:vendor-id vendor-id
:client-id client-id
:invoice-number invoice-number
:location location
:total total}
:bank-account-id bank-account-id
:type type}
[:pdf-url [:invoices invoice-read]]]}]}))))
;; EVENTS ;; EVENTS
(re-frame/reg-event-db (re-frame/reg-event-db
::adding ::adding
(fn [db [_ new]] (fn [db [_ new]]
(-> db (forms/start-form ::new-invoice new)))) (-> db (forms/start-form ::form new))))
(re-frame/reg-event-db (re-frame/reg-event-db
::editing ::editing
@@ -41,7 +80,7 @@
(let [edit-invoice (update which :date #(date->str % standard)) (let [edit-invoice (update which :date #(date->str % standard))
edit-invoice (assoc edit-invoice :original edit-invoice)] edit-invoice (assoc edit-invoice :original edit-invoice)]
(-> db (-> db
(forms/start-form ::new-invoice {:id (:id edit-invoice) (forms/start-form ::form {:id (:id edit-invoice)
:status (:status edit-invoice) :status (:status edit-invoice)
:date (:date edit-invoice) :date (:date edit-invoice)
:invoice-number (:invoice-number edit-invoice) :invoice-number (:invoice-number edit-invoice)
@@ -60,75 +99,21 @@
(let [first-location (-> @(re-frame/subscribe [::subs/clients-by-id]) (let [first-location (-> @(re-frame/subscribe [::subs/clients-by-id])
(get-in [value :locations]) (get-in [value :locations])
first)] first)]
{:dispatch [::forms/change ::new-invoice {:dispatch [::forms/change ::form
[:client-id] value [:client-id] value
[:location] first-location]}))) [:location] first-location]})))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::create-submitted ::submitted
(fn [{:keys [db]} [_ invoice-created]] (fn [{:keys [db]} [_ invoice-created invoice-printed command bank-account-id type]]
(when @(re-frame/subscribe [::can-submit-edit-invoice]) (when @(re-frame/subscribe [::can-submit-edit-invoice])
(let [{:keys [data]} @(re-frame/subscribe [::forms/form ::new-invoice])] (let [{:keys [data]} @(re-frame/subscribe [::forms/form ::form])]
{:db (forms/loading db ::new-invoice) {:db (forms/loading db ::form)
:graphql :graphql
{:token (-> db :user) {:token (-> db :user)
:query-obj {:venia/operation {:operation/type :mutation :query-obj @(re-frame/subscribe [::submit-query command bank-account-id type])
:operation/name "AddInvoice"} :on-success [::succeeded command invoice-created invoice-printed]
:venia/queries [{:query/data [:add-invoice :on-error [::forms/save-error ::form]}}))))
{:invoice {:date (:date data)
:vendor-id (:vendor-id data)
:client-id (:client-id data)
:invoice-number (:invoice-number data)
:location (:location data)
:total (:total data)
}}
invoice-read]}]}
:on-success [::succeeded :create invoice-created nil]
:on-error [::forms/save-error ::new-invoice]}}))))
(re-frame/reg-event-fx
::update-submitted
(fn [{:keys [db]} [_ invoice-created]]
(when @(re-frame/subscribe [::can-submit-edit-invoice])
(let [{{:keys [date total invoice-number id expense-accounts]} :data} @(re-frame/subscribe [::forms/form ::new-invoice])]
{:db (-> db
(assoc-in [::forms/forms ::new-invoice :status] :loading)
(assoc-in [::forms/forms ::new-invoice :error] nil))
:graphql
{:token (-> db :user)
:query-obj {:venia/operation {:operation/type :mutation
:operation/name "EditInvoice"}
:venia/queries [{:query/data [:edit-invoice
{:invoice {:id id :invoice-number invoice-number :date date :total total :expense-accounts (map (fn [ea]
{:id (:id ea)
:amount (:amount ea)})
expense-accounts)}}
invoice-read]}]}
:on-success [::succeeded :edit invoice-created nil]
:on-error [::forms/save-error ::new-invoice]}}))))
(re-frame/reg-event-fx
::add-and-print-submitted
(fn [{:keys [db]} [_ invoice-created invoice-printed bank-account-id type ]]
(when @(re-frame/subscribe [::can-submit-edit-invoice])
(let [{:keys [data]} @(re-frame/subscribe [::forms/form ::new-invoice])]
{:db (forms/loading db ::new-invoice)
:graphql
{:token (-> db :user)
:query-obj {:venia/operation {:operation/type :mutation
:operation/name "AddAndPrintInvoice"}
:venia/queries [{:query/data [:add-and-print-invoice
{:invoice {:date (:date data)
:vendor-id (:vendor-id data)
:client-id (:client-id data)
:invoice-number (:invoice-number data)
:location (:location data)
:total (:total data)}
:bank-account-id bank-account-id
:type type}
[:pdf-url [:invoices invoice-read]]]}]}
:on-success [::succeeded :add-and-print invoice-created invoice-printed]
:on-error [::forms/save-error ::new-invoice]}}))))
(re-frame/reg-event-fx (re-frame/reg-event-fx
@@ -140,53 +125,30 @@
:create (:add-invoice result) :create (:add-invoice result)
:add-and-print (first (:invoices (:add-and-print-invoice result)))) :add-and-print (first (:invoices (:add-and-print-invoice result))))
db (condp = command db (cond-> db
:edit (-> db (forms/stop-form ::new-invoice)) true (forms/stop-form ::form)
:create (-> db
(forms/stop-form ::new-invoice) (#{:create :add-and-print} command) (forms/start-form ::form {:client-id (:id @(re-frame/subscribe [::subs/client]))
(forms/start-form ::new-invoice {:client-id (:id @(re-frame/subscribe [::subs/client]))
:status :unpaid :status :unpaid
:date (date->str (c/now) standard) :date (date->str (c/now) standard)
:location (first (:locations @(re-frame/subscribe [::subs/client])))})) :location (first (:locations @(re-frame/subscribe [::subs/client])))}))
:add-and-print (-> db
(forms/stop-form ::new-invoice)
(forms/start-form ::new-invoice {:client-id (:id @(re-frame/subscribe [::subs/client]))
:status :unpaid
:date (date->str (c/now) standard)
:location (first (:locations @(re-frame/subscribe [::subs/client])))})))
events (cond-> [(conj invoice-created invoice)] events (cond-> [(conj invoice-created invoice)]
(= :add-and-print command) (conj (conj invoice-printed (:pdf-url (:add-and-print-invoice result)))))] (= :add-and-print command) (conj (conj invoice-printed (:pdf-url (:add-and-print-invoice result)))))]
{:db db {:db db
:dispatch-n events}))) :dispatch-n events})))
(re-frame/reg-event-fx
::invoice-created-and-printed
(fn [{:keys [db]} [_ {:keys [add-and-print-invoice]}]]
{:db (-> db
(forms/stop-form ::new-invoice)
(forms/start-form ::new-invoice {:client-id (:id @(re-frame/subscribe [::subs/client]))
:status :unpaid
:date (date->str (c/now) standard)
:location (first (:locations @(re-frame/subscribe [::subs/client])))})
(update-in [::invoice-page :invoices]
(fn [is]
(into (vec (map #(assoc % :class "live-added")
(:invoices add-and-print-invoice)))
is)))
(assoc-in [::check-results :shown?] true)
(assoc-in [::check-results :pdf-url] (:pdf-url add-and-print-invoice)))}))
;; VIEWS ;; VIEWS
(defn form [{:keys [can-change-amount? invoice-created invoice-printed]}] (defn form [{:keys [can-change-amount? invoice-created invoice-printed]}]
[forms/side-bar-form {:form ::new-invoice } [forms/side-bar-form {:form ::form }
(let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::new-invoice]) (let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form])
exists? (:id data) exists? (:id data)
current-client @(re-frame/subscribe [::subs/client]) current-client @(re-frame/subscribe [::subs/client])
can-change-amount? (#{:unpaid ":unpaid"} (:status data)) can-change-amount? (#{:unpaid ":unpaid"} (:status data))
change-event [::forms/change ::new-invoice] change-event [::forms/change ::form]
locations (get-in @(re-frame/subscribe [::subs/clients-by-id]) [(:client-id data) :locations]) locations (get-in @(re-frame/subscribe [::subs/clients-by-id]) [(:client-id data) :locations])
multi-location? (> (count locations) 1) multi-location? (> (count locations) 1)
@@ -202,8 +164,8 @@
(.stopPropagation e) (.stopPropagation e)
(.preventDefault e)) (.preventDefault e))
(if exists? (if exists?
(re-frame/dispatch-sync [::update-submitted invoice-created]) (re-frame/dispatch-sync [::submitted invoice-created nil :edit])
(re-frame/dispatch-sync [::create-submitted invoice-created])))} (re-frame/dispatch-sync [::submitted invoice-created nil :create])))}
[:h1.title.is-2 "New Invoice"] [:h1.title.is-2 "New Invoice"]
(when-not @(re-frame/subscribe [::subs/client]) (when-not @(re-frame/subscribe [::subs/client])
[:div.field [:div.field
@@ -215,7 +177,7 @@
:auto-focus (if @(re-frame/subscribe [::subs/client]) false true) :auto-focus (if @(re-frame/subscribe [::subs/client]) false true)
:field [:client-id] :field [:client-id]
:disabled exists? :disabled exists?
:event [::change-new-invoice-client [::new-invoice]] :event [::change-new-invoice-client [::form]]
:spec ::invoice/client-id :spec ::invoice/client-id
:subscription data}]]]]) :subscription data}]]]])
@@ -257,22 +219,11 @@
:next-month-button-label "" :next-month-button-label ""
:next-month-label "" :next-month-label ""
:type "date" :type "date"
:field [:date]
:event change-event
:spec ::invoice/date
:subscription data}]
#_[:input.input {:type "date"
:field [:date] :field [:date]
:event change-event :event change-event
:spec ::invoice/date :spec ::invoice/date
:subscription data}]]]] :subscription data}]]]]
#_[horizontal-field
[:label.label "Date 2 "]
]
[:div.field [:div.field
[:p.help "Invoice #"] [:p.help "Invoice #"]
[:div.control [:div.control
@@ -320,7 +271,7 @@
[:input.input {:type "number" [:input.input {:type "number"
:field [:expense-accounts index :amount] :field [:expense-accounts index :amount]
:style {:text-align "right"} :style {:text-align "right"}
:event [::forms/change ::new-invoice] :event [::forms/change ::form]
:subscription data :subscription data
:value (get-in expense-account [:amount]) :value (get-in expense-account [:amount])
:max (:total data) :max (:total data)
@@ -354,11 +305,11 @@
(if (= :cash type) (if (= :cash type)
^{:key id} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print-invoice invoice-created invoice-printed id :cash])} "With cash"] ^{:key id} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print-invoice invoice-created invoice-printed id :cash])} "With cash"]
(list (list
^{:key (str id "-check")} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print-submitted invoice-created invoice-printed id :check])} "Print checks from " name] ^{:key (str id "-check")} [:a.dropdown-item {:on-click (dispatch-event [::submitted invoice-created invoice-printed :add-and-print id :check])} "Print checks from " name]
^{:key (str id "-debit")} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print-submitted invoice-created invoice-printed id :debit])} "Debit from " name]))))]]]) ^{:key (str id "-debit")} [:a.dropdown-item {:on-click (dispatch-event [::submitted invoice-created invoice-printed :add-and-print id :debit])} "Debit from " name]))))]]])
[:div.column [:div.column
[:button.button.is-medium.is-primary.is-fullwidth {:disabled (if @(re-frame/subscribe [::can-submit-edit-invoice]) [:button.button.is-medium.is-primary.is-fullwidth {:disabled (if @(re-frame/subscribe [::can-submit-edit-invoice])
"" ""
"disabled") "disabled")
:class (str @(re-frame/subscribe [::forms/loading-class ::new-invoice]) :class (str @(re-frame/subscribe [::forms/loading-class ::form])
(when error " animated shake"))} "Save"]] ]])]) (when error " animated shake"))} "Save"]] ]])])