passing around params
This commit is contained in:
@@ -27,46 +27,55 @@
|
|||||||
(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))))))
|
||||||
|
|
||||||
|
(defmulti submit-query (fn [_ [_ command]]
|
||||||
|
command))
|
||||||
|
|
||||||
|
(defmethod submit-query :create [db]
|
||||||
|
(let [{:keys [data] {:keys [id invoice-number date location total expense-accounts vendor-id client-id]} :data} @(re-frame/subscribe [::forms/form ::form])]
|
||||||
|
{: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]}]}))
|
||||||
|
|
||||||
|
(defmethod submit-query :edit [db]
|
||||||
|
(let [{:keys [data] {:keys [id invoice-number date location total expense-accounts vendor-id client-id]} :data} @(re-frame/subscribe [::forms/form ::form])]
|
||||||
|
{: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]}]}))
|
||||||
|
|
||||||
|
(defmethod submit-query :add-and-print [db [_ _ 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])]
|
||||||
|
{: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]]]}]}))
|
||||||
|
|
||||||
(re-frame/reg-sub
|
(re-frame/reg-sub
|
||||||
::submit-query
|
::submit-query
|
||||||
(fn [db [_ command bank-account-id type]]
|
submit-query)
|
||||||
(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
|
||||||
@@ -105,44 +114,42 @@
|
|||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
::submitted
|
::submitted
|
||||||
(fn [{:keys [db]} [_ invoice-created invoice-printed command bank-account-id type]]
|
(fn [{:keys [db]} [_ params 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 ::form])]
|
(let [{:keys [data]} @(re-frame/subscribe [::forms/form ::form])]
|
||||||
{:db (forms/loading db ::form)
|
{:db (forms/loading db ::form)
|
||||||
:graphql
|
:graphql
|
||||||
{:token (-> db :user)
|
{:token (-> db :user)
|
||||||
:query-obj @(re-frame/subscribe [::submit-query command bank-account-id type])
|
:query-obj @(re-frame/subscribe [::submit-query command bank-account-id type])
|
||||||
:on-success [::succeeded command invoice-created invoice-printed]
|
:on-success [::succeeded params command]
|
||||||
:on-error [::forms/save-error ::form]}}))))
|
:on-error [::forms/save-error ::form]}}))))
|
||||||
|
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
::succeeded
|
::succeeded
|
||||||
(fn [{:keys [db]} [_ command invoice-created invoice-printed result]]
|
(fn [{:keys [db]} [_ {:keys [invoice-created invoice-printed]} command result]]
|
||||||
(let [invoice (condp = command
|
(let [invoice (condp = command
|
||||||
:edit (:edit-invoice result)
|
:edit (:edit-invoice result)
|
||||||
|
|
||||||
: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 (cond-> db
|
|
||||||
true (forms/stop-form ::form)
|
|
||||||
|
|
||||||
(#{:create :add-and-print} command) (forms/start-form ::form {:client-id (:id @(re-frame/subscribe [::subs/client]))
|
{:db (cond-> db
|
||||||
:status :unpaid
|
true (forms/stop-form ::form)
|
||||||
:date (date->str (c/now) standard)
|
|
||||||
:location (first (:locations @(re-frame/subscribe [::subs/client])))}))
|
|
||||||
events (cond-> [(conj invoice-created invoice)]
|
|
||||||
(= :add-and-print command) (conj (conj invoice-printed (:pdf-url (:add-and-print-invoice result)))))]
|
|
||||||
|
|
||||||
{:db db
|
(#{:create :add-and-print} command) (forms/start-form ::form {:client-id (:id @(re-frame/subscribe [::subs/client]))
|
||||||
:dispatch-n events})))
|
:status :unpaid
|
||||||
|
:date (date->str (c/now) standard)
|
||||||
|
:location (first (:locations @(re-frame/subscribe [::subs/client])))}))
|
||||||
|
:dispatch-n (cond-> [(conj invoice-created invoice)]
|
||||||
|
(= :add-and-print command) (conj (conj invoice-printed (:pdf-url (:add-and-print-invoice result)))))})))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
;; VIEWS
|
;; VIEWS
|
||||||
|
|
||||||
(defn form [{:keys [can-change-amount? invoice-created invoice-printed]}]
|
(defn form [{:keys [can-change-amount?] :as params}]
|
||||||
[forms/side-bar-form {:form ::form }
|
[forms/side-bar-form {:form ::form }
|
||||||
(let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form])
|
(let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form])
|
||||||
exists? (:id data)
|
exists? (:id data)
|
||||||
@@ -164,8 +171,8 @@
|
|||||||
(.stopPropagation e)
|
(.stopPropagation e)
|
||||||
(.preventDefault e))
|
(.preventDefault e))
|
||||||
(if exists?
|
(if exists?
|
||||||
(re-frame/dispatch-sync [::submitted invoice-created nil :edit])
|
(re-frame/dispatch-sync [::submitted params :edit])
|
||||||
(re-frame/dispatch-sync [::submitted invoice-created nil :create])))}
|
(re-frame/dispatch-sync [::submitted params :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
|
||||||
@@ -303,10 +310,10 @@
|
|||||||
(list
|
(list
|
||||||
(for [{:keys [id number name type]} (->> (:bank-accounts current-client) (filter :visible) (sort-by :sort-order))]
|
(for [{:keys [id number name type]} (->> (:bank-accounts current-client) (filter :visible) (sort-by :sort-order))]
|
||||||
(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 [::submitted params id :cash])} "With cash"]
|
||||||
(list
|
(list
|
||||||
^{: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 "-check")} [:a.dropdown-item {:on-click (dispatch-event [::submitted params :add-and-print id :check])} "Print checks 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]))))]]])
|
^{:key (str id "-debit")} [:a.dropdown-item {:on-click (dispatch-event [::submitted params :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])
|
||||||
""
|
""
|
||||||
|
|||||||
@@ -703,7 +703,7 @@
|
|||||||
:component-will-mount #(re-frame/dispatch-sync [::params-change {:status status}]) }))
|
:component-will-mount #(re-frame/dispatch-sync [::params-change {:status status}]) }))
|
||||||
|
|
||||||
(defn unpaid-invoices-page [{:keys [status]}]
|
(defn unpaid-invoices-page [{:keys [status]}]
|
||||||
(let [{invoice-bar-active? :active?} @(re-frame/subscribe [::forms/form ::form/new-invoice])]
|
(let [{invoice-bar-active? :active?} @(re-frame/subscribe [::forms/form ::form/form])]
|
||||||
[side-bar-layout {:side-bar [invoices-side-bar {}
|
[side-bar-layout {:side-bar [invoices-side-bar {}
|
||||||
^{:key "extra-filter"}
|
^{:key "extra-filter"}
|
||||||
[:div
|
[:div
|
||||||
|
|||||||
Reference in New Issue
Block a user