first step of page abstraction.
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
[auto-ap.views.components.money-field :refer [money-field]]
|
||||
[auto-ap.views.components.typeahead :refer [typeahead-entity]]
|
||||
[auto-ap.views.pages.invoices.common :refer [invoice-read]]
|
||||
[auto-ap.status :as status]
|
||||
[auto-ap.views.utils :refer [date->str date-picker dispatch-event standard with-user]]
|
||||
[cljs-time.core :as c]
|
||||
[clojure.spec.alpha :as s]
|
||||
@@ -17,8 +18,6 @@
|
||||
[re-frame.core :as re-frame]))
|
||||
|
||||
;; SUBS
|
||||
|
||||
|
||||
(re-frame/reg-sub
|
||||
::can-submit
|
||||
:<- [::forms/form ::form]
|
||||
@@ -26,8 +25,7 @@
|
||||
(let [min-total (if (= (:total (:original data)) (:outstanding-balance (:original data)))
|
||||
nil
|
||||
(- (:total (:original data)) (:outstanding-balance (:original data))))]
|
||||
(and (not= :loading status)
|
||||
(s/valid? ::invoice/invoice data)
|
||||
(and (s/valid? ::invoice/invoice data)
|
||||
(or (not min-total) (>= (:total data) min-total))
|
||||
(or (not (:id data))
|
||||
(dollars= (js/parseFloat (:total data)) (reduce + 0 (map (fn [ea] (js/parseFloat (:amount ea))) (:expense-accounts data)))))))))
|
||||
@@ -107,6 +105,15 @@
|
||||
|
||||
;; EVENTS
|
||||
|
||||
(re-frame/reg-event-db
|
||||
::updated
|
||||
(fn [db [_ invoice command]]
|
||||
(-> db
|
||||
(forms/stop-form ::form )
|
||||
(forms/start-form ::form {:client @(re-frame/subscribe [::subs/client])
|
||||
:status :unpaid
|
||||
:date (date->str (c/now) standard)}))))
|
||||
|
||||
(re-frame/reg-event-db
|
||||
::adding
|
||||
(fn [db [_ new]]
|
||||
@@ -168,48 +175,41 @@
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::add-and-print
|
||||
[with-user (forms/triggers-loading ::form) (forms/in-form ::form)]
|
||||
[with-user (forms/in-form ::form)]
|
||||
(fn [{:keys [user] {:keys [data]} :db} [_ params bank-account-id type]]
|
||||
{:graphql
|
||||
{:token user
|
||||
:owns-state {:single ::form}
|
||||
:query-obj @(re-frame/subscribe [::add-and-print-query bank-account-id type])
|
||||
:on-success [::succeeded params :add-and-print]
|
||||
:on-success [::added-and-printed params]
|
||||
:on-error [::forms/save-error ::form]}}))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::saving
|
||||
[with-user (forms/triggers-loading ::form) (forms/in-form ::form)]
|
||||
[with-user (forms/in-form ::form)]
|
||||
(fn [{:keys [user] {:keys [data]} :db} [_ params]]
|
||||
(let [command (if (:id data)
|
||||
:edit
|
||||
:create)]
|
||||
{:graphql
|
||||
{:token user
|
||||
:owns-state {:single ::form}
|
||||
:query-obj (if (:id data)
|
||||
@(re-frame/subscribe [::edit-query])
|
||||
@(re-frame/subscribe [::create-query]))
|
||||
:on-success [::succeeded params command]
|
||||
:on-success (fn [result]
|
||||
[::updated (assoc (if (:id data)
|
||||
(:edit-invoice result)
|
||||
(:add-invoice result))
|
||||
:class "live-added")])
|
||||
:on-error [::forms/save-error ::form]}})))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::succeeded
|
||||
(fn [{:keys [db]} [_ {:keys [invoice-created invoice-printed]} command result]]
|
||||
(let [invoice (condp = command
|
||||
:edit (:edit-invoice result)
|
||||
|
||||
:create (:add-invoice result)
|
||||
|
||||
:add-and-print (first (:invoices (:add-and-print-invoice result))))]
|
||||
|
||||
|
||||
{:db (cond-> db
|
||||
(#{:create :add-and-print} command) (-> (forms/stop-form ::form )
|
||||
(forms/start-form ::form {:client @(re-frame/subscribe [::subs/client])
|
||||
:status :unpaid
|
||||
:date (date->str (c/now) standard)}))
|
||||
(= :edit command) (forms/stop-form ::form))
|
||||
:dispatch-n (cond-> [(conj invoice-created invoice)]
|
||||
(= :add-and-print command) (conj (conj invoice-printed (:pdf-url (:add-and-print-invoice result)))))})))
|
||||
::added-and-printed
|
||||
(fn [{:keys [db]} [_ {:keys [invoice-printed]} result]]
|
||||
(let [invoice (first (:invoices (:add-and-print-invoice result)))]
|
||||
{:dispatch-n [[::updated (assoc invoice :class "live-added")]
|
||||
(conj invoice-printed (:pdf-url (:add-and-print-invoice result)))]})))
|
||||
|
||||
|
||||
|
||||
@@ -224,6 +224,7 @@
|
||||
[layouts/side-bar {:on-close (dispatch-event [::forms/form-closing ::form ])}
|
||||
(let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form])
|
||||
{:keys [form-inline field raw-field error-notification submit-button ]} invoice-form
|
||||
status @(re-frame/subscribe [::status/single ::form])
|
||||
exists? (:id data)
|
||||
can-change-amount? (#{:unpaid ":unpaid"} (:status data))
|
||||
min-total (if (= (:total (:original data)) (:outstanding-balance (:original data)))
|
||||
@@ -319,13 +320,8 @@
|
||||
[drop-down {:header [:button.button.is-info.is-outlined.is-medium.is-fullwidth {:aria-haspopup true
|
||||
:type "button"
|
||||
:on-click (dispatch-event [::events/toggle-menu ::add-and-print-invoice ])
|
||||
:disabled (if @(re-frame/subscribe [::can-submit])
|
||||
""
|
||||
"disabled")
|
||||
|
||||
:class (if false
|
||||
"is-loading"
|
||||
"")}
|
||||
:disabled (status/disabled-for status)
|
||||
:class (status/class-for status)}
|
||||
"Save & Pay "
|
||||
[:span " "]
|
||||
[:span.icon.is-small [:i.fa.fa-angle-down {:aria-hidden "true"}]]]
|
||||
|
||||
Reference in New Issue
Block a user