supports validation on complete

This commit is contained in:
2022-07-20 06:02:47 -07:00
parent 0baab4eaf0
commit 9b5b7bef53
6 changed files with 115 additions and 127 deletions

View File

@@ -3,6 +3,7 @@
[auto-ap.forms.builder :as form-builder]
[auto-ap.schema :as schema]
[auto-ap.utils :refer [dollars-0?]]
[auto-ap.views.components :as com]
[auto-ap.views.components.button-radio :as button-radio]
[auto-ap.views.components.level :refer [left-stack]]
[auto-ap.views.components.money-field :refer [money-field]]
@@ -232,18 +233,7 @@
[:location schema/not-empty-string]
[:amount schema/money]]]))
(defn select-field [{:keys [options allow-nil? class] :as props}]
[:div.select {:class class}
[:select (-> props
(dissoc :allow-nil? :class :options)
(update :value (fn [v] (if (str/blank? v)
""
v))))
[:<>
(when allow-nil?
[:option {:value nil}])
(for [[k v] options]
^{:key k} [:option {:value k} v])]]])
(defn expense-accounts-field-v2 [{value :value on-change :on-change expense-accounts :value client :client max-value :max locations :locations disabled :disabled percentage-only? :percentage-only? :or {percentage-only? false}}]
[form-builder/virtual-builder {:value value
@@ -306,7 +296,7 @@
[form-builder/field-v2 {:required? true
:field [index :location]}
"Location"
[select-field {:options (if (:location account)
[com/select-field {:options (if (:location account)
[[(:location account) (:location account)]]
(map (fn [l] [l l])
locations))

View File

@@ -72,13 +72,6 @@
[:or [:maybe :string]
[:maybe keyword?]]]]))
(re-frame/reg-sub
::can-submit
:<- [::forms/form ::vendor-form]
(fn [form]
(m/validate schema (:data form))))
(re-frame/reg-event-fx
::save-complete
[(forms/triggers-stop ::vendor-form)]
@@ -89,55 +82,58 @@
::save
[with-user with-is-admin? (forms/triggers-loading ::vendor-form) (forms/in-form ::vendor-form)]
(fn [{:keys [user is-admin?] {{:keys [name hidden print-as terms invoice-reminder-schedule primary-contact automatically-paid-when-due schedule-payment-dom secondary-contact address default-account terms-overrides account-overrides id legal-entity-tin legal-entity-tin-type legal-entity-first-name legal-entity-last-name legal-entity-middle-name legal-entity-1099-type] :as data} :data} :db} _]
(let [query [:upsert-vendor
{:vendor (cond-> {:id id
:name name
:print-as print-as
:terms (or terms
nil)
:default-account-id (:id default-account)
:address address
:primary-contact primary-contact
:secondary-contact secondary-contact
:invoice-reminder-schedule invoice-reminder-schedule}
is-admin? (assoc :hidden hidden
:terms-overrides (mapv
(fn [{:keys [client terms id]}]
{:id id
:client-id (:id client)
:terms (or (str->int terms) 0)})
terms-overrides)
:account-overrides (mapv
(fn [{:keys [client account id]}]
{:id id
:client-id (:id client)
:account-id (:id account)})
account-overrides)
:schedule-payment-dom (mapv
(fn [{:keys [client dom id]}]
{:id id
:client-id (:id client)
:dom (or (str->int dom)
0)})
schedule-payment-dom)
:automatically-paid-when-due (mapv
(comp :id :client)
automatically-paid-when-due)
:legal-entity-first-name legal-entity-first-name
:legal-entity-middle-name legal-entity-middle-name
:legal-entity-last-name legal-entity-last-name
:legal-entity-tin legal-entity-tin
:legal-entity-tin-type (some-> legal-entity-tin-type clojure.core/name not-empty keyword)
:legal-entity-1099-type (some-> legal-entity-1099-type clojure.core/name not-empty keyword)
))}
common/default-read]]
{ :graphql
{:token user
:owns-state {:single ::vendor-form}
:query-obj {:venia/operation
{:operation/type :mutation
:operation/name "UpsertVendor"} :venia/queries [{:query/data query}]}
:on-success [::save-complete]}})))
(if (m/validate schema data)
(let [query [:upsert-vendor
{:vendor (cond-> {:id id
:name name
:print-as print-as
:terms (or terms
nil)
:default-account-id (:id default-account)
:address address
:primary-contact primary-contact
:secondary-contact secondary-contact
:invoice-reminder-schedule invoice-reminder-schedule}
is-admin? (assoc :hidden hidden
:terms-overrides (mapv
(fn [{:keys [client terms id]}]
{:id id
:client-id (:id client)
:terms (or (str->int terms) 0)})
terms-overrides)
:account-overrides (mapv
(fn [{:keys [client account id]}]
{:id id
:client-id (:id client)
:account-id (:id account)})
account-overrides)
:schedule-payment-dom (mapv
(fn [{:keys [client dom id]}]
{:id id
:client-id (:id client)
:dom (or (str->int dom)
0)})
schedule-payment-dom)
:automatically-paid-when-due (mapv
(comp :id :client)
automatically-paid-when-due)
:legal-entity-first-name legal-entity-first-name
:legal-entity-middle-name legal-entity-middle-name
:legal-entity-last-name legal-entity-last-name
:legal-entity-tin legal-entity-tin
:legal-entity-tin-type (some-> legal-entity-tin-type clojure.core/name not-empty keyword)
:legal-entity-1099-type (some-> legal-entity-1099-type clojure.core/name not-empty keyword)))}
common/default-read]]
{ :graphql
{:token user
:owns-state {:single ::vendor-form}
:query-obj {:venia/operation
{:operation/type :mutation
:operation/name "UpsertVendor"} :venia/queries [{:query/data query}]}
:on-success [::save-complete]}})
{:dispatch-n [[::forms/attempted-submit ::vendor-form]
[::status/error ::vendor-form [{:message "Please fix the errors and try again."}]]]})))
(defn pull-left []
(into [:div {:style {:position "relative"
@@ -178,7 +174,6 @@
(let [is-admin? @(re-frame/subscribe [::subs/is-admin?])
clients @(re-frame/subscribe [::subs/client-refs])]
[form-builder/builder {:submit-event [::save]
:can-submit [::can-submit]
:id ::vendor-form
:schema schema}
[form-builder/field-v2 {:field :name
@@ -275,19 +270,18 @@
[:div {:style {:width "30em"}}
[form-builder/raw-field-v2 {:field :address}
[address2-field]]]]
[form-builder/section {:title "Contact"}
[contact-field {:name "Primary"
:field [:primary-contact]}]
[contact-field {:name "Secondary"
:field [:secondary-contact]}]]
(when is-admin?
[form-builder/section {:title "Legal Entity"}
[form-builder/vertical-control
"Name"
[left-stack
[left-stack
[:div.control
[form-builder/raw-field-v2 {:field :legal-entity-first-name}
[:input.input {:type "text"
@@ -304,7 +298,7 @@
:placeholder "Last Name"}]]]]]
[form-builder/vertical-control
"TIN"
[left-stack
[left-stack
[form-builder/raw-field-v2 {:field :legal-entity-tin}
[:input.input {:type "text"
:placeholder "SSN or EIN"
@@ -334,27 +328,23 @@
::vendor-selected
[with-user (forms/in-form ::select-vendor-form)]
(fn [{{:keys [data]} :db :keys [user]} _]
{:graphql {:token user
:query-obj {:venia/queries [[:vendor-by-id
{:id (:id (:vendor data))}
common/default-read]]}
:owns-state {:single ::select-vendor-form}
:on-success (fn [r]
[::started (:vendor-by-id r)])}}))
(re-frame/reg-sub
::can-submit-select-vendor-form
:<- [::forms/field ::select-vendor-form [:vendor]]
(fn [vendor]
(if vendor
true
false)))
(if (:vendor data)
{:graphql {:token user
:query-obj {:venia/queries [[:vendor-by-id
{:id (:id (:vendor data))}
common/default-read]]}
:owns-state {:single ::select-vendor-form}
:on-success (fn [r]
[::started (:vendor-by-id r)])}}
{:dispatch-n [[::forms/attempted-submit ::select-vendor-form]
[::status/error ::select-vendor-form [{:message "Please select a vendor."}]]]})))
(defn select-vendor-form-content []
[form-builder/builder {:submit-event [::vendor-selected]
:can-submit [::can-submit-select-vendor-form]
:id ::select-vendor-form}
:id ::select-vendor-form
:validation-error-string "Please select a vendor."
:schema [:map
[:vendor schema/reference]]}
[form-builder/field-v2 {:field :vendor
:required? true}
"Vendor to edit"
@@ -384,11 +374,11 @@
{:title "Vendor"
:body [vendor-dialog]
:class "semi-wide"
:status-from [::status/single ::vendor-form]
:confirm {:value "Save Vendor"
:status-from [::status/single ::vendor-form]
:class "is-primary"
:on-click (dispatch-event [::save])
:can-submit [::can-submit]
:close-event [::status/completed ::vendor-form]}}]}))
(re-frame/reg-event-fx
@@ -402,5 +392,4 @@
:status-from [::status/single ::select-vendor-form]
:class "is-primary"
:on-click (dispatch-event [::vendor-selected])
:can-submit [::can-submit-select-vendor-form]
:close-event [::status/completed ::select-vendor-form]}}]}))