Undoes old way of doing forms in favor of context version. Much easier

This commit is contained in:
2022-07-17 17:32:47 -07:00
parent a17f587ff3
commit b6962c5bfa
17 changed files with 782 additions and 1393 deletions

View File

@@ -2,7 +2,6 @@
(:require
[auto-ap.forms :as forms]
[auto-ap.status :as status]
[auto-ap.subs :as subs]
[auto-ap.utils :refer [by]]
[auto-ap.views.components.modal :as modal]
[auto-ap.views.components.typeahead.vendor :refer [search-backed-typeahead]]
@@ -10,17 +9,13 @@
[auto-ap.views.utils :refer [dispatch-event with-user]]
[clojure.string :as str]
[goog.string :as gstring]
[re-frame.core :as re-frame]))
(re-frame/reg-sub
::can-submit
(fn [db]
true))
[re-frame.core :as re-frame]
[auto-ap.forms.builder :as form-builder]))
(re-frame/reg-event-fx
::try-save
[(forms/in-form ::form)]
(fn [{:keys [db]} [_ id ]]
(fn [{:keys [db]} _]
(let [{{:keys [ total]} :invoice
:keys [expense-accounts]} (:data db)
expense-accounts (vals expense-accounts)
@@ -44,7 +39,7 @@
(re-frame/reg-event-fx
::save
[with-user (forms/in-form ::form)]
(fn [{:keys [db user] } [_ id]]
(fn [{:keys [db user] } _]
(let [{{:keys [id]} :invoice
:keys [expense-accounts]} (:data db)
expense-accounts (vals expense-accounts)]
@@ -85,16 +80,10 @@
(fn [db [_ x]]
(update-in db [:data :expense-accounts] dissoc x)))
(def change-expense-accounts-form (forms/vertical-form {:submit-event [::try-save]
:change-event [::forms/change ::form]
:can-submit [::can-submit]
:id ::form}))
(defn form []
(let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form])
(let [{:keys [data]} @(re-frame/subscribe [::forms/form ::form])
expense-accounts (:expense-accounts data)
{:keys [total] :or {total 0} {:keys [locations] :as client} :client} (:invoice data)
{:keys [form-inline horizontal-field field raw-field error-notification submit-button]} change-expense-accounts-form
multi-location? (> (count locations) 1)
expense-accounts-total (->> expense-accounts
vals
@@ -108,72 +97,75 @@
[:div
[:div
[:a.button.is-outlined {:on-click (dispatch-event [::add-split])} "Add split"]]
(form-inline {}
[:table.table
[:thead
[:tr
[:th {:style {:width "500px"}} "Expense Account"]
(when multi-location?
[:th {:style {:width "200px"}} "Location"])
[:th {:style {:width "200px"}} "Original Amount"]
[:th {:style {:width "300px"}} "Amount"]
[:th {:style {:width "5em"}}]]]
[:tbody
(doall (for [[id expense-account] expense-accounts]
^{:key id}
[:tr
[:td.expandable [:div.control
(raw-field
[search-backed-typeahead {:search-query (fn [i]
[:search_account
{:query i
:client-id (:id client)}
[:name :id :location]])
:type "typeahead-v3"
:field [:expense-accounts id :account]}])]]
[form-builder/builder {:submit-event [::try-save]
:id ::form}
[:table.table
[:thead
[:tr
[:th {:style {:width "500px"}} "Expense Account"]
(when multi-location?
[:th {:style {:width "200px"}} "Location"])
[:th {:style {:width "200px"}} "Original Amount"]
[:th {:style {:width "300px"}} "Amount"]
[:th {:style {:width "5em"}}]]]
[:tbody
(doall (for [[id _] expense-accounts]
^{:key id}
[:tr
[:td.expandable [:div.control
[form-builder/raw-field
[search-backed-typeahead {:search-query (fn [i]
[:search_account
{:query i
:client-id (:id client)}
[:name :id :location]])
:type "typeahead-v3"
:field [:expense-accounts id :account]}]]]]
(when multi-location?
[:td
(if-let [forced-location (get-in expense-accounts [id :account :location])]
[:div.select
[:select {:disabled "disabled" :value forced-location} [:option {:value forced-location} forced-location]]]
[:div.select
(raw-field
[:select {:type "select"
:field [:expense-accounts id :location]
:spec (set locations)}
(map (fn [l] ^{:key l} [:option {:value l} l]) locations)])])])
[:td
(str "$" (get-in expense-accounts [id :amount]))]
[:td
[:div.control
[:div.field.has-addons.is-extended
[:p.control [:a.button.is-static "$"]]
[:p.control
(raw-field
[:input.input {:type "number"
:field [:expense-accounts id :new-amount-temp]
:style {:text-align "right"}
:on-blur (dispatch-event [::forms/change ::form [:expense-accounts id :new-amount] (get-in expense-accounts [id :new-amount-temp])])
:on-key-down (fn [e ]
(if (= 13 (.-keyCode e))
(do
(re-frame/dispatch [::forms/change ::form [:expense-accounts id :new-amount] (get-in expense-accounts [id :new-amount-temp])])
true)
false))
:max (:total data)
:step "0.01"}])]]]]
[:td [:a.button {:on-click (dispatch-event [::remove-expense-account-split id])} [:i.fa.fa-times]]]]))
[:tr
[:td.no-border { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Invoice total: "]
[:td.no-border { :style { :text-align "right"} } (str (gstring/format "$%.2f" total ) )]]
[:tr
[:td { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Account total: "]
[:td { :style { :text-align "right"} } (str (gstring/format "$%.2f" expense-accounts-total ) )]]
[:tr
[:td.no-border { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Difference: "]
[:td.no-border { :style { :text-align "right"} } (str (gstring/format "$%.2f" (- total expense-accounts-total) ) )]]]])]))
(when multi-location?
[:td
(if-let [forced-location (get-in expense-accounts [id :account :location])]
[:div.select
[:select {:disabled "disabled" :value forced-location} [:option {:value forced-location} forced-location]]]
[:div.select
[form-builder/raw-field
[:select {:type "select"
:field [:expense-accounts id :location]
:spec (set locations)}
(map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])])
[:td
(str "$" (get-in expense-accounts [id :amount]))]
[:td
[:div.control
[:div.field.has-addons.is-extended
[:p.control [:a.button.is-static "$"]]
[:p.control
[form-builder/raw-field
[:input.input {:type "number"
:field [:expense-accounts id :new-amount-temp]
:style {:text-align "right"}
:on-blur (dispatch-event [::forms/change ::form [:expense-accounts id :new-amount] (get-in expense-accounts [id :new-amount-temp])])
:on-key-down (fn [e ]
(if (= 13 (.-keyCode e))
(do
(re-frame/dispatch [::forms/change ::form [:expense-accounts id :new-amount] (get-in expense-accounts [id :new-amount-temp])])
true)
false))
:max (:total data)
:step "0.01"}]]]]]]
[:td [:a.button {:on-click (dispatch-event [::remove-expense-account-split id])} [:i.fa.fa-times]]]]))
[:tr
[:td.no-border { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Invoice total: "]
[:td.no-border { :style { :text-align "right"} } (str (gstring/format "$%.2f" total ) )]]
[:tr
[:td { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Account total: "]
[:td { :style { :text-align "right"} } (str (gstring/format "$%.2f" expense-accounts-total ) )]]
[:tr
[:td.no-border { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Difference: "]
[:td.no-border { :style { :text-align "right"} } (str (gstring/format "$%.2f" (- total expense-accounts-total) ) )]]]]
[form-builder/hidden-submit-button]]]))
(re-frame/reg-event-fx
::show
@@ -184,11 +176,10 @@
:status-from [::status/single ::form]
:class "is-primary"
:on-click (dispatch-event [::try-save])
:can-submit [::can-submit]
:close-event [::status/completed ::form]}}]
:db (-> db
(forms/start-form ::form
(forms/start-form ::form
{:expense-accounts (by :id
(:expense-accounts i))
:invoice i}))}))
{:expense-accounts (by :id
(:expense-accounts i))
:invoice i}))}))

View File

@@ -5,7 +5,7 @@
[react :as react]))
(def good-$ #"^\-?[0-9]+(\.[0-9][0-9])?$")
(defn -money-field [{:keys [min max disabled on-change value class style]}]
(defn -money-field [{:keys [min max disabled on-change value class style placeholder]}]
(let [[ parsed-amount set-parsed-amount] (react/useState {:parsed value
:raw (cond
(str/blank? value)
@@ -40,6 +40,7 @@
[:div.control.has-icons-left
[:input.input {:type "text"
:disabled disabled
:placeholder placeholder
:class class
:on-change (fn [e]
(let [raw (.. e -target -value)

View File

@@ -119,7 +119,9 @@
[:div.level-item
[:div.control
[:div.tags.has-addons
[:span.tag (:name selectedItem)]
[:span.tag (if entity->text
(entity->text selectedItem)
(:name selectedItem))]
(when name
[:input {:type "hidden" :name name :value (:id (js->clj selectedItem :keywordize-keys true))}])
(when-not disabled