made it possible to change accounts.

This commit is contained in:
Bryce Covert
2020-04-29 08:21:39 -07:00
parent 3f8ce25e7f
commit 8043fb0b81
8 changed files with 246 additions and 120 deletions

View File

@@ -38,7 +38,9 @@
[:td name]
[:td type]
[:td location]
[:td [:a.button {:on-click (dispatch-event [::account-form/editing account])} [:span [:span.icon [:i.fa.fa-pencil]]]]]])]]])])
[:td [:a.button {:on-click (dispatch-event [::account-form/editing
account
[::edit-completed]])} [:span [:span.icon [:i.fa.fa-pencil]]]]]])]]])])
(defn admin-accounts-content []
[:div
@@ -46,12 +48,14 @@
[: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"]]
[:a.button.is-success {:on-click (dispatch-event [::account-form/editing
{:type :asset
:account-set "default"}
[::edit-completed]])} "New Account"]]
[accounts-table {:accounts accounts}]])])
(defn admin-accounts-page []
(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 {:edit-completed [::edit-completed]}]]}]))
:right-side-bar [appearing-side-bar {:visible? active?} [account-form/form ]]}]))

View File

@@ -2,45 +2,66 @@
(:require [auto-ap.forms :as forms]
[clojure.string :as str]
[clojure.spec.alpha :as s]
[auto-ap.subs :as subs]
[auto-ap.entities.account :as entity]
[auto-ap.views.utils :refer [bind-field dispatch-event]]
[auto-ap.views.components.typeahead :refer [typeahead-entity]]
[re-frame.core :as re-frame]))
(def types [:dividend :expense :asset :liability :equity :revenue])
(def applicabilities [:global :optional :customized])
(re-frame/reg-sub
::can-submit
(fn [db]
true))
(re-frame/reg-sub
::request
:<- [::forms/form ::form]
(fn [{{:keys [id location type numeric-code name account-set]} :data}]
(fn [{{:keys [id location type client-overrides applicability numeric-code name account-set]} :data}]
{:id id
:type (keyword type)
:applicability (keyword applicability)
:location (if (clojure.string/blank? location)
nil
location)
:numeric-code numeric-code
:name name
:account-set account-set}))
:account-set account-set
:client-overrides (map (fn [client-override]
{:client-id (:id (:client client-override))
:name (if (str/blank? (:name client-override))
nil
(:name client-override))})
client-overrides)}))
(re-frame/reg-sub
::can-submit
:<- [::request]
(fn [request]
(s/valid? ::entity/account request)))
(re-frame/reg-event-db
::editing
(fn [db [_ which]]
(fn [db [_ which complete-listener]]
(-> db
(forms/start-form ::form which))))
(forms/start-form ::form which complete-listener))))
(re-frame/reg-event-fx
::edited
(fn [{:keys [db]} [_ edit-completed {:keys [upsert-account]}]]
{:db (-> db (forms/saved-form ::form upsert-account))
:dispatch (conj edit-completed upsert-account)}))
[(forms/triggers-saved ::form :upsert-account)]
(fn [{:keys [db]} [_ {:keys [upsert-account]}]]))
(re-frame/reg-event-db
::add-client-override
[(forms/in-form ::form)]
(fn [form]
(update form :data (fn [data]
(-> data
(update :client-overrides conj (:new-client-override data))
(dissoc :new-client-override))))))
(re-frame/reg-event-fx
::saving
(fn [{:keys [db]} [_ edit-completed]]
(fn [{:keys [db]} _]
(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 )
@@ -50,88 +71,88 @@
:operation/name "UpsertAccount"}
:venia/queries [{:query/data [:upsert-account
{:account @(re-frame/subscribe [::request])}
[:id :type :name :account-set :numeric-code :location]]}]}
:on-success [::edited edit-completed]
[:id :type :name :account-set :numeric-code :location :applicability [:client-overrides [:name [:client [:id :name]]]]]]}]}
:on-success [::edited]
:on-error [::forms/save-error ::form]}}))))
(defn form [{:keys [edit-completed]}]
(def account-form (forms/vertical-form {:can-submit [::can-submit]
:change-event [::forms/change ::form]
:submit-event [::saving]
:id ::form}))
(defn form [_]
(let [{error :error account :data } @(re-frame/subscribe [::forms/form ::form])
change-event [::forms/change ::form]]
{:keys [form field field-holder raw-field error-notification submit-button]} account-form
change-event [::forms/change ::form]]
[forms/side-bar-form {:form ::form}
[:form
[:h1.title.is-2 (if (:id account)
"Edit account"
"Add account")]
^{:key (:id account)}
[form {:title (if (:id account)
"Edit account"
"Add account")}
[:div.field
[:p.help "Account Set"]
[:div.control
[bind-field
[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 "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 "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"
:field :account-set
:disabled (if (:id account)
"disabled"
"")
:spec ::entity/account-set
:event change-event
:subscription account}]]]]
: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]
[:div.field
[:p.help "Code"]
[:div.control
[bind-field
[:input.input {:type "text"
:field :numeric-code
:disabled (if (:id account)
"disabled"
"")
:spec ::entity/numeric-code
:event change-event
:subscription account}]]]]
[:div.field
[:p.help "Name"]
[:div.control
[bind-field
[:input.input {:type "text"
:field :name
:spec ::entity/name
:event change-event
:subscription account}]]]]
[:div.field
[:p.help "Account Type"]
[:div.control
[:div.select
[bind-field
[:select {:type "select"
:field :type
:spec (set types)
:event change-event
:subscription account}
(map (fn [l]
[:option {:value (name l)} (str/capitalize (name l))]) types)]]]]]
[:div.field
[:p.help "Location"]
[:div.control
[bind-field
[:input.input.known-field.location {:type "text"
:field :location
:spec ::entity/location
:event change-event
:subscription account}]]]]
(when error
[:div.notification.is-warning.animated.fadeInUp
error])
[:button.button.is-large.is-primary {:disabled (if (s/valid? ::entity/account @(re-frame/subscribe [::request]))
""
"disabled")
:on-click (dispatch-event [::saving edit-completed])
:class (str @(re-frame/subscribe [::forms/loading-class ::form]) (when error " animated shake"))} "Save"]]]))
[submit-button "Save"]]))