From f40ff431dddb2ca65b51ee4fbd8edd3f8311b746 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Wed, 2 Sep 2020 07:57:56 -0700 Subject: [PATCH] progress on making saving work better for accounts. --- src/clj/auto_ap/datomic/accounts.clj | 6 +- src/clj/auto_ap/graphql/accounts.clj | 12 +- src/cljs/auto_ap/events.cljs | 2 +- src/cljs/auto_ap/forms.cljs | 2 + .../auto_ap/views/components/typeahead.cljs | 3 +- .../views/pages/admin/accounts/form.cljs | 133 ++++++++---------- src/cljs/auto_ap/views/utils.cljs | 39 ++--- 7 files changed, 100 insertions(+), 97 deletions(-) diff --git a/src/clj/auto_ap/datomic/accounts.clj b/src/clj/auto_ap/datomic/accounts.clj index 9997e5b4..db3aa711 100644 --- a/src/clj/auto_ap/datomic/accounts.clj +++ b/src/clj/auto_ap/datomic/accounts.clj @@ -11,7 +11,8 @@ ([args] (let [query (cond-> {:query {:find ['(pull ?e [* {:account/type [:db/ident :db/id] :account/applicability [:db/ident :db/id] - :account/client-overrides [:account-client-override/name + :account/client-overrides [:db/id + :account-client-override/name {:account-client-override/client [:db/id :client/name]}]}])] :in ['$] :where [['?e :account/name]]} @@ -27,7 +28,8 @@ (defn get-by-id [id] (let [query {:query {:find ['(pull ?e [* {:account/type [:db/ident :db/id] :account/applicability [:db/ident :db/id] - :account/client-overrides [:account-client-override/name + :account/client-overrides [:db/id + :account-client-override/name {:account-client-override/client [:db/id :client/name]}]}])] :in ['$ '?e]} :args [(d/db (d/connect uri) ) id]}] diff --git a/src/clj/auto_ap/graphql/accounts.clj b/src/clj/auto_ap/graphql/accounts.clj index 463beaca..9b4d80b4 100644 --- a/src/clj/auto_ap/graphql/accounts.clj +++ b/src/clj/auto_ap/graphql/accounts.clj @@ -2,7 +2,8 @@ (:require [datomic.api :as d] [auto-ap.datomic.accounts :as d-accounts] [auto-ap.graphql.utils :refer [->graphql <-graphql enum->keyword] ] - [auto-ap.datomic :refer [uri merge-query remove-nils]])) + [auto-ap.datomic :refer [uri merge-query remove-nils audit-transact]] + [clojure.tools.logging :as log])) @@ -22,8 +23,7 @@ {} )))) (let [original (if id (d/entity (d/db (d/connect uri)) id)) - result @(d/transact (d/connect uri) - (cond-> + result (audit-transact (cond-> [(remove-nils {:db/id (or id "new-account") :account/name name @@ -39,10 +39,12 @@ (mapv (fn [client-override] (remove-nils - {:account-client-override/client (:client-id client-override) + {:db/id (:id client-override) + :account-client-override/client (:client-id client-override) :account-client-override/name (:name client-override)})) client-overrides)]] - (and (not location) (:account/location original)) (conj [:db/retract (or id "new-account") :account/location (:account/location original)])))] + (and (not location) (:account/location original)) (conj [:db/retract (or id "new-account") :account/location (:account/location original)])) + (:id context))] (->graphql (d-accounts/get-by-id (or id (get-in result [:tempids "new-account"]))))))) diff --git a/src/cljs/auto_ap/events.cljs b/src/cljs/auto_ap/events.cljs index 392171fd..864ea1c4 100644 --- a/src/cljs/auto_ap/events.cljs +++ b/src/cljs/auto_ap/events.cljs @@ -60,7 +60,7 @@ [:forecasted-transactions [:id :amount :identifier :day-of-month]]]] [:vendor vendor-query] - [:accounts [:numeric-code :location :name :type :account_set :applicability :id [:client-overrides [:name [:client [:name :id]]]]]]]} + [:accounts [:numeric-code :location :name :type :account_set :applicability :id [:client-overrides [:name :id [:client [:name :id]]]]]]]} :on-success [::received-initial]}})))) diff --git a/src/cljs/auto_ap/forms.cljs b/src/cljs/auto_ap/forms.cljs index a7626aa3..2fd6837d 100644 --- a/src/cljs/auto_ap/forms.cljs +++ b/src/cljs/auto_ap/forms.cljs @@ -74,6 +74,7 @@ (re-frame/reg-event-db ::change (fn [db [_ form & path-pairs]] + (reduce (fn [db [path value]] (assoc-in db (into [::forms form :data] path) value)) @@ -82,6 +83,7 @@ (defn change-handler [form customize-fn] (fn [db [_ & path-pairs]] + (reduce (fn [db [path value]] (let [updated (assoc-in db (into [::forms form :data] path) value)] diff --git a/src/cljs/auto_ap/views/components/typeahead.cljs b/src/cljs/auto_ap/views/components/typeahead.cljs index be501231..19814606 100644 --- a/src/cljs/auto_ap/views/components/typeahead.cljs +++ b/src/cljs/auto_ap/views/components/typeahead.cljs @@ -47,7 +47,8 @@ [:div.control [:div.tags.has-addons [:span.tag text] - [:a.tag.is-delete {:on-click (fn [] (select [nil "" nil]))}]]]] + [:a.tag.is-delete {:on-click (fn [] + (select [nil "" nil]))}]]]] ^{:key "typeahead"} [:input.input {:type "text" :class class 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 37b9b0f6..81f0f571 100644 --- a/src/cljs/auto_ap/views/pages/admin/accounts/form.cljs +++ b/src/cljs/auto_ap/views/pages/admin/accounts/form.cljs @@ -1,12 +1,12 @@ (ns auto-ap.views.pages.admin.accounts.form - (:require [auto-ap.forms :as forms] - [clojure.string :as str] - [clojure.spec.alpha :as s] + (:require [auto-ap.entities.account :as entity] + [auto-ap.forms :as forms] [auto-ap.subs :as subs] - [auto-ap.entities.account :as entity] [auto-ap.views.components.layouts :refer [side-bar]] - [auto-ap.views.utils :refer [bind-field dispatch-event]] [auto-ap.views.components.typeahead :refer [typeahead-entity]] + [auto-ap.views.utils :refer [dispatch-event multi-field]] + [clojure.spec.alpha :as s] + [clojure.string :as str] [re-frame.core :as re-frame])) (def types [:dividend :expense :asset :liability :equity :revenue]) @@ -28,7 +28,8 @@ :name name :account-set account-set :client-overrides (map (fn [client-override] - {:client-id (:id (:client client-override)) + {:id (:id client-override) + :client-id (:id (:client client-override)) :name (if (str/blank? (:name client-override)) nil (:name client-override))}) @@ -43,6 +44,7 @@ (re-frame/reg-event-db ::editing (fn [db [_ which complete-listener]] + (println "WHICH" which) (-> db (forms/start-form ::form which complete-listener)))) @@ -72,7 +74,7 @@ :operation/name "UpsertAccount"} :venia/queries [{:query/data [:upsert-account {:account @(re-frame/subscribe [::request])} - [:id :type :name :account-set :numeric-code :location :applicability [:client-overrides [:name [:client [:id :name]]]]]]}]} + [: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]}})))) @@ -83,78 +85,67 @@ (defn form [_] (let [{error :error account :data } @(re-frame/subscribe [::forms/form ::form]) - {:keys [form field field-holder raw-field error-notification submit-button]} account-form - change-event [::forms/change ::form]] + {:keys [form-inline field field-holder raw-field error-notification submit-button]} account-form] ^{:key (:id account)} [side-bar {:on-close (dispatch-event [::forms/form-closing ::form])} - [form {:title (if (:id account) - "Edit account" - "Add account")} + (form-inline {:title (if (:id account) + "Edit account" + "Add account")} + [:<> - [field "Account Set" - [:input.input {:type "text" - :field :account-set - :disabled (boolean (:id account)) - :spec ::entity/account-set}]] - + (field "Account Set" + [:input.input {:type "text" + :field :account-set + :disabled (boolean (:id account)) + :spec ::entity/account-set}]) - [field "Code" - [:input.input {:type "text" - :field :numeric-code - :disabled (boolean (:id account)) - :spec ::entity/numeric-code}]] - + (field "Code" + [:input.input {:type "text" + :field :numeric-code + :disabled (boolean (:id account)) + :spec ::entity/numeric-code}]) - [field "Name" - [:input.input {:type "text" - :field :name - :spec ::entity/name}]] + (field "Name" + [:input.input {:type "text" + :field :name + :spec ::entity/name}]) - - [field-holder "Account Type" - [:div.select - [raw-field - [:select {:type "select" - :field :type - :spec (set types)} - (map (fn [l] - [:option {:value (name l)} (str/capitalize (name l))]) types)]]]] + (field-holder "Account Type" + [:div.select + (raw-field + [:select {:type "select" + :field :type + :spec (set types)} + (map (fn [l] + [:option {:value (name l)} (str/capitalize (name l))]) types)])]) - [field "Location" - [:input.input.known-field.location {:type "text" - :field :location - :spec ::entity/location}]] + (field "Location" + [:input.input.known-field.location {:type "text" + :field :location + :spec ::entity/location}]) - [:h2.subtitle "Client"] - [field-holder "Applicability" - [:div.select - [raw-field - [:select {:type "select" - :field :applicability - :spec (set applicabilities)} - (map (fn [l] - [:option {:value (name l)} (str/capitalize (name l))]) applicabilities)]]]] - [field-holder "Customizations" - [:div.field.has-addons - [:p.control - ^{:key (count (:client-overrides account))} ;; resets after adding - [raw-field - [typeahead-entity {:matches @(re-frame/subscribe [::subs/clients]) - :match->text :name - :type "typeahead" - :field [:new-client-override :client]}]]] - [:p.control - [raw-field - [:input.input {:type "text" - :placeholder "Bubblegum" - :field [:new-client-override :name]}]]] - [:p.control [:button.button.is-primary {:on-click (dispatch-event [::add-client-override])} "Add"]]]] - [:ul - (for [client-override (:client-overrides account)] - ^{:key (:name client-override)} - [:li (:name (:client client-override)) "-" (:name client-override)])] - [error-notification] + [:h2.subtitle "Client"] + (field-holder "Applicability" + [:div.select + (raw-field + [:select {:type "select" + :field :applicability + :spec (set applicabilities)} + (map (fn [l] + [:option {:value (name l)} (str/capitalize (name l))]) applicabilities)])]) + (field "Customizations" + [multi-field {:type "multi-field" + :field [:client-overrides] + :template [[typeahead-entity {:matches @(re-frame/subscribe [::subs/clients]) + :match->text :name + :type "typeahead" + :field [:client]}] + [:input.input {:type "text" + :placeholder "Bubblegum" + :field [:name]}] + ]}]) + (error-notification) - [submit-button "Save"]]])) + (submit-button "Save")])])) diff --git a/src/cljs/auto_ap/views/utils.cljs b/src/cljs/auto_ap/views/utils.cljs index 8cdaa94a..5e857098 100644 --- a/src/cljs/auto_ap/views/utils.cljs +++ b/src/cljs/auto_ap/views/utils.cljs @@ -116,12 +116,12 @@ [:span])]))) -(defn multi-field [{:keys [override-key override-value-key change-event default-key data value template on-change allow-change?]} ] +(defn multi-field [{:keys [change-event data value template on-change allow-change?]} ] (let [value-repr (r/atom (mapv (fn [x] (assoc x :key (random-uuid) :new? false)) value))] - (fn [{:keys [override-key override-value-key change-event default-key data value template on-change allow-change?]} ] + (fn [{:keys [change-event data value template on-change allow-change?]} ] (let [value @value-repr already-has-new-row? (= [:key :new?] (keys (last value))) value (if already-has-new-row? @@ -147,20 +147,22 @@ ^{:key idx} [:div.level-item - [update template 1 assoc - :value (get-in override (get-in template [1 :field])) - :disabled is-disabled? - :on-change (fn [e] - - (reset! value-repr - (into [] - (filter (fn [r] - (not= [:key :new?] (keys r))) - (assoc-in value (into [i] (get-in template [1 :field])) (.. e -target -value ) )))) - (on-change (mapv - (fn [v] - (dissoc v :new? :key)) - @value-repr)))]]) + (update template 1 assoc + :value (get-in override (get-in template [1 :field])) + :disabled is-disabled? + :on-change (fn [e] + (reset! value-repr + (into [] + (filter (fn [r] + (not= [:key :new?] (keys r))) + (assoc-in value (into [i] (get-in template [1 :field])) + (if (and e (.. e -target)) + (.. e -target -value ) + e) )))) + (on-change (mapv + (fn [v] + (dissoc v :new? :key)) + @value-repr))))]) ] [:div.level-item [:a.button.level-item @@ -170,9 +172,12 @@ (when-not is-disabled? (reset! value-repr (into [] + (filter (fn [{:keys [key ] :as v}] (not= key (:key override))) - value))) + (filter (fn [r] + (not= [:key :new?] (keys r))) + value)))) (on-change (mapv (fn [v]