progress on making saving work better for accounts.

This commit is contained in:
Bryce Covert
2020-09-02 07:57:56 -07:00
parent 0297304cfc
commit f40ff431dd
7 changed files with 100 additions and 97 deletions

View File

@@ -11,7 +11,8 @@
([args] ([args]
(let [query (cond-> {:query {:find ['(pull ?e [* {:account/type [:db/ident :db/id] (let [query (cond-> {:query {:find ['(pull ?e [* {:account/type [:db/ident :db/id]
:account/applicability [: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]}]}])] {:account-client-override/client [:db/id :client/name]}]}])]
:in ['$] :in ['$]
:where [['?e :account/name]]} :where [['?e :account/name]]}
@@ -27,7 +28,8 @@
(defn get-by-id [id] (defn get-by-id [id]
(let [query {:query {:find ['(pull ?e [* {:account/type [:db/ident :db/id] (let [query {:query {:find ['(pull ?e [* {:account/type [:db/ident :db/id]
:account/applicability [: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]}]}])] {:account-client-override/client [:db/id :client/name]}]}])]
:in ['$ '?e]} :in ['$ '?e]}
:args [(d/db (d/connect uri) ) id]}] :args [(d/db (d/connect uri) ) id]}]

View File

@@ -2,7 +2,8 @@
(:require [datomic.api :as d] (:require [datomic.api :as d]
[auto-ap.datomic.accounts :as d-accounts] [auto-ap.datomic.accounts :as d-accounts]
[auto-ap.graphql.utils :refer [->graphql <-graphql enum->keyword] ] [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)) (let [original (if id (d/entity (d/db (d/connect uri)) id))
result @(d/transact (d/connect uri) result (audit-transact (cond->
(cond->
[(remove-nils [(remove-nils
{:db/id (or id "new-account") {:db/id (or id "new-account")
:account/name name :account/name name
@@ -39,10 +39,12 @@
(mapv (mapv
(fn [client-override] (fn [client-override]
(remove-nils (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)})) :account-client-override/name (:name client-override)}))
client-overrides)]] 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 (->graphql
(d-accounts/get-by-id (or id (get-in result [:tempids "new-account"]))))))) (d-accounts/get-by-id (or id (get-in result [:tempids "new-account"])))))))

View File

@@ -60,7 +60,7 @@
[:forecasted-transactions [:id :amount :identifier :day-of-month]]]] [:forecasted-transactions [:id :amount :identifier :day-of-month]]]]
[:vendor [:vendor
vendor-query] 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]}})))) :on-success [::received-initial]}}))))

View File

@@ -74,6 +74,7 @@
(re-frame/reg-event-db (re-frame/reg-event-db
::change ::change
(fn [db [_ form & path-pairs]] (fn [db [_ form & path-pairs]]
(reduce (reduce
(fn [db [path value]] (fn [db [path value]]
(assoc-in db (into [::forms form :data] path) value)) (assoc-in db (into [::forms form :data] path) value))
@@ -82,6 +83,7 @@
(defn change-handler [form customize-fn] (defn change-handler [form customize-fn]
(fn [db [_ & path-pairs]] (fn [db [_ & path-pairs]]
(reduce (reduce
(fn [db [path value]] (fn [db [path value]]
(let [updated (assoc-in db (into [::forms form :data] path) value)] (let [updated (assoc-in db (into [::forms form :data] path) value)]

View File

@@ -47,7 +47,8 @@
[:div.control [:div.control
[:div.tags.has-addons [:div.tags.has-addons
[:span.tag text] [: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"} ^{:key "typeahead"}
[:input.input {:type "text" [:input.input {:type "text"
:class class :class class

View File

@@ -1,12 +1,12 @@
(ns auto-ap.views.pages.admin.accounts.form (ns auto-ap.views.pages.admin.accounts.form
(:require [auto-ap.forms :as forms] (:require [auto-ap.entities.account :as entity]
[clojure.string :as str] [auto-ap.forms :as forms]
[clojure.spec.alpha :as s]
[auto-ap.subs :as subs] [auto-ap.subs :as subs]
[auto-ap.entities.account :as entity]
[auto-ap.views.components.layouts :refer [side-bar]] [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.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])) [re-frame.core :as re-frame]))
(def types [:dividend :expense :asset :liability :equity :revenue]) (def types [:dividend :expense :asset :liability :equity :revenue])
@@ -28,7 +28,8 @@
:name name :name name
:account-set account-set :account-set account-set
:client-overrides (map (fn [client-override] :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)) :name (if (str/blank? (:name client-override))
nil nil
(:name client-override))}) (:name client-override))})
@@ -43,6 +44,7 @@
(re-frame/reg-event-db (re-frame/reg-event-db
::editing ::editing
(fn [db [_ which complete-listener]] (fn [db [_ which complete-listener]]
(println "WHICH" which)
(-> db (-> db
(forms/start-form ::form which complete-listener)))) (forms/start-form ::form which complete-listener))))
@@ -72,7 +74,7 @@
:operation/name "UpsertAccount"} :operation/name "UpsertAccount"}
:venia/queries [{:query/data [:upsert-account :venia/queries [{:query/data [:upsert-account
{:account @(re-frame/subscribe [::request])} {: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-success [::edited]
:on-error [::forms/save-error ::form]}})))) :on-error [::forms/save-error ::form]}}))))
@@ -83,78 +85,67 @@
(defn form [_] (defn form [_]
(let [{error :error account :data } @(re-frame/subscribe [::forms/form ::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 {:keys [form-inline field field-holder raw-field error-notification submit-button]} account-form]
change-event [::forms/change ::form]]
^{:key (:id account)} ^{:key (:id account)}
[side-bar {:on-close (dispatch-event [::forms/form-closing ::form])} [side-bar {:on-close (dispatch-event [::forms/form-closing ::form])}
[form {:title (if (:id account) (form-inline {:title (if (:id account)
"Edit account" "Edit account"
"Add account")} "Add account")}
[:<>
[field "Account Set" (field "Account Set"
[:input.input {:type "text" [:input.input {:type "text"
:field :account-set :field :account-set
:disabled (boolean (:id account)) :disabled (boolean (:id account))
:spec ::entity/account-set}]] :spec ::entity/account-set}])
[field "Code" (field "Code"
[:input.input {:type "text" [:input.input {:type "text"
:field :numeric-code :field :numeric-code
:disabled (boolean (:id account)) :disabled (boolean (:id account))
:spec ::entity/numeric-code}]] :spec ::entity/numeric-code}])
[field "Name" (field "Name"
[:input.input {:type "text" [:input.input {:type "text"
:field :name :field :name
:spec ::entity/name}]] :spec ::entity/name}])
(field-holder "Account Type"
[field-holder "Account Type" [:div.select
[:div.select (raw-field
[raw-field [:select {:type "select"
[:select {:type "select" :field :type
:field :type :spec (set types)}
:spec (set types)} (map (fn [l]
(map (fn [l] [:option {:value (name l)} (str/capitalize (name l))]) types)])])
[:option {:value (name l)} (str/capitalize (name l))]) types)]]]]
[field "Location" (field "Location"
[:input.input.known-field.location {:type "text" [:input.input.known-field.location {:type "text"
:field :location :field :location
:spec ::entity/location}]] :spec ::entity/location}])
[:h2.subtitle "Client"] [:h2.subtitle "Client"]
[field-holder "Applicability" (field-holder "Applicability"
[:div.select [:div.select
[raw-field (raw-field
[:select {:type "select" [:select {:type "select"
:field :applicability :field :applicability
:spec (set applicabilities)} :spec (set applicabilities)}
(map (fn [l] (map (fn [l]
[:option {:value (name l)} (str/capitalize (name l))]) applicabilities)]]]] [:option {:value (name l)} (str/capitalize (name l))]) applicabilities)])])
[field-holder "Customizations" (field "Customizations"
[:div.field.has-addons [multi-field {:type "multi-field"
[:p.control :field [:client-overrides]
^{:key (count (:client-overrides account))} ;; resets after adding :template [[typeahead-entity {:matches @(re-frame/subscribe [::subs/clients])
[raw-field :match->text :name
[typeahead-entity {:matches @(re-frame/subscribe [::subs/clients]) :type "typeahead"
:match->text :name :field [:client]}]
:type "typeahead" [:input.input {:type "text"
:field [:new-client-override :client]}]]] :placeholder "Bubblegum"
[:p.control :field [:name]}]
[raw-field ]}])
[:input.input {:type "text" (error-notification)
: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]
[submit-button "Save"]]])) (submit-button "Save")])]))

View File

@@ -116,12 +116,12 @@
[:span])]))) [: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 (let [value-repr (r/atom (mapv
(fn [x] (fn [x]
(assoc x :key (random-uuid) :new? false)) (assoc x :key (random-uuid) :new? false))
value))] 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 (let [value @value-repr
already-has-new-row? (= [:key :new?] (keys (last value))) already-has-new-row? (= [:key :new?] (keys (last value)))
value (if already-has-new-row? value (if already-has-new-row?
@@ -147,20 +147,22 @@
^{:key idx} ^{:key idx}
[:div.level-item [:div.level-item
[update template 1 assoc (update template 1 assoc
:value (get-in override (get-in template [1 :field])) :value (get-in override (get-in template [1 :field]))
:disabled is-disabled? :disabled is-disabled?
:on-change (fn [e] :on-change (fn [e]
(reset! value-repr
(reset! value-repr (into []
(into [] (filter (fn [r]
(filter (fn [r] (not= [:key :new?] (keys r)))
(not= [:key :new?] (keys r))) (assoc-in value (into [i] (get-in template [1 :field]))
(assoc-in value (into [i] (get-in template [1 :field])) (.. e -target -value ) )))) (if (and e (.. e -target))
(on-change (mapv (.. e -target -value )
(fn [v] e) ))))
(dissoc v :new? :key)) (on-change (mapv
@value-repr)))]]) (fn [v]
(dissoc v :new? :key))
@value-repr))))])
] ]
[:div.level-item [:div.level-item
[:a.button.level-item [:a.button.level-item
@@ -170,9 +172,12 @@
(when-not is-disabled? (when-not is-disabled?
(reset! value-repr (into [] (reset! value-repr (into []
(filter (fn [{:keys [key ] :as v}] (filter (fn [{:keys [key ] :as v}]
(not= key (:key override))) (not= key (:key override)))
value))) (filter (fn [r]
(not= [:key :new?] (keys r)))
value))))
(on-change (mapv (on-change (mapv
(fn [v] (fn [v]