can edit accounts.

This commit is contained in:
Bryce Covert
2019-04-12 10:42:18 -07:00
parent c2229c11eb
commit bdb06d802b
6 changed files with 92 additions and 13 deletions

View File

@@ -1,6 +1,7 @@
(ns auto-ap.views.pages.admin.accounts
(:require [auto-ap.forms :as forms]
[auto-ap.subs :as subs]
[auto-ap.utils :refer [replace-by]]
[auto-ap.views.components.admin.side-bar :refer [admin-side-bar]]
[auto-ap.views.utils :refer [dispatch-event]]
[auto-ap.views.components.layouts
@@ -9,7 +10,11 @@
[auto-ap.views.pages.admin.accounts.form :as account-form]
[re-frame.core :as re-frame]))
(re-frame/reg-event-db
::edit-completed
(fn [db [_ edit-account]]
(-> db
(update :accounts replace-by :id edit-account))))
(defn accounts-table [{:keys [accounts]} ]
@@ -46,4 +51,4 @@
(let [{:keys [active?]} @(re-frame/subscribe [::forms/form ::account-form/form])]
[side-bar-layout {:side-bar [admin-side-bar {}]
:main [admin-accounts-content]
:right-side-bar [appearing-side-bar {:visible? active?} [account-form/form]]}]))
:right-side-bar [appearing-side-bar {:visible? active?} [account-form/form {:edit-completed [::edit-completed]}]]}]))

View File

@@ -1,21 +1,50 @@
(ns auto-ap.views.pages.admin.accounts.form
(:require [auto-ap.forms :as forms]
[auto-ap.subs :as subs]
[auto-ap.views.components.admin.side-bar :refer [admin-side-bar]]
[clojure.string :as str]
[auto-ap.views.utils :refer [bind-field dispatch-event]]
[auto-ap.views.components.layouts
:refer
[appearing-side-bar side-bar-layout]]
[re-frame.core :as re-frame]))
(def types [:dividend :expense :asset :liability :equities :revenue])
(re-frame/reg-sub
::can-submit
(fn [db]
true))
(re-frame/reg-event-db
::editing
(fn [db [_ which]]
(-> db
(forms/start-form ::form which))))
(defn form []
(re-frame/reg-event-fx
::edited
(fn [{:keys [db]} [_ edit-completed {:keys [upsert-account]}]]
{:db (-> db
(forms/stop-form ::form))
:dispatch (conj edit-completed upsert-account)}))
(re-frame/reg-event-fx
::saving
(fn [{:keys [db]} [_ edit-completed]]
(when @(re-frame/subscribe [::can-submit])
(let [{{:keys [id type name numeric-code account-set]} :data :as data} @(re-frame/subscribe [::forms/form ::form])]
{:db (forms/loading db ::form )
:graphql
{:token (-> db :user)
:query-obj {:venia/operation {:operation/type :mutation
:operation/name "UpsertAccount"}
:venia/queries [{:query/data [:upsert-account
{:account {:id id
:type (keyword type)
:numeric-code numeric-code
:name name
:account-set account-set}}
[:id :type :name :account-set :numeric-code]]}]}
:on-success [::edited edit-completed]
:on-error [::forms/save-error ::form]}}))))
(defn form [{:keys [edit-completed]}]
(let [{error :error account :data } @(re-frame/subscribe [::forms/form ::form])
change-event [::forms/change ::form]]
@@ -28,6 +57,9 @@
[bind-field
[:input.input {:type "text"
:field :numeric-code
:disabled (if (:id account)
"disabled"
"")
#_:spec #_:entity/name
:event change-event
:subscription account}]]]]
@@ -52,7 +84,9 @@
:spec (set types)
:event change-event
:subscription account}
(map (fn [l] [:option {:value l} (name l)]) types)]]]]]
(map (fn [l]
[:option {:value (name l)} (str/capitalize (name l))]) types)]]]]]
(when error
[:div.notification.is-warning.animated.fadeInUp
@@ -62,5 +96,5 @@
[:submit.button.is-large.is-primary {#_:disabled #_(if (s/valid? :entity/client @(re-frame/subscribe [::new-client-request]))
""
"disabled")
:on-click (dispatch-event [::account-saving])
:on-click (dispatch-event [::saving edit-completed])
:class (str @(re-frame/subscribe [::forms/loading-class ::form]) (when error " animated shake"))} "Save"]]]))