Supports editing expense accounts

This commit is contained in:
Bryce Covert
2019-04-12 12:00:42 -07:00
parent bdb06d802b
commit e64820d71a
6 changed files with 119 additions and 53 deletions

View File

@@ -1,6 +1,8 @@
(ns auto-ap.views.pages.admin.accounts.form
(:require [auto-ap.forms :as forms]
[clojure.string :as str]
[clojure.spec.alpha :as s]
[auto-ap.entities.account :as entity]
[auto-ap.views.utils :refer [bind-field dispatch-event]]
[re-frame.core :as re-frame]))
@@ -11,6 +13,16 @@
(fn [db]
true))
(re-frame/reg-sub
::account-request
:<- [::forms/form ::form]
(fn [{{:keys [id type numeric-code name account-set]} :data}]
{:id id
:type (keyword type)
:numeric-code numeric-code
:name name
:account-set account-set}))
(re-frame/reg-event-db
::editing
(fn [db [_ which]]
@@ -20,8 +32,7 @@
(re-frame/reg-event-fx
::edited
(fn [{:keys [db]} [_ edit-completed {:keys [upsert-account]}]]
{:db (-> db
(forms/stop-form ::form))
{:db (-> db (forms/saved-form ::form upsert-account))
:dispatch (conj edit-completed upsert-account)}))
(re-frame/reg-event-fx
@@ -35,11 +46,7 @@
: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}}
{:account @(re-frame/subscribe [::account-request])}
[:id :type :name :account-set :numeric-code]]}]}
:on-success [::edited edit-completed]
:on-error [::forms/save-error ::form]}}))))
@@ -50,7 +57,23 @@
[forms/side-bar-form {:form ::form}
[:form
[:h1.title.is-2 "Add/edit account"]
[:h1.title.is-2 (if (:id account)
"Edit account"
"Add account")]
[:div.field
[:p.help "Account Set"]
[:div.control
[bind-field
[:input.input {:type "text"
:field :account-set
:disabled (if (:id account)
"disabled"
"")
:spec ::entity/account-set
:event change-event
:subscription account}]]]]
[:div.field
[:p.help "Code"]
[:div.control
@@ -60,7 +83,7 @@
:disabled (if (:id account)
"disabled"
"")
#_:spec #_:entity/name
:spec ::entity/numeric-code
:event change-event
:subscription account}]]]]
@@ -70,12 +93,14 @@
[bind-field
[:input.input {:type "text"
:field :name
#_:spec #_:entity/name
:spec ::entity/name
:event change-event
:subscription account}]]]]
[:div.field
[:p.help "Location"]
[:p.help "Account Type"]
[:div.control
[:div.select
[bind-field
@@ -85,15 +110,13 @@
:event change-event
:subscription account}
(map (fn [l]
[:option {:value (name l)} (str/capitalize (name l))]) types)]]]]]
(when error
[:div.notification.is-warning.animated.fadeInUp
error])
[:submit.button.is-large.is-primary {#_:disabled #_(if (s/valid? :entity/client @(re-frame/subscribe [::new-client-request]))
[:button.button.is-large.is-primary {:disabled (if (s/valid? ::entity/account @(re-frame/subscribe [::account-request]))
""
"disabled")
:on-click (dispatch-event [::saving edit-completed])