improved two major forms.

This commit is contained in:
2022-07-19 15:10:33 -07:00
parent 483e9ad311
commit 0baab4eaf0
14 changed files with 431 additions and 355 deletions

View File

@@ -7,26 +7,76 @@
[auto-ap.status :as status]
[auto-ap.views.components.level :refer [left-stack]]
[auto-ap.subs :as subs]
[auto-ap.views.components :as com]
[auto-ap.views.components.address :refer [address2-field]]
[auto-ap.views.components.modal :as modal]
[auto-ap.views.components.number :refer [number-input]]
[auto-ap.views.components.typeahead :refer [typeahead-v3]]
[auto-ap.views.components.typeahead.vendor
:refer [search-backed-typeahead]]
[auto-ap.views.pages.admin.vendors.common :as common]
[auto-ap.views.components.multi :refer [multi-field-v2]]
[auto-ap.views.utils
:refer [dispatch-event multi-field str->int with-is-admin? with-user]]
[clojure.spec.alpha :as s]
[re-frame.core :as re-frame]
[reagent.core :as r]))
[reagent.core :as r]
[malli.core :as m]
[auto-ap.schema :as schema]
[malli.error :as me]))
;; Remaining cleanup todos:
;; test minification
(def terms-override-schema (m/schema [:map
[:client schema/reference]
[:terms :int]]))
(def automatically-paid-schema (m/schema [:map
[:client schema/reference]]))
(def schedule-payment-dom-schema (m/schema [:map
[:client schema/reference]
[:dom [:int {:max 30}]]]))
(def account-override-schema (m/schema [:map
[:client schema/reference]
[:account schema/reference]]))
(def schema (m/schema [:map [:name schema/not-empty-string]
[:print-as {:optional true}
[:maybe :string]]
[:hidden {:optional true}
[:maybe :boolean]]
[:terms {:optional true}
[:maybe :int]]
[:terms-overrides {:optional true}
[:maybe [:sequential terms-override-schema]]]
[:schedule-payment-dom {:optional true}
[:maybe [:sequential schedule-payment-dom-schema]]]
[:default-account schema/reference]
[:account-overrides {:optional true}
[:sequential account-override-schema]]
[:legal-entity-first-name {:optional true}
[:maybe :string]]
[:legal-entity-middle-name {:optional true}
[:maybe :string]]
[:legal-entity-last-name {:optional true}
[:maybe :string]]
[:legal-entity-tin {:optional true}
[:maybe :string]]
[:legal-entity-tin-type {:optional true}
[:or [:maybe :string]
[:maybe keyword?]]]
[:legal-entity-1099-type {:optional true}
[:or [:maybe :string]
[:maybe keyword?]]]]))
(re-frame/reg-sub
::can-submit
:<- [::forms/form ::vendor-form]
(fn [form]
(s/valid? ::entity/vendor (:data form))))
(m/validate schema (:data form))))
(re-frame/reg-event-fx
@@ -39,56 +89,55 @@
::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} _]
(when (s/valid? ::entity/vendor data)
(let [query [:upsert-vendor
{:vendor (cond-> {:id id
:name name
:print-as print-as
:terms (or (str->int terms)
(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]}}))))
: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]}})))
(defn pull-left []
(into [:div {:style {:position "relative"
@@ -103,10 +152,8 @@
[form-builder/vertical-control {:is-small? true}
"Name"
[:div.control.has-icons-left
[form-builder/raw-field
[:input.input.is-expanded {:type "text"
:field :name
:spec ::contact/name}]]
[form-builder/raw-field-v2 {:field :name}
[:input.input.is-expanded {:type "text"}]]
[:span.icon.is-small.is-left
[:i.fa.fa-user]]]]
[form-builder/vertical-control {:is-small? true}
@@ -115,137 +162,118 @@
[:div.control.has-icons-left
[:span.icon.is-small.is-left
[:i.fa.fa-envelope]]
[form-builder/raw-field
[:input.input {:type "email"
:field :email
:spec ::contact/email}]]]]
[form-builder/raw-field-v2 {:field :email}
[:input.input {:type "email"}]]]]
[form-builder/vertical-control {:is-small? true}
"Phone"
[:div.control.has-icons-left
[form-builder/raw-field
[:input.input {:type "phone"
:field :phone
:spec ::contact/phone}]]
[form-builder/raw-field-v2 {:field :phone}
[:input.input {:type "phone"}]]
[:span.icon.is-small.is-left
[:i.fa.fa-phone]]]]]]])
(defn form-content [{:keys [data]}]
(let [is-admin? @(re-frame/subscribe [::subs/is-admin?])
clients @(re-frame/subscribe [::subs/clients])]
clients @(re-frame/subscribe [::subs/client-refs])]
[form-builder/builder {:submit-event [::save]
:can-submit [::can-submit]
:id ::vendor-form}
[form-builder/field
[:span "Name " [:span.has-text-danger "*"]]
[:input.input {:type "text"
:auto-focus true
:field :name
:spec ::entity/name}]]
:id ::vendor-form
:schema schema}
[form-builder/field-v2 {:field :name
:required true}
"Name"
[:input.input {:auto-focus true}]]
[form-builder/field
[form-builder/field-v2 {:field :print-as}
"Print Checks As"
[:input.input {:type "text"
:field :print-as
:spec ::entity/print-as}]]
[:input.input]]
(when is-admin?
[:div.field
[:label.checkbox
[form-builder/raw-field
[:input {:type "checkbox"
:field [:hidden]
:spec ::entity/hidden}]]
" Hidden"]])
[form-builder/raw-field-v2 {:field :hidden}
[com/checkbox {:label "Hidden"}]])
[form-builder/section {:title "Terms"}
[form-builder/field
[form-builder/field-v2 {:field :terms}
"Terms"
[:input.input {:type "number"
:step "1"
:style {:width "4em"}
:field [:terms]
:size 3
:spec ::entity/terms}]]
[number-input ]]
(when is-admin?
[form-builder/field
[form-builder/field-v2 {:field [:terms-overrides]}
"Overrides"
[multi-field {:type "multi-field"
:field [:terms-overrides]
:template [[typeahead-v3 {:entities clients
:entity->text :name
:style {:width "13em"}
:type "typeahead-v3"
:field [:client]}]
[:input.input {:type "number"
:step "1"
:style {:width "4em"}
:field [:terms]
:size 3
:spec ::entity/terms}]]}]])
]
[multi-field-v2 {:template [[form-builder/raw-field-v2 {:field [:client]}
[typeahead-v3 {:entities clients
:entity->text :name
:style {:width "13em"}
:type "typeahead-v3"
}]]
[form-builder/raw-field-v2 {:field :terms}
[number-input]]]
:schema [:sequential terms-override-schema]
:key-fn :id
:next-key (random-uuid)
:new-text "New Terms Override"}]])]
(when is-admin?
[form-builder/section {:title "Schedule payment when due"}
[form-builder/field
[form-builder/field-v2 {:field [:automatically-paid-when-due]}
"Client"
[multi-field {:type "multi-field"
:field [:automatically-paid-when-due]
:template [[typeahead-v3 {:entities clients
:entity->text :name
:style {:width "13em"}
:type "typeahead-v3"
:field [:client]}]]}]]])
[multi-field-v2 {:template [[form-builder/raw-field-v2 {:field :client}
[typeahead-v3 {:entities clients
:entity->text :name
:style {:width "13em"}}]]]
:schema [:sequential automatically-paid-schema]
:key-fn :id
:next-key (random-uuid)
:new-text "Schedule another client"}]]])
(when is-admin?
[form-builder/section {:title "Schedule payment on day of month"}
[form-builder/field
[form-builder/field-v2 {:field [:schedule-payment-dom]}
"Overrides"
[multi-field {:type "multi-field"
:field [:schedule-payment-dom]
:template [[typeahead-v3 {:entities clients
:entity->text :name
:style {:width "13em"}
:type "typeahead-v3"
:field [:client]}]
[:input.input {:type "number"
:step "1"
:style {:width "4em"}
:field [:dom]
:size 3
:spec ::entity/terms}]]}]]])
[multi-field-v2 {:template [[form-builder/raw-field-v2 {:field :client}
[typeahead-v3 {:entities clients
:entity->text :name
:style {:width "13em"}}]]
[form-builder/raw-field-v2 {:field :dom}
[number-input]]]
:schema [:sequential schedule-payment-dom-schema]
:key-fn :id
:next-key (random-uuid)
:new-text "Schedule another client"}]]])
[form-builder/section {:title "Expense Accounts"}
[form-builder/field
"Default *"
[form-builder/field-v2 {:field :default-account
:required? true}
"Default"
[search-backed-typeahead {:search-query (fn [i]
[:search_account
{:query i}
[:name :id]])
:type "typeahead-v3"
:style {:width "19em"}
:field [:default-account]}]]
:style {:width "19em"}}]]
(when is-admin?
[form-builder/field
[form-builder/field-v2 {:field [:account-overrides]}
"Overrides"
[multi-field {:type "multi-field"
:field [:account-overrides]
:template (fn [entity]
[[typeahead-v3 {:entities clients
:entity->text :name
:style {:width "19em"}
:type "typeahead-v3"
:field [:client]}]
[search-backed-typeahead {:search-query (fn [i]
[:search_account
{:query i
:client_id (:id (:client entity))}
[:name :id]])
:type "typeahead-v3"
:style {:width "15em"}
:field [:account]}]])}]])]
[multi-field-v2 {:template (fn [entity]
[[form-builder/raw-field-v2 {:field :client}
[typeahead-v3 {:entities clients
:entity->text :name
:style {:width "19em"}
}]]
[form-builder/raw-field-v2 {:field :account}
[search-backed-typeahead {:search-query (fn [i]
[:search_account
{:query i
:client_id (:id (:client entity))}
[:name :id]])
:style {:width "15em"}}]]])
:schema [:sequential account-override-schema]
:key-fn :id
:next-key (random-uuid)
:new-text "Add override"}]])]
[form-builder/with-scope {:scope [:address ]}
[form-builder/section {:title "Address"}
[:div {:style {:width "30em"}}
[form-builder/section {:title "Address"}
[:div {:style {:width "30em"}}
[form-builder/raw-field-v2 {:field :address}
[address2-field]]]]
[form-builder/section {:title "Contact"}
@@ -261,55 +289,40 @@
"Name"
[left-stack
[:div.control
[form-builder/raw-field
[form-builder/raw-field-v2 {:field :legal-entity-first-name}
[:input.input {:type "text"
:placeholder "First Name"
:field [:legal-entity-first-name]
:spec ::contact/name}]]]
:placeholder "First Name"}]]]
[:div.control
[form-builder/raw-field
[form-builder/raw-field-v2 {:field :legal-entity-middle-name}
[:input.input {:type "text"
:placeholder "Middle Name"
:field [:legal-entity-middle-name]
:spec ::contact/name}]]]
:placeholder "Middle Name"}]]]
[:div.control
[form-builder/raw-field
[form-builder/raw-field-v2 {:field :legal-entity-last-name}
[:input.input {:type "text"
:placeholder "Last Name"
:field [:legal-entity-last-name]
:spec ::contact/name}]]]]]
:placeholder "Last Name"}]]]]]
[form-builder/vertical-control
"TIN"
[left-stack
[form-builder/raw-field
[form-builder/raw-field-v2 {:field :legal-entity-tin}
[:input.input {:type "text"
:placeholder "SSN or EIN"
:field [:legal-entity-tin]
:size "12"
:spec ::contact/name}]]
}]]
[:div.control
[:div.select
[form-builder/raw-field
[:select {:type "select"
:field [:legal-entity-tin-type]}
[:option {:value nil} ""]
[:option {:value "ein"} "EIN"]
[:option {:value "ssn"} "SSN"]]]]]]]
[form-builder/raw-field-v2 {:field :legal-entity-tin-type}
[com/select-field {:options [["ein" "EIN"]
["ssn" "SSN"]]
:allow-nil? true}]]]]]
[form-builder/vertical-control
[form-builder/field-v2 {:field :legal-entity-1099-type}
"1099 Type"
[:div.control
[:div.select
[form-builder/raw-field
[:select {:type "select"
:field [:legal-entity-1099-type]}
[:option {:value nil} ""]
[:option {:value "none"} "Don't 1099"]
[:option {:value "misc"} "Misc"]
[:option {:value "landlord"} "Landlord"]]]]]]])
[com/select-field {:options [["none" "Don't 1099"]
["misc" "Misc"]
["landlord" "Landlord"]]
:allow-nil? true}]]])
[form-builder/hidden-submit-button]]))
(defn vendor-dialog [ ]
@@ -342,15 +355,15 @@
[form-builder/builder {:submit-event [::vendor-selected]
:can-submit [::can-submit-select-vendor-form]
:id ::select-vendor-form}
[form-builder/field
[form-builder/field-v2 {:field :vendor
:required? true}
"Vendor to edit"
[search-backed-typeahead {:search-query (fn [i]
[:search_vendor
{:query i}
[:name :id]])
:type "typeahead-v3"
:auto-focus true
:field [:vendor]}]]
:style {:width "20em"}
:auto-focus true}]]
[form-builder/hidden-submit-button]])
@@ -360,6 +373,7 @@
(fn [{:keys [db]} [_ vendor]]
{:db (-> db (forms/start-form ::vendor-form (-> vendor
(update :automatically-paid-when-due #(mapv (fn [apwd]
apwd
{:id (:id apwd)
:client apwd})
%))