Simplified forms considerably

This commit is contained in:
2022-07-16 10:15:47 -07:00
parent d16b9c9a5e
commit 16a1d243e8
16 changed files with 519 additions and 592 deletions

View File

@@ -3,23 +3,24 @@
[auto-ap.entities.contact :as contact]
[auto-ap.entities.vendors :as entity]
[auto-ap.forms :as forms]
[auto-ap.forms.builder :as form-builder]
[auto-ap.status :as status]
[auto-ap.views.components.level :refer [left-stack]]
[auto-ap.subs :as subs]
[auto-ap.views.components.address :refer [address2-field]]
[auto-ap.views.components.typeahead.vendor
:refer [search-backed-typeahead]]
[auto-ap.views.components.modal :as modal]
[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.utils
:refer [bind-field
dispatch-event
horizontal-field
with-is-admin?
with-user]]
:refer [dispatch-event multi-field str->int with-is-admin? with-user]]
[clojure.spec.alpha :as s]
[re-frame.core :as re-frame]
[auto-ap.forms.builder :as form-builder]))
[reagent.core :as r]))
;; Remaining cleanup todos:
;; test minification
(re-frame/reg-sub
::can-submit
@@ -27,37 +28,6 @@
(fn [form]
(s/valid? ::entity/vendor (:data form))))
(re-frame/reg-event-db
::removed-override
[(forms/in-form ::vendor-form)]
(fn [form [_ override-key index]]
(update-in form [:data override-key]
(fn [overrides]
(reduce
(fn [overrides [i override]]
(if (= i index)
overrides
(conj overrides override)))
[]
(map vector (range) overrides))))))
(re-frame/reg-event-db
::changed
[(forms/settles {:key ::vendor-form
:time 500
:event [::settled]})]
(forms/change-handler
::vendor-form
(fn [data field _]
(let [[override-key? i?] field]
(if (and (#{:account-overrides :terms-overrides :schedule-payment-dom} override-key?)
(nil? (get-in data [override-key? i? :key])))
[[override-key? i? :key] (random-uuid)]
[])))))
(re-frame/reg-event-fx
::save-complete
@@ -74,7 +44,8 @@
{:vendor (cond-> {:id id
:name name
:print-as print-as
:terms terms
:terms (or (str->int terms)
0)
:default-account-id (:id default-account)
:address address
:primary-contact primary-contact
@@ -82,28 +53,29 @@
:invoice-reminder-schedule invoice-reminder-schedule}
is-admin? (assoc :hidden hidden
:terms-overrides (mapv
(fn [{:keys [client override id]}]
(fn [{:keys [client terms id]}]
{:id id
:client-id (:id client)
:terms override})
:terms (or (str->int terms) 0)})
terms-overrides)
:account-overrides (mapv
(fn [{:keys [client override id]}]
(fn [{:keys [client account id]}]
{:id id
:client-id (:id client)
:account-id (:id override)})
:account-id (:id account)})
account-overrides)
:schedule-payment-dom (mapv
(fn [{:keys [client override id]}]
(fn [{:keys [client dom id]}]
{:id id
:client-id (:id client)
:dom override})
:dom (or (str->int dom)
0)})
schedule-payment-dom)
:automatically-paid-when-due (mapv
:id
(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-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)
@@ -118,239 +90,176 @@
:operation/name "UpsertVendor"} :venia/queries [{:query/data query}]}
:on-success [::save-complete]}}))))
(defn client-list [{:keys [override-key data]}]
(let [clients @(re-frame/subscribe [::subs/clients])]
[form-builder/horizontal-control
"Client"
(doall
(for [[i override] (map vector (range) (conj (override-key data) {:key (random-uuid)}))]
^{:key (or (:id override)
(:key override))}
[:div.level
[:div.level-left
[:div.level-item
[form-builder/raw-field
[typeahead-v3 {:entities clients
:entity->text :name
:style {:width "13em"}
:type "typeahead-v3"
:field [override-key i]}]]]
[:div.level-item
[:a.button {:on-click (dispatch-event [::removed-override override-key i])} [:span.icon [:span.icon-remove]]]]]]))]))
(defn pull-left []
(into [:div {:style {:position "relative"
:left "-40px"}}]
(r/children (r/current-component))))
(defn default-with-overrides [{:keys [override-key default-key data mandatory?]} template]
(let [clients @(re-frame/subscribe [::subs/clients])]
[:div
[form-builder/horizontal-control
[:span "Default"
(when mandatory?
[:span.has-text-danger " *"])]
(template default-key nil)]
[form-builder/horizontal-control
"Overrides"
(doall
(for [[i override] (map vector (range) (conj (override-key data) {:key (random-uuid)}))]
^{:key (or
(:id override)
(:key override)
)}
[:div.level
[:div.level-left
[:div.level-item
[form-builder/raw-field
[typeahead-v3 {:entities clients
:entity->text :name
:style {:width "13em"}
:type "typeahead-v3"
:field [override-key i :client]}]]]
[:div.level-item
(template
[override-key i :override]
(get-in data [override-key i :client]))]
[:div.level-item
[:a.button {:on-click (dispatch-event [::removed-override override-key i])} [:span.icon [:span.icon-remove]]]]]]))]]))
(defn contact-field [{:keys [name field]}]
[form-builder/with-scope {:scope field}
[form-builder/vertical-control
name
[left-stack
[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}]]
[:span.icon.is-small.is-left
[:i.fa.fa-user]]]]
[form-builder/vertical-control {:is-small? true}
"Email"
[: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/vertical-control {:is-small? true}
"Phone"
[:div.control.has-icons-left
[form-builder/raw-field
[:input.input {:type "phone"
:field :phone
:spec ::contact/phone}]]
[:span.icon.is-small.is-left
[:i.fa.fa-phone]]]]]]])
(defn client-overrides [{:keys [override-key data]} template]
(let [clients @(re-frame/subscribe [::subs/clients])]
[form-builder/horizontal-control
"Overrides"
(doall
(for [[i override] (map vector (range) (conj (override-key data) {:key (random-uuid)}))]
^{:key (or
(:id override)
(:key override))}
[:div.level
[:div.level-left
[:div.level-item
[form-builder/raw-field
[typeahead-v3 {:entities clients
:entity->text :name
:style {:width "13em"}
:type "typeahead-v3"
:field [override-key i :client]}]]]
[:div.level-item
(template
[override-key i :override]
(get-in data [override-key i :client]))]
[:div.level-item
[:a.button {:on-click (dispatch-event [::removed-override override-key i])} [:span.icon [:span.icon-remove]]]]]]))]))
(defn form-content [{:keys [data]}]
(let [is-admin? @(re-frame/subscribe [::subs/is-admin?])]
(let [is-admin? @(re-frame/subscribe [::subs/is-admin?])
clients @(re-frame/subscribe [::subs/clients])]
[form-builder/builder {:submit-event [::save]
:can-submit [::can-submit]
:change-event [::changed]
:id ::vendor-form}
[:div
[form-builder/horizontal-field
[:span "Name " [:span.has-text-danger "*"]]
[:input.input {:type "text"
:auto-focus true
:field :name
:spec ::entity/name}]]
[form-builder/field
[:span "Name " [:span.has-text-danger "*"]]
[:input.input {:type "text"
:auto-focus true
:field :name
:spec ::entity/name}]]
[form-builder/horizontal-field
"Print Checks As"
[:input.input {:type "text"
:field :print-as
:spec ::entity/print-as}]]
[form-builder/field
"Print Checks As"
[:input.input {:type "text"
:field :print-as
:spec ::entity/print-as}]]
(when is-admin?
[:div.field
[:label.checkbox
[form-builder/raw-field
[:input {:type "checkbox"
:field [:hidden]
:spec ::entity/hidden}]]
" Hidden"]])
[form-builder/section {:title "Terms"}
[form-builder/field
"Terms"
[:input.input {:type "number"
:step "1"
:style {:width "4em"}
:field [:terms]
:size 3
:spec ::entity/terms}]]
(when is-admin?
[form-builder/horizontal-field
"Hidden"
[:input {:type "checkbox"
:field :hidden
:spec ::entity/hidden}]])
[form-builder/field
"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}]]}]])
]
(when is-admin?
[form-builder/section {:title "Schedule payment when due"}
[form-builder/field
"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]}]]}]]])
(if is-admin?
[:<>
[form-builder/section {:title "Terms"}
[default-with-overrides {:data data
:default-key :terms
:override-key :terms-overrides}
(fn [field _]
[form-builder/raw-field
[:input.input {:type "number"
:step "1"
:style {:width "4em"}
:field field
:size 3
:spec ::entity/terms}]])]]]
[form-builder/horizontal-field
[:span "Terms"]
[:input.input {:type "number"
:step "1"
:style {:width "4em"}
:field :terms
:size 3
:spec ::entity/terms}]])
(when is-admin?
[form-builder/section {:title "Schedule payment on day of month"}
[form-builder/field
"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}]]}]]])
[form-builder/section {:title "Expense Accounts"}
[form-builder/field
"Default *"
[search-backed-typeahead {:search-query (fn [i]
[:search_account
{:query i}
[:name :id]])
:type "typeahead-v3"
:style {:width "19em"}
:field [:default-account]}]]
(when is-admin?
[form-builder/section {:title "Schedule payment when due"}
[client-list {:data data
:override-key :automatically-paid-when-due}]])
[form-builder/field
"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]}]])}]])]
(when is-admin?
[form-builder/section {:title "Schedule payment on day of month"}
[client-overrides {:data data
:mandatory? true
:override-key :schedule-payment-dom}
(fn [field _]
[form-builder/raw-field
[:input.input {:type "number"
:step "1"
:style {:width "5em"}
:field field
:size 3
:spec ::entity/dom}]])]])
(if is-admin?
[form-builder/section {:title "Expense Accounts"}
[default-with-overrides {:data data
:mandatory? true
:default-key :default-account
:override-key :account-overrides}
(fn [field client]
[form-builder/raw-field
[search-backed-typeahead {:search-query (fn [i]
[:search_account
{:query i
:client-id (:id client)}
[:name :id]])
:type "typeahead-v3"
:style {:width "15em"}
:field field}]])]]
[form-builder/with-scope {:scope [:address ]}
[form-builder/section {:title "Address"}
[:div {:style {:width "30em"}}
[address2-field]]]]
[form-builder/section {:title "Contact"}
[contact-field {:name "Primary"
:field [:primary-contact]}]
[contact-field {:name "Secondary"
:field [:secondary-contact]}]]
[form-builder/horizontal-field
"Expense Account"
[search-backed-typeahead {:search-query (fn [i]
[:search_account
{:query i}
[:name :id]])
:type "typeahead-v3"
:field :default-account}]])
[form-builder/with-scope {:scope [:address ]}
[form-builder/section {:title "Address"}
[address2-field]]]
[form-builder/section {:title "Contact"}
[form-builder/horizontal-control
"Primary"
[:div.control.has-icons-left
[form-builder/raw-field
[:input.input.is-expanded {:type "text"
:field [:primary-contact :name]
:spec ::contact/name}]]
[:span.icon.is-small.is-left
[:i.fa.fa-user]]]
[:div.control.has-icons-left
[:span.icon.is-small.is-left
[:i.fa.fa-envelope]]
[form-builder/raw-field
[:input.input {:type "email"
:field [:primary-contact :email]
:spec ::contact/email}]]]
[:div.control.has-icons-left
[form-builder/raw-field
[:input.input {:type "phone"
:field [:primary-contact :phone]
:spec ::contact/phone}]]
[:span.icon.is-small.is-left
[:i.fa.fa-phone]]]]
[form-builder/horizontal-control
"Secondary"
[:div.control.has-icons-left
[form-builder/raw-field
[:input.input.is-expanded {:type "text"
:field [:secondary-contact :name]
:spec ::contact/name}]]
[:span.icon.is-small.is-left
[:i.fa.fa-user]]]
[:div.control.has-icons-left
[:span.icon.is-small.is-left
[:i.fa.fa-envelope]]
[form-builder/raw-field
[:input.input {:type "email"
:field [:secondary-contact :email]
:spec ::contact/email}]]]
[:div.control.has-icons-left
[form-builder/raw-field
[:input.input {:type "phone"
:field [:secondary-contact :phone]
:spec ::contact/phone}]]
[:span.icon.is-small.is-left
[:i.fa.fa-phone]]]]]
(when is-admin?
[form-builder/section {:title "Legal Entity"}
[form-builder/horizontal-control
"Name"
(when is-admin?
[form-builder/section {:title "Legal Entity"}
[form-builder/vertical-control
"Name"
[left-stack
[:div.control
[form-builder/raw-field
[:input.input {:type "text"
@@ -370,16 +279,16 @@
[:input.input {:type "text"
:placeholder "Last Name"
:field [:legal-entity-last-name]
:spec ::contact/name}]]]]
[form-builder/horizontal-control
"TIN"
[:div.control
[form-builder/raw-field
[:input.input {:type "text"
:placeholder "SSN or EIN"
:field [:legal-entity-tin]
:size "12"
:spec ::contact/name}]]]
:spec ::contact/name}]]]]]
[form-builder/vertical-control
"TIN"
[left-stack
[form-builder/raw-field
[:input.input {:type "text"
:placeholder "SSN or EIN"
:field [:legal-entity-tin]
:size "12"
:spec ::contact/name}]]
[:div.control
[:div.select
@@ -388,19 +297,20 @@
:field [:legal-entity-tin-type]}
[:option {:value nil} ""]
[:option {:value "ein"} "EIN"]
[:option {:value "ssn"} "SSN"]]]]]]
[:option {:value "ssn"} "SSN"]]]]]]]
[form-builder/horizontal-control
"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"]]]]]]])]]))
[form-builder/vertical-control
"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"]]]]]]])
[form-builder/hidden-submit-button]]))
(defn vendor-dialog [ ]
(let [{:keys [data]} @(re-frame/subscribe [::forms/form ::vendor-form])]
@@ -417,7 +327,6 @@
common/default-read]]}
:owns-state {:single ::select-vendor-form}
:on-success (fn [r]
(println (:vendor-by-id r))
[::started (:vendor-by-id r)])}}))
(re-frame/reg-sub
@@ -442,7 +351,7 @@
:type "typeahead-v3"
:auto-focus true
:field [:vendor]}]]
#_[form-builder/submit-button "Save"]])
[form-builder/hidden-submit-button]])
@@ -450,33 +359,17 @@
::started
(fn [{:keys [db]} [_ vendor]]
{:db (-> db (forms/start-form ::vendor-form (-> vendor
(update :account-overrides #(mapv
(fn [ao]
{:id (:id ao)
:client (:client ao)
:override (:account ao)})
%))
(update :schedule-payment-dom #(mapv
(fn [spdom]
{:id (:id spdom)
:client (:client spdom)
:override (:dom spdom)})
%))
(update :terms-overrides #(mapv
(fn [to]
{:id (:id to)
:client (:client to)
:override (:terms to)})
%))
(update :automatically-paid-when-due #(mapv identity %))
(update :automatically-paid-when-due #(mapv (fn [apwd]
{:id (:id apwd)
:client apwd})
%))
(update :hidden #(if (nil? %)
false
%)))))
:dispatch [::modal/modal-requested
{:title "Vendor"
:class "is-wide"
:body [vendor-dialog]
:class "semi-wide"
:confirm {:value "Save Vendor"
:status-from [::status/single ::vendor-form]
:class "is-primary"
@@ -490,7 +383,6 @@
{:db (-> db (forms/start-form ::select-vendor-form {}))
:dispatch [::modal/modal-requested
{:title "Select Vendor"
:class "is-wide"
:body [select-vendor-form-content]
:confirm {:value "Choose a vendor"
:status-from [::status/single ::select-vendor-form]