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

@@ -29,13 +29,17 @@
:id (random-uuid)
:status nil
:data data}))
(defn saved-form [db form data]
(update-in db [::forms form]
assoc :error nil :status nil :data data))
(defn stop-form [db form]
(update db ::forms dissoc form))
(re-frame/reg-event-db
::form-closing
(fn [db [_ f]]
(-> db
(stop-form f))))
@@ -56,7 +60,6 @@
(re-frame/reg-event-db
::save-error
(fn [db [_ form result]]
(-> db
(assoc-in [::forms form :status] :error)
(assoc-in [::forms form :error] (or (:message (first result))

View File

@@ -16,35 +16,36 @@
(-> db
(update :accounts replace-by :id edit-account))))
(defn accounts-table [{:keys [accounts]} ]
(defn accounts-table [{:keys [accounts]}]
[:div
(for [[account-set accounts] (group-by :account-set accounts)]
(do (println accounts)
^{:key account-set}
[:div
[:h2.title.is-4 account-set]
[:table.table.compact
[:thead
[:th "Code"]
[:th "Name"]
[:th "Type"]
[:th {:style {:width "5em"}}]
]
[:tbody
(for [{:keys [id numeric-code name type] :as account} (sort-by :numeric-code accounts)]
^{:key id}
[:tr
[:td numeric-code]
[:td name]
[:td type]
[:td [:a.button {:on-click (dispatch-event [::account-form/editing account])} [:span [:span.icon [:i.fa.fa-pencil]]]]]])]]]))])
^{:key (or account-set "blank")}
[:div
[:h2.title.is-4 account-set]
[:table.table.compact.is-fullwidth
[:thead
[:tr
[:th "Code"]
[:th "Name"]
[:th "Type"]
[:th {:style {:width "5em"}}]]]
[:tbody
(for [{:keys [id numeric-code name type] :as account} (sort-by :numeric-code accounts)]
^{:key id}
[:tr
[:td numeric-code]
[:td name]
[:td type]
[:td [:a.button {:on-click (dispatch-event [::account-form/editing account])} [:span [:span.icon [:i.fa.fa-pencil]]]]]])]]])])
(defn admin-accounts-content []
[:div
(let [accounts @(re-frame/subscribe [::subs/accounts])]
[:div
[:h1.title "Accounts"]
[:div.is-pulled-right
[:a.button.is-success {:on-click (dispatch-event [::account-form/editing {:type :asset
:account-set "default"}])} "New Account"]]
[accounts-table {:accounts accounts}]])])
(defn admin-accounts-page []

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])