From 3c11891c4551ce24106bc336742356a34b795372 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Fri, 22 Jul 2022 06:59:37 -0700 Subject: [PATCH] Another improvement --- src/cljs/auto_ap/schema.cljs | 5 +- src/cljs/auto_ap/views/components.cljs | 18 ++- .../auto_ap/views/pages/admin/accounts.cljs | 3 +- .../views/pages/admin/accounts/form.cljs | 136 +++++++++--------- src/cljs/auto_ap/views/utils.cljs | 2 +- 5 files changed, 87 insertions(+), 77 deletions(-) diff --git a/src/cljs/auto_ap/schema.cljs b/src/cljs/auto_ap/schema.cljs index 64a276e0..58b76f55 100644 --- a/src/cljs/auto_ap/schema.cljs +++ b/src/cljs/auto_ap/schema.cljs @@ -12,8 +12,9 @@ (def money (m/schema [float? {:error/message "Invalid money"}])) (def not-empty-string (m/schema [:re {:error/message "Required"} #"\S+"])) (def code-string (m/schema [:re #"[A-Z0-9\-]+"])) -(def keyword (m/schema [:fn (fn [d] - (keyword? d))])) + +(def positive-integer (m/schema [:int {:min 1}])) +(def integer-code (m/schema [:int {:min 10000 :max 99999}])) (def expense-account (m/schema [:map [:id :string] diff --git a/src/cljs/auto_ap/views/components.cljs b/src/cljs/auto_ap/views/components.cljs index 95b93d8c..6e25a3aa 100644 --- a/src/cljs/auto_ap/views/components.cljs +++ b/src/cljs/auto_ap/views/components.cljs @@ -22,14 +22,26 @@ ] (r/children (r/current-component)))) -(defn select-field [{:keys [options allow-nil? class] :as props}] +(defn select-field [{:keys [options allow-nil? class on-change keywordize?] :as props}] [:div.select {:class class} [:select (-> props (dissoc :allow-nil? :class :options) (update :value (fn [v] - (if (str/blank? v) + (cond (str/blank? v) "" - v)))) + + keywordize? + (name v) + + :else + v))) + (assoc :on-change + (fn [e] + (println "VALUE IS" (keyword (.. e -target -value))) + (if keywordize? + (on-change (keyword (.. e -target -value))) + (on-change e)))) + (dissoc :keywordize?)) [:<> (when allow-nil? [:option {:value nil}]) diff --git a/src/cljs/auto_ap/views/pages/admin/accounts.cljs b/src/cljs/auto_ap/views/pages/admin/accounts.cljs index 4bc7af25..45be819d 100644 --- a/src/cljs/auto_ap/views/pages/admin/accounts.cljs +++ b/src/cljs/auto_ap/views/pages/admin/accounts.cljs @@ -73,8 +73,7 @@ [buttons/new-button {:name "Account" :class "is-primary" :event [::account-form/editing - {:type :asset - :account-set "default"}]}]] + {:account-set "default"}]}]] [table/accounts-table {:data-page ::page}]]) (defn admin-accounts-page [] diff --git a/src/cljs/auto_ap/views/pages/admin/accounts/form.cljs b/src/cljs/auto_ap/views/pages/admin/accounts/form.cljs index 33871559..12df4343 100644 --- a/src/cljs/auto_ap/views/pages/admin/accounts/form.cljs +++ b/src/cljs/auto_ap/views/pages/admin/accounts/form.cljs @@ -9,7 +9,10 @@ [clojure.string :as str] [re-frame.core :as re-frame] [auto-ap.forms.builder :as form-builder] - [vimsical.re-frame.cofx.inject :as inject])) + [vimsical.re-frame.cofx.inject :as inject] + [auto-ap.views.components :as com] + [malli.core :as m] + [auto-ap.schema :as schema])) (def types [:dividend :expense :asset :liability :equity :revenue]) (def applicabilities [:global :optional :customized]) @@ -35,11 +38,7 @@ (:name client-override))}) client-overrides)})) -(re-frame/reg-sub - ::can-submit - :<- [::request] - (fn [request] - (s/valid? ::entity/account request))) + (re-frame/reg-event-db ::editing @@ -58,92 +57,91 @@ ::saving [with-user (re-frame/inject-cofx ::inject/sub [::request]) ] (fn [{:keys [user] ::keys [request]} _] - (when @(re-frame/subscribe [::can-submit]) - (let [_ @(re-frame/subscribe [::forms/form ::form])] - {:graphql - {:owns-state {:single ::form} - :token user - :query-obj {:venia/operation {:operation/type :mutation - :operation/name "UpsertAccount"} - :venia/queries [{:query/data [:upsert-account - {:account request} - [:id :type :name :account-set :numeric-code :location :applicability [:client-overrides [:name :id [:client [:id :name]]]]]]}]} - :on-success [::edited] - :on-error [::forms/save-error ::form]}})))) + (let [_ @(re-frame/subscribe [::forms/form ::form])] + {:graphql + {:owns-state {:single ::form} + :token user + :query-obj {:venia/operation {:operation/type :mutation + :operation/name "UpsertAccount"} + :venia/queries [{:query/data [:upsert-account + {:account request} + [:id :type :name :account-set :numeric-code :location :applicability [:client-overrides [:name :id [:client [:id :name]]]]]]}]} + :on-success [::edited] + :on-error [::forms/save-error ::form]}}))) +(def account-customization-schema + (m/schema + [:map [:client schema/reference] + [:name schema/not-empty-string]])) + +(def account-schema + (m/schema + [:map + [:numeric-code schema/integer-code] + [:name schema/not-empty-string] + [:type [:enum :dividend :expense :asset :liability :equity :revenue]] + [:location {:optional true} [:maybe :string]] + [:applicability [:enum :global :optional :customized]] + [:client-overrides {:optional true} + [:maybe [:sequential account-customization-schema]]]])) (defn form [_] (let [{account :data } @(re-frame/subscribe [::forms/form ::form])] [side-bar {:on-close (dispatch-event [::forms/form-closing ::form])} - [form-builder/builder {:can-submit [::can-submit] - :change-event [::forms/change ::form] + [form-builder/builder {:change-event [::forms/change ::form] :submit-event [::saving] - :id ::form} + :id ::form + :schema account-schema} [form-builder/section {:title (if (:id account) "Edit account" "Add account")} - [form-builder/field - "Account Set" - [:input.input {:type "text" - :field :account-set - :disabled (boolean (:id account)) - :spec ::entity/account-set}]] - - [form-builder/field + [form-builder/field-v2 {:field :numeric-code} "Code" - [:input.input {:type "text" - :field :numeric-code - :disabled (boolean (:id account)) - :spec ::entity/numeric-code}]] + [com/number-input + {:disabled (boolean (:id account)) + :auto-focus (not (boolean (:id account))) + :style {:width "9em"}}]] - [form-builder/field + [form-builder/field-v2 {:field :name} "Name" [:input.input {:type "text" - :field :name - :spec ::entity/name}]] + :auto-focus (boolean (:id account))}]] - [form-builder/vertical-control + [form-builder/field-v2 {:field :type} "Account Type" - [:div.select - [form-builder/raw-field - [:select {:type "select" - :field :type - :spec (set types)} - (map (fn [l] - [:option {:value (name l)} (str/capitalize (name l))]) types)]]]] + [com/select-field {:options (map (fn [l] + [l (str/capitalize (name l))]) + types) + :allow-nil? true + :keywordize? true}]] - [form-builder/field + [form-builder/field-v2 {:field :location} "Location" - [:input.input.known-field.location {:type "text" - :field :location - :spec ::entity/location}]] + [:input.input.known-field.location {:type "text"}]] [form-builder/section {:title "Client"} [:h2.subtitle "Client"] - [form-builder/vertical-control + [form-builder/field-v2 {:field :applicability} "Applicability" - [:div.select - [form-builder/raw-field - [:select {:type "select" - :field :applicability - :spec (set applicabilities)} - (map (fn [l] - [:option {:value (name l)} (str/capitalize (name l))]) applicabilities)]]]] - [form-builder/field + [com/select-field {:options (map (fn [l] + [l + (str/capitalize (name l))]) + applicabilities) + :allow-nil? true + :keywordize? true}]] + [form-builder/field-v2 {:field :client-overrides} "Customizations" - [multi-field {:type "multi-field" - :field [:client-overrides] - :template [[typeahead-v3 {:entities @(re-frame/subscribe [::subs/clients]) - :style {:width "13em"} - :entity->text :name - :type "typeahead-v3" - :field [:client]}] - [:input.input {:type "text" - :style {:width "15em"} - :placeholder "Bubblegum" - :field [:name]}] - ]}]]] + [com/multi-field-v2 {:template [[form-builder/raw-field-v2 {:field :client} + [typeahead-v3 {:entities @(re-frame/subscribe [::subs/clients]) + :style {:width "13em"} + :entity->text :name}]] + [form-builder/raw-field-v2 {:field :name} + [:input.input {:type "text" + :style {:width "15em"} + :placeholder "Bubblegum"}]]] + :key-fn :id + :schema [:sequential account-customization-schema]}]]] [form-builder/error-notification] [form-builder/submit-button "Save"]]]])) diff --git a/src/cljs/auto_ap/views/utils.cljs b/src/cljs/auto_ap/views/utils.cljs index 1814ce76..7cb88925 100644 --- a/src/cljs/auto_ap/views/utils.cljs +++ b/src/cljs/auto_ap/views/utils.cljs @@ -63,7 +63,7 @@ (def login-url (let [client-id "264081895820-0nndcfo3pbtqf30sro82vgq5r27h8736.apps.googleusercontent.com" redirect-uri (js/encodeURI (str (.-origin (.-location js/window)) "/api/oauth"))] - (str "https://accounts.google.com/o/oauth2/auth?access_type=online&client_id=" client-id "&redirect_uri=" redirect-uri "&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile"))) + (str "https://accounts.google.com/o/oauth2/auth?access_type=online&client_id=" client-id "&redirect_uri=" redirect-uri "&response_type=code&max_auth_age=0&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile"))) (defn dispatch-value-change [event] (fn [e]