more migrating onto v2

This commit is contained in:
2022-07-21 12:55:55 -07:00
parent 22a5f2a707
commit 1d0cdcaf88
6 changed files with 122 additions and 85 deletions

View File

@@ -2,7 +2,8 @@
(:require [re-frame.core :as re-frame] (:require [re-frame.core :as re-frame]
[re-frame.interceptor :as i] [re-frame.interceptor :as i]
[auto-ap.status :as status] [auto-ap.status :as status]
[auto-ap.views.utils :refer [dispatch-event bind-field]])) [auto-ap.views.utils :refer [dispatch-event bind-field]]
[malli.core :as m]))
(re-frame/reg-sub (re-frame/reg-sub
@@ -85,6 +86,14 @@
(fn [db [_ form & paths]] (fn [db [_ form & paths]]
(update-in db [::forms form :visited] (fn [v] (update-in db [::forms form :visited] (fn [v]
(set (into v paths)))))) (set (into v paths))))))
(re-frame/reg-event-db
::check-problems
(fn [db [_ form schema]]
(println "Checking problems")
(assoc-in db [::forms form :problems]
(when schema (m/explain schema (get-in db [::forms form :data]))))))
(re-frame/reg-event-db (re-frame/reg-event-db
::attempted-submit ::attempted-submit
(fn [db [_ form & paths]] (fn [db [_ form & paths]]

View File

@@ -41,27 +41,29 @@
(apply f (for [field fields] (apply f (for [field fields]
(aget consumed field)))))]) (aget consumed field)))))])
(re-frame/reg-event-fx
::blurred
(fn [_ [_ schema id field]]
{:dispatch-n [[::forms/check-problems id schema]
[::forms/visited id]]}))
(defn builder [{:keys [value on-change can-submit data-sub error-messages change-event submit-event id fullwidth? schema validation-error-string]}] (defn builder [{:keys [value on-change can-submit data-sub error-messages change-event submit-event id fullwidth? schema validation-error-string]}]
(when (and change-event on-change) (when (and change-event on-change)
(throw "Either the form is to be managed by ::forms, or it should have value and on-change passed in")) (throw "Either the form is to be managed by ::forms, or it should have value and on-change passed in"))
(let [data-sub (or data-sub [::forms/form id]) (let [data-sub (or data-sub [::forms/form id])
change-event (when-not on-change change-event (when-not on-change
(or change-event [::forms/change id])) (or change-event [::forms/change id]))
{:keys [data visited attempted-submit?] form-key :id} @(re-frame/subscribe data-sub) {:keys [data visited attempted-submit? problems] form-key :id} @(re-frame/subscribe data-sub)
data (or value data) data (or value data)
status @(re-frame/subscribe [::status/single id]) status @(re-frame/subscribe [::status/single id])
can-submit (if can-submit @(re-frame/subscribe can-submit) can-submit (if can-submit @(re-frame/subscribe can-submit)
true) true)]
;; TODO ONLY validate on blur
problems (when schema
(m/explain schema data))]
(r/create-element Provider #js {:value #js {:can-submit can-submit (r/create-element Provider #js {:value #js {:can-submit can-submit
:error-messages (or error-messages :error-messages (or error-messages
nil) nil)
:on-change on-change :on-change on-change
:change-event change-event :change-event change-event
:blur-event [::forms/visited id] :blur-event [::blurred schema id]
:visited visited :visited visited
:submit-event submit-event :submit-event submit-event
:problems problems :problems problems
@@ -92,17 +94,14 @@
(let [key (r/atom (random-uuid))] (let [key (r/atom (random-uuid))]
(fn [{:keys [value on-change can-submit error-messages fullwidth? schema]}] (fn [{:keys [value on-change can-submit error-messages fullwidth? schema]}]
(let [data-sub [::forms/form @key] (let [data-sub [::forms/form @key]
{:keys [data error visited]} @(re-frame/subscribe data-sub) {:keys [data error problems visited]} @(re-frame/subscribe data-sub)
data (or value data) data (or value data)]
problems (when schema
(m/explain schema data))]
(r/create-element Provider #js {:value #js {:can-submit can-submit (r/create-element Provider #js {:value #js {:can-submit can-submit
:error-messages (or error-messages :error-messages (or error-messages
nil) nil)
:on-change on-change :on-change on-change
:blur-event [::forms/visited @key] :blur-event [::blurred schema @key]
:visited visited :visited visited
:problems problems
:error error :error error
:id @key :id @key
:data data :data data
@@ -148,7 +147,10 @@
event-or-value)) event-or-value))
data)) data))
(defn blur-handler [path re-frame-blur-event _] (defn blur-handler [path re-frame-blur-event original-on-blur e]
(when original-on-blur
(original-on-blur e))
(re-frame/dispatch (-> re-frame-blur-event (re-frame/dispatch (-> re-frame-blur-event
(conj path)))) (conj path))))
@@ -159,7 +161,8 @@
(consume FormScopeConsumer (consume FormScopeConsumer
["scope"] ["scope"]
(fn [scope] (fn [scope]
(let [full-field-path (cond (let [scope (or scope [])
full-field-path (cond
(sequential? field) (sequential? field)
(into scope field) (into scope field)
@@ -186,7 +189,7 @@
["scope"] ["scope"]
(fn [scope] (fn [scope]
(update child 1 (fn [child-props] (update child 1 (fn [child-props]
(let [ (let [scope (or scope [])
full-field-path (cond full-field-path (cond
(sequential? field) (sequential? field)
(into scope field) (into scope field)
@@ -203,7 +206,7 @@
(if on-change (if on-change
(partial form-change-handler data full-field-path on-change) (partial form-change-handler data full-field-path on-change)
(partial change-handler full-field-path change-event)) (partial change-handler full-field-path change-event))
:on-blur (partial blur-handler full-field-path blur-event) :on-blur (partial blur-handler full-field-path blur-event (:on-blur child-props))
:value value) :value value)
(update :class (fn [class] (update :class (fn [class]
(str class (str class

View File

@@ -10,7 +10,8 @@
[clojure.string :as str] [clojure.string :as str]
[goog.string :as gstring] [goog.string :as gstring]
[re-frame.core :as re-frame] [re-frame.core :as re-frame]
[auto-ap.forms.builder :as form-builder])) [auto-ap.forms.builder :as form-builder]
[auto-ap.views.components :as com]))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::try-save ::try-save
@@ -28,6 +29,7 @@
%)) %))
(reduce + 0)) (reduce + 0))
does-add-up? (< (Math/abs (- expense-accounts-total (js/parseFloat total))) 0.001)] does-add-up? (< (Math/abs (- expense-accounts-total (js/parseFloat total))) 0.001)]
(if (and does-add-up? (if (and does-add-up?
(every? :new-amount expense-accounts)) (every? :new-amount expense-accounts))
@@ -113,14 +115,12 @@
^{:key id} ^{:key id}
[:tr [:tr
[:td.expandable [:div.control [:td.expandable [:div.control
[form-builder/raw-field [form-builder/raw-field-v2 {:field [:expense-accounts id :account]}
[search-backed-typeahead {:search-query (fn [i] [search-backed-typeahead {:search-query (fn [i]
[:search_account [:search_account
{:query i {:query i
:client-id (:id client)} :client-id (:id client)}
[:name :id :location]]) [:name :id :location]])}]]]]
:type "typeahead-v3"
:field [:expense-accounts id :account]}]]]]
(when multi-location? (when multi-location?
[:td [:td
@@ -129,11 +129,10 @@
[:select {:disabled "disabled" :value forced-location} [:option {:value forced-location} forced-location]]] [:select {:disabled "disabled" :value forced-location} [:option {:value forced-location} forced-location]]]
[:div.select [:div.select
[form-builder/raw-field [form-builder/raw-field-v2 {:field [:expense-accounts id :location]}
[:select {:type "select" [com/select-field {:options (map (fn [l] [l l])
:field [:expense-accounts id :location] locations)
:spec (set locations)} :allow-nil? true}]]])])
(map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])])
[:td [:td
(str "$" (get-in expense-accounts [id :amount]))] (str "$" (get-in expense-accounts [id :amount]))]
@@ -142,9 +141,8 @@
[:div.field.has-addons.is-extended [:div.field.has-addons.is-extended
[:p.control [:a.button.is-static "$"]] [:p.control [:a.button.is-static "$"]]
[:p.control [:p.control
[form-builder/raw-field [form-builder/raw-field-v2 {:field [:expense-accounts id :new-amount-temp]}
[:input.input {:type "number" [:input.input {:type "number"
:field [:expense-accounts id :new-amount-temp]
:style {:text-align "right"} :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-blur (dispatch-event [::forms/change ::form [:expense-accounts id :new-amount] (get-in expense-accounts [id :new-amount-temp])])
:on-key-down (fn [e ] :on-key-down (fn [e ]

View File

@@ -13,7 +13,6 @@
(let [prop-value (if (seq prop-value) (let [prop-value (if (seq prop-value)
prop-value prop-value
[])] [])]
(println "PROP VALUE IS")
[form-builder/virtual-builder {:value prop-value [form-builder/virtual-builder {:value prop-value
:schema schema :schema schema
:on-change on-change} :on-change on-change}
@@ -69,7 +68,7 @@
(defn multi-field-v2 [] (defn multi-field-v2 []
(into (into
[:f> multi-field-v2-internal [:f> multi-field-v2-internal
(reagent/props (reagent/current-component))] (reagent/props (reagent/current-component))]
(reagent/children (reagent/current-component)))) (reagent/children (reagent/current-component))))

View File

@@ -34,6 +34,14 @@
(def location-schema (m/schema [:map (def location-schema (m/schema [:map
[:location schema/not-empty-string]])) [:location schema/not-empty-string]]))
(def square-location-schema (m/schema [:map
[:square-location schema/reference]
[:client-location schema/not-empty-string]]))
(def ezcater-schema (m/schema [:map
[:caterer schema/reference]
[:client-location schema/not-empty-string]]))
(def name-match-schema (m/schema [:map (def name-match-schema (m/schema [:map
[:match schema/not-empty-string]])) [:match schema/not-empty-string]]))
(def location-match-schema (m/schema [:map (def location-match-schema (m/schema [:map
@@ -52,7 +60,9 @@
[:matches {:optional true} [:matches {:optional true}
[:maybe [:sequential name-match-schema]]] [:maybe [:sequential name-match-schema]]]
[:location-matches {:optional true} [:location-matches {:optional true}
[:maybe [:sequential location-match-schema]]]]) [:maybe [:sequential location-match-schema]]]
[:selected-square-locations {:optional true}
[:maybe [:sequential square-location-schema]]]])
(defn upload-replacement-button [{:keys [on-change]} text] (defn upload-replacement-button [{:keys [on-change]} text]
(let [button (atom nil)] (let [button (atom nil)]
@@ -486,7 +496,7 @@
[:div.control [:div.control
[:p.help "If this account is location-specific, add the valid locations"] [:p.help "If this account is location-specific, add the valid locations"]
[form-builder/raw-field-v2 {:field :locations} [form-builder/raw-field-v2 {:field :locations}
[com/multi-field-v2 {:template [[form-builder/raw-field {:key :location} [com/multi-field-v2 {:template [[form-builder/raw-field-v2 {:field :location}
[com/select-field {:options (map (fn [l] [com/select-field {:options (map (fn [l]
[(:location l) (:location l)]) [(:location l) (:location l)])
(get-in new-client [:locations])) (get-in new-client [:locations]))
@@ -650,45 +660,49 @@
(defn square-section [] (defn square-section []
(let [{new-client :data} @(re-frame/subscribe [::forms/form ::form])] (let [{new-client :data} @(re-frame/subscribe [::forms/form ::form])]
[form-builder/section {:title "Square Integration"} [form-builder/section {:title "Square Integration"}
[form-builder/field "Square Authentication Token" [form-builder/field-v2 {:field :square-auth-token}
"Square Authentication Token"
[:input.input {:type "text" [:input.input {:type "text"
:style {:width "40em"} :style {:width "40em"}}]]
:field [:square-auth-token]}]] [form-builder/field-v2 {:field :selected-square-locations}
[form-builder/field
"Square Locations" "Square Locations"
[multi-field {:type "multi-field" [com/multi-field-v2 {:template [[form-builder/raw-field-v2 {:field :square-location}
:field :selected-square-locations [typeahead-v3 {:entities (:square-locations new-client)
:template [[typeahead-v3 {:entities (:square-locations new-client) :entity->text :name
:entity->text :name :style {:width "15em"}}]]
:type "typeahead-v3" [form-builder/raw-field-v2 {:field :client-location}
:style {:width "15em"} [com/select-field {:options (map (fn [l]
:field [:square-location]}] [(:location l) (:location l)])
[:input.input {:type "text" (get-in new-client [:locations]))
:style {:width "4em"} :allow-nil? true
:field [:client-location] :style {:width "7em"}
:step "0.01"}]] }]]]
:disable-remove? true}]]])) :disable-remove? true
:key-fn :id
:schema [:sequential square-location-schema]}]]]))
(defn ezcater-section [] (defn ezcater-section []
[form-builder/section {:title "EZCater integration"} (let [{new-client :data} @(re-frame/subscribe [::forms/form ::form])]
[form-builder/section {:title "EZCater integration"}
[form-builder/field [form-builder/field-v2 {:field :ezcater-locations}
"EZCater Locations" "EZCater Locations"
[multi-field {:type "multi-field" [com/multi-field-v2 {:template [[form-builder/raw-field-v2 {:field :caterer}
:field :ezcater-locations [search-backed-typeahead {:search-query (fn [i]
:template [[search-backed-typeahead {:search-query (fn [i] [:search_ezcater_caterer
[:search_ezcater_caterer {:query i}
{:query i} [:name :id]])
[:name :id]]) :entity->text :name
:entity->text :name :style {:width "20em"}}]]
:type "typeahead-v3" [form-builder/raw-field-v2 {:field [:location]}
:field [:caterer] [com/select-field {:options (map (fn [l]
:style {:width "20em"}}] [(:location l) (:location l)])
[:input.input {:type "text" (get-in new-client [:locations]))
:style {:width "4em"} :allow-nil? true
:field [:location] :style {:width "7em"}}]]]
:step "0.01"}]] :key-fn :id
:disable-remove? true}]]]) :schema [:sequential ezcater-schema]
:disable-remove? true}]]]))
(defn form-content [] (defn form-content []

View File

@@ -9,7 +9,10 @@
:refer [does-amount-exceed-outstanding? invoice-read]] :refer [does-amount-exceed-outstanding? invoice-read]]
[auto-ap.views.components.money-field :refer [money-field]] [auto-ap.views.components.money-field :refer [money-field]]
[auto-ap.views.utils :refer [dispatch-event horizontal-field with-user]] [auto-ap.views.utils :refer [dispatch-event horizontal-field with-user]]
[re-frame.core :as re-frame])) [re-frame.core :as re-frame]
[auto-ap.views.components :as com]
[malli.core :as m]
[auto-ap.schema :as schema]))
(re-frame/reg-sub (re-frame/reg-sub
::can-submit ::can-submit
@@ -32,22 +35,26 @@
:else :else
true))) true)))
(def advanced-print-schema (m/schema
[:map
[:bank-account-id schema/not-empty-string]]))
(defn form [] (defn form []
(let [real-bank-accounts @(re-frame/subscribe [::subs/real-bank-accounts]) (let [real-bank-accounts @(re-frame/subscribe [::subs/real-bank-accounts])
{:keys [data]} @(re-frame/subscribe [::forms/form ::form])] {:keys [data]} @(re-frame/subscribe [::forms/form ::form])]
[form-builder/builder {:submit-event [::save] [form-builder/builder {:submit-event [::try-save]
:can-submit [::can-submit] :can-submit [::can-submit]
:id ::form} :id ::form
:schema advanced-print-schema}
[:div.field [:div.field
[:label.label "Pay using"] [:label.label "Pay using"]
[:div.control [:div.control
[:span.select [:span.select
[form-builder/raw-field [form-builder/raw-field-v2 {:field :bank-account-id}
[:select {:type "select" [com/select-field {:options (for [{:keys [id name]} real-bank-accounts]
:field :bank-account-id} [id name])
(for [{:keys [id name]} real-bank-accounts] :allow-nil? true}]]]]]
^{:key id} [:option {:value id} name])]]]]]
[:table.table.is-fullwidth [:table.table.is-fullwidth
[:thead [:thead
@@ -64,10 +71,8 @@
[:td invoice-number] [:td invoice-number]
[:td [:td
[form-builder/raw-field [form-builder/raw-field-v2 {:field [:invoice-amounts :id :amount]}
[money-field {:type "money" [money-field]]]]))]]]))
:field [:invoice-amounts id :amount]
:step "0.01"}]]]]))]]]))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::show ::show
@@ -77,13 +82,12 @@
:confirm {:value "Print checks" :confirm {:value "Print checks"
:status-from [::status/single ::form] :status-from [::status/single ::form]
:class "is-primary" :class "is-primary"
:on-click (dispatch-event [::save]) :on-click (dispatch-event [::try-save])
:can-submit [::can-submit] :can-submit [::can-submit]
:close-event [::status/completed ::form]}}] :close-event [::status/completed ::form]}}]
:db (-> db :db (-> db
(forms/start-form ::form (forms/start-form ::form
{:bank-account-id (:id (first @(re-frame/subscribe [::subs/real-bank-accounts]))) {:invoices invoices
:invoices invoices
:invoice-amounts (into {} :invoice-amounts (into {}
(map (fn [i] [(:id i) (map (fn [i] [(:id i)
{:amount (:outstanding-balance i)}]) {:amount (:outstanding-balance i)}])
@@ -91,13 +95,23 @@
(re-frame/reg-event-fx
::try-save
[(forms/in-form ::form)]
(fn [{:keys [db]}]
(if (not (m/validate advanced-print-schema (:data db)))
{:dispatch-n [[::status/error ::form [{:message "Please correct any errors and try again"}]]
[::forms/attempted-submit ::form]]}
{:dispatch [::save]})))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::save ::save
[with-user (forms/in-form ::form) ] [with-user (forms/in-form ::form) ]
(fn [{:keys [db user]} [_ bank-account-id]] (fn [{:keys [db user]} _]
(let [type (or (->> @(re-frame/subscribe [::subs/client]) (let [type (or (->> @(re-frame/subscribe [::subs/client])
:bank-accounts :bank-accounts
(filter #(= bank-account-id (:id %))) (filter #(= (:bank-account-id (:data db)) (:id %)))
first first
:type) :type)
:check) :check)