can add with multiple expense accounts.
This commit is contained in:
@@ -11,7 +11,8 @@
|
|||||||
[bind-field date->str date-picker dispatch-event standard]]
|
[bind-field date->str date-picker dispatch-event standard]]
|
||||||
[cljs-time.core :as c]
|
[cljs-time.core :as c]
|
||||||
[clojure.spec.alpha :as s]
|
[clojure.spec.alpha :as s]
|
||||||
[re-frame.core :as re-frame]))
|
[re-frame.core :as re-frame]
|
||||||
|
[clojure.string :as str]))
|
||||||
|
|
||||||
;; SUBS
|
;; SUBS
|
||||||
(re-frame/reg-sub
|
(re-frame/reg-sub
|
||||||
@@ -42,7 +43,8 @@
|
|||||||
:location location
|
:location location
|
||||||
:total total
|
:total total
|
||||||
:expense-accounts (map (fn [ea]
|
:expense-accounts (map (fn [ea]
|
||||||
{:id (:id ea)
|
{:id (when-not (str/starts-with? (:id ea) "new-")
|
||||||
|
(:id ea))
|
||||||
:account_id (:id (:account ea))
|
:account_id (:id (:account ea))
|
||||||
:location (:location ea)
|
:location (:location ea)
|
||||||
:amount (:amount ea)})
|
:amount (:amount ea)})
|
||||||
@@ -59,7 +61,8 @@
|
|||||||
:date date
|
:date date
|
||||||
:total total
|
:total total
|
||||||
:expense-accounts (map (fn [ea]
|
:expense-accounts (map (fn [ea]
|
||||||
{:id (:id ea)
|
{:id (when-not (str/starts-with? (:id ea) "new-")
|
||||||
|
(:id ea))
|
||||||
:account_id (:id (:account ea))
|
:account_id (:id (:account ea))
|
||||||
:location (:location ea)
|
:location (:location ea)
|
||||||
:amount (:amount ea)})
|
:amount (:amount ea)})
|
||||||
@@ -78,7 +81,8 @@
|
|||||||
:location location
|
:location location
|
||||||
:total total
|
:total total
|
||||||
:expense-accounts (map (fn [ea]
|
:expense-accounts (map (fn [ea]
|
||||||
{:id (:id ea)
|
{:id (when-not (str/starts-with? (:id ea) "new-")
|
||||||
|
(:id ea))
|
||||||
:account_id (:id (:account ea))
|
:account_id (:id (:account ea))
|
||||||
:location (:location ea)
|
:location (:location ea)
|
||||||
:amount (:amount ea)})
|
:amount (:amount ea)})
|
||||||
@@ -95,7 +99,7 @@
|
|||||||
(re-frame/reg-event-db
|
(re-frame/reg-event-db
|
||||||
::adding
|
::adding
|
||||||
(fn [db [_ new]]
|
(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
|
(re-frame/reg-event-db
|
||||||
::editing
|
::editing
|
||||||
@@ -130,7 +134,19 @@
|
|||||||
::add-expense-account
|
::add-expense-account
|
||||||
[(forms/in-form ::form) (re-frame/path [:data])]
|
[(forms/in-form ::form) (re-frame/path [:data])]
|
||||||
(fn [form]
|
(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
|
(re-frame/reg-event-fx
|
||||||
::submitted
|
::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))]
|
(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}
|
^{:key id}
|
||||||
[:div.columns
|
[:div.box
|
||||||
[:div.column
|
[:div.columns
|
||||||
[bind-field
|
[:div.column
|
||||||
[typeahead {:matches (map (fn [x] [(:id x) (str (:numeric-code x) " - " (:name x))]) @(re-frame/subscribe [::subs/chooseable-expense-accounts]))
|
[:h1.subtitle.is-5 "Ex"]]
|
||||||
:type "typeahead"
|
[:div.column.is-narrow
|
||||||
:field [:expense-accounts index :account :id]
|
[:a.button {:on-click (dispatch-event [::remove-expense-account id])} [:span.icon [:i.fa.fa-times]]]]]
|
||||||
:event [::forms/change ::form ]
|
[:div.field
|
||||||
:subscription data}]]]
|
[:div.columns
|
||||||
|
[:div.column
|
||||||
(when multi-location?
|
[: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
|
[:div.column.is-narrow
|
||||||
(if-let [forced-location (:location @(re-frame/subscribe [::subs/account (get-in data [:expense-accounts index :account :id])]))]
|
[:p.help "Location"]
|
||||||
[:div.select
|
[:div.control
|
||||||
[:select {:disabled "disabled" :value forced-location} [:option {:value forced-location} forced-location]]]
|
(if-let [forced-location (:location @(re-frame/subscribe [::subs/account (get-in data [:expense-accounts index :account :id])]))]
|
||||||
[:div.select
|
[:div.select
|
||||||
[bind-field
|
[:select {:disabled "disabled" :style {:width "4em"} :value forced-location} [:option {:value forced-location} forced-location]]]
|
||||||
[:select {:type "select"
|
[:div.select
|
||||||
:field [:expense-accounts index :location]
|
[bind-field
|
||||||
:allow-nil? true
|
[:select {:type "select"
|
||||||
:spec (set locations)
|
:style {:width "4em"}
|
||||||
:event [::forms/change ::form]
|
:field [:expense-accounts index :location]
|
||||||
:subscription data}
|
:allow-nil? true
|
||||||
(map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])])
|
: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.control
|
||||||
[:div.field.has-addons.is-extended
|
[:div.field.has-addons.is-extended
|
||||||
[:p.control [:a.button.is-static "$"]]
|
[:p.control [:a.button.is-static "$"]]
|
||||||
|
|||||||
Reference in New Issue
Block a user