splitting out form for refactoring.

This commit is contained in:
Bryce Covert
2019-04-16 11:22:19 -07:00
parent d3dce3cd22
commit 3fd9a1c964
4 changed files with 408 additions and 361 deletions

View File

@@ -0,0 +1,364 @@
(ns auto-ap.views.pages.invoices.form
(:require [auto-ap.entities.invoice :as invoice]
[auto-ap.events :as events]
[auto-ap.forms :as forms]
[auto-ap.subs :as subs]
[auto-ap.views.components.dropdown :refer [drop-down]]
[auto-ap.views.components.typeahead :refer [typeahead]]
[auto-ap.views.pages.invoices.common :refer [invoice-read]]
[auto-ap.views.utils
:refer
[bind-field date->str date-picker dispatch-event standard]]
[cljs-time.core :as c]
[clojure.spec.alpha :as s]
[re-frame.core :as re-frame]))
;; SUBS
(re-frame/reg-sub
::can-submit-edit-invoice
:<- [::forms/form ::new-invoice]
(fn [{:keys [data status]} _]
(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)
(or (not min-total) (>= (:total data) min-total))
(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))))))
;; EVENTS
(re-frame/reg-event-db
::adding
(fn [db [_ new]]
(-> db (forms/start-form ::new-invoice new))))
(re-frame/reg-event-db
::editing
(fn [db [_ which]]
(let [edit-invoice (update which :date #(date->str % standard))
edit-invoice (assoc edit-invoice :original edit-invoice)]
(-> db
(forms/start-form ::new-invoice {:id (:id edit-invoice)
:status (:status edit-invoice)
:date (:date edit-invoice)
:invoice-number (:invoice-number edit-invoice)
:total (:total edit-invoice)
:original edit-invoice
:vendor-id (:id (:vendor edit-invoice))
:vendor-name (:name (:vendor edit-invoice))
:client-id (:id (:client edit-invoice))
:expense-accounts (:expense-accounts edit-invoice)
:client-name (:name (:client edit-invoice))})))))
(re-frame/reg-event-fx
::change-new-invoice-client
(fn [{:keys [db ]} [_ location field value]]
(let [first-location (-> @(re-frame/subscribe [::subs/clients-by-id])
(get-in [value :locations])
first)]
{:dispatch [::forms/change ::new-invoice
[:client-id] value
[:location] first-location]})))
(re-frame/reg-event-fx
::create-submitted
(fn [{:keys [db]} [_ invoice-created]]
(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 "AddInvoice"}
:venia/queries [{:query/data [:add-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)
}}
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
::succeeded
(fn [{:keys [db]} [_ command invoice-created invoice-printed result]]
(let [invoice (condp = command
:edit (:edit-invoice result)
:create (:add-invoice result)
:add-and-print (first (:invoices (:add-and-print-invoice result))))
db (condp = command
:edit (-> db (forms/stop-form ::new-invoice))
:create (-> 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])))}))
: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)]
(= :add-and-print command) (conj (conj invoice-printed (:pdf-url (:add-and-print-invoice result)))))]
{:db db
: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
(defn form [{:keys [can-change-amount? invoice-created invoice-printed]}]
[forms/side-bar-form {:form ::new-invoice }
(let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::new-invoice])
exists? (:id data)
current-client @(re-frame/subscribe [::subs/client])
can-change-amount? (#{:unpaid ":unpaid"} (:status data))
change-event [::forms/change ::new-invoice]
locations (get-in @(re-frame/subscribe [::subs/clients-by-id]) [(:client-id data) :locations])
multi-location? (> (count locations) 1)
min-total (if (= (:total (:original data)) (:outstanding-balance (:original data)))
nil
(- (:total (:original data)) (:outstanding-balance (:original data))))
should-select-location? (and locations
(> (count locations) 1))
chooseable-expense-accounts @(re-frame/subscribe [::subs/chooseable-expense-accounts])]
^{:key id}
[:form { :on-submit (fn [e]
(when (.-stopPropagation e)
(.stopPropagation e)
(.preventDefault e))
(if exists?
(re-frame/dispatch-sync [::update-submitted invoice-created])
(re-frame/dispatch-sync [::create-submitted invoice-created])))}
[:h1.title.is-2 "New Invoice"]
(when-not @(re-frame/subscribe [::subs/client])
[:div.field
[:p.help "Client"]
[:div.control
[bind-field
[typeahead {:matches (map (fn [x] [(:id x) (:name x)]) @(re-frame/subscribe [::subs/clients]))
:type "typeahead"
:auto-focus (if @(re-frame/subscribe [::subs/client]) false true)
:field [:client-id]
:disabled exists?
:event [::change-new-invoice-client [::new-invoice]]
:spec ::invoice/client-id
:subscription data}]]]])
(when (and should-select-location? (not exists?))
[:div.field
[:p.help "Location"]
[:div.control
[:div.select
[bind-field
[:select {:type "select"
:field [:location]
:spec (set locations)
:event change-event
:subscription data}
(map (fn [l] [:option {:value l} l]) locations)]]]]])
[:div.field
[:p.help "Vendor"]
[:div.control
[bind-field
[typeahead {:matches (map (fn [x] [(:id x) (:name x)]) @(re-frame/subscribe [::subs/vendors]))
:type "typeahead"
:disabled exists?
:auto-focus (if @(re-frame/subscribe [::subs/client]) true false)
:field [:vendor-id]
:text-field [:vendor-name]
:event change-event
:spec (s/nilable ::invoice/vendor-id)
:subscription data}]]]]
[:div.field
[:p.help "Date"]
[:div.control
[bind-field
[date-picker {:class-name "input"
:class "input"
:format-week-number (fn [] "")
:previous-month-button-label ""
:placeholder "mm/dd/yyyy"
:next-month-button-label ""
:next-month-label ""
:type "date"
:field [:date]
:event change-event
:spec ::invoice/date
:subscription data}]
#_[:input.input {:type "date"
:field [:date]
:event change-event
:spec ::invoice/date
:subscription data}]]]]
#_[horizontal-field
[:label.label "Date 2 "]
]
[:div.field
[:p.help "Invoice #"]
[:div.control
[bind-field
[:input.input {:type "text"
:field [:invoice-number]
:event change-event
:spec ::invoice/invoice-number
:subscription data}]]]]
[:div.field
[:p.help "Total"]
[:div.control
[:div.field.has-addons.is-extended
[:p.control [:a.button.is-static "$"]]
[:p.control
[bind-field
[:input.input {:type "number"
:field [:total]
:disabled (if can-change-amount? "" "disabled")
:event change-event
:min min-total
:subscription data
:spec ::invoice/total
:step "0.01"}]]]]]]
(when exists?
[:div
[:h2.subtitle "Expense Accounts"]
(for [[index {:keys [id location] :as expense-account {account-id :id account-numeric-code :numeric-code account-name :name} :account}] (map vector (range) (:expense-accounts data))]
^{:key id}
[:div.columns
[:div.column account-numeric-code " - " account-name]
(when multi-location?
[:div.column location])
[:div.column
[:div.control
[:div.field.has-addons.is-extended
[:p.control [:a.button.is-static "$"]]
[:p.control
[bind-field
[:input.input {:type "number"
:field [:expense-accounts index :amount]
:style {:text-align "right"}
:event [::forms/change ::new-invoice]
:subscription data
:value (get-in expense-account [:amount])
:max (:total data)
:step "0.01"}]]]]]]])])
(when error
^{:key error} [:div.notification.is-warning.animated.fadeInUp
error])
[:div.columns
(when-not exists?
[:div.column
[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-edit-invoice])
""
"disabled")
:class (if false
"is-loading"
"")}
"Save & Pay "
[:span " "]
[:span.icon.is-small [:i.fa.fa-angle-down {:aria-hidden "true"}]]]
:class "is-fullwidth"
:id ::add-and-print-invoice}
[:div
(list
(for [{:keys [id number name type]} (->> (:bank-accounts current-client) (filter :visible) (sort-by :sort-order))]
(if (= :cash type)
^{:key id} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print-invoice invoice-created invoice-printed id :cash])} "With cash"]
(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 "-debit")} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print-submitted invoice-created invoice-printed id :debit])} "Debit from " name]))))]]])
[:div.column
[:button.button.is-medium.is-primary.is-fullwidth {:disabled (if @(re-frame/subscribe [::can-submit-edit-invoice])
""
"disabled")
:class (str @(re-frame/subscribe [::forms/loading-class ::new-invoice])
(when error " animated shake"))} "Save"]] ]])])