can add with multiple expense accounts.

This commit is contained in:
Bryce Covert
2019-04-16 22:10:30 -07:00
parent bc4287befe
commit 5c44ef07de

View File

@@ -11,7 +11,8 @@
[bind-field date->str date-picker dispatch-event standard]]
[cljs-time.core :as c]
[clojure.spec.alpha :as s]
[re-frame.core :as re-frame]))
[re-frame.core :as re-frame]
[clojure.string :as str]))
;; SUBS
(re-frame/reg-sub
@@ -42,7 +43,8 @@
:location location
:total total
:expense-accounts (map (fn [ea]
{:id (:id ea)
{:id (when-not (str/starts-with? (:id ea) "new-")
(:id ea))
:account_id (:id (:account ea))
:location (:location ea)
:amount (:amount ea)})
@@ -59,7 +61,8 @@
:date date
:total total
:expense-accounts (map (fn [ea]
{:id (:id ea)
{:id (when-not (str/starts-with? (:id ea) "new-")
(:id ea))
:account_id (:id (:account ea))
:location (:location ea)
:amount (:amount ea)})
@@ -78,7 +81,8 @@
:location location
:total total
:expense-accounts (map (fn [ea]
{:id (:id ea)
{:id (when-not (str/starts-with? (:id ea) "new-")
(:id ea))
:account_id (:id (:account ea))
:location (:location ea)
:amount (:amount ea)})
@@ -95,7 +99,7 @@
(re-frame/reg-event-db
::adding
(fn [db [_ new]]
(-> db (forms/start-form ::form (assoc new :expense-accounts [{}])))))
(-> db (forms/start-form ::form (assoc new :expense-accounts [{:id (str "new-" (random-uuid))}])))))
(re-frame/reg-event-db
::editing
@@ -130,7 +134,19 @@
::add-expense-account
[(forms/in-form ::form) (re-frame/path [:data])]
(fn [form]
(update form :expense-accounts conj {:amount 0})))
(update form :expense-accounts conj {:amount 0 :id (str "new-" (random-uuid))})))
(re-frame/reg-event-db
::remove-expense-account
[(forms/in-form ::form) (re-frame/path [:data])]
(fn [form [_ x]]
(update form :expense-accounts (fn [eas]
(transduce (filter
(fn [ea]
(not= (:id ea) x)) )
conj
[]
eas)))))
(re-frame/reg-event-fx
::submitted
@@ -289,32 +305,44 @@
(for [[index {:keys [id location] :as expense-account {account-id :id account-numeric-code :numeric-code account-name :name} :account}] (map vector (range) (:expense-accounts data))]
^{:key id}
[:div.columns
[:div.column
[bind-field
[typeahead {:matches (map (fn [x] [(:id x) (str (:numeric-code x) " - " (:name x))]) @(re-frame/subscribe [::subs/chooseable-expense-accounts]))
:type "typeahead"
:field [:expense-accounts index :account :id]
:event [::forms/change ::form ]
:subscription data}]]]
(when multi-location?
[:div.box
[:div.columns
[:div.column
[:h1.subtitle.is-5 "Ex"]]
[:div.column.is-narrow
[:a.button {:on-click (dispatch-event [::remove-expense-account id])} [:span.icon [:i.fa.fa-times]]]]]
[:div.field
[:div.columns
[:div.column
[:p.help "Expense Account"]
[:div.control.is-fullwidth
[bind-field
[typeahead {:matches (map (fn [x] [(:id x) (str (:numeric-code x) " - " (:name x))]) @(re-frame/subscribe [::subs/chooseable-expense-accounts]))
:type "typeahead"
:field [:expense-accounts index :account :id]
:text-field [:expense-accounts index :account :name]
:event [::forms/change ::form ]
:subscription data}]]]]
[:div.column.is-narrow
(if-let [forced-location (:location @(re-frame/subscribe [::subs/account (get-in data [:expense-accounts index :account :id])]))]
[:div.select
[:select {:disabled "disabled" :value forced-location} [:option {:value forced-location} forced-location]]]
[:div.select
[bind-field
[:select {:type "select"
:field [:expense-accounts index :location]
:allow-nil? true
:spec (set locations)
:event [::forms/change ::form]
:subscription data}
(map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])])
[:p.help "Location"]
[:div.control
(if-let [forced-location (:location @(re-frame/subscribe [::subs/account (get-in data [:expense-accounts index :account :id])]))]
[:div.select
[:select {:disabled "disabled" :style {:width "4em"} :value forced-location} [:option {:value forced-location} forced-location]]]
[:div.select
[bind-field
[:select {:type "select"
:style {:width "4em"}
:field [:expense-accounts index :location]
:allow-nil? true
:spec (set locations)
:event [::forms/change ::form]
:subscription data}
(map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])]]]]
[:div.column.is-narrow
[:div.field
[:p.help "Amount"]
[:div.control
[:div.field.has-addons.is-extended
[:p.control [:a.button.is-static "$"]]