good progress on making expense accounts editable.

This commit is contained in:
Bryce Covert
2019-04-17 09:00:34 -07:00
parent b79fca113b
commit b3e407d211
4 changed files with 135 additions and 96 deletions

View File

@@ -0,0 +1,108 @@
(ns auto-ap.views.components.expense-account-field
(:require [auto-ap.forms :as forms]
[auto-ap.subs :as subs]
[auto-ap.views.components.typeahead :refer [typeahead]]
[auto-ap.views.utils :refer [bind-field dispatch-event]]
[goog.string :as gstring]
[re-frame.core :as re-frame]))
;; EVENTS
(re-frame/reg-event-fx
::add-expense-account
(fn [_ [_ event expense-accounts]]
{:dispatch (conj event (conj expense-accounts
{:amount 0 :id (str "new-" (random-uuid))}))}))
(re-frame/reg-event-fx
::remove-expense-account
(fn [_ [_ event expense-accounts id]]
{:dispatch (conj event (transduce (filter
(fn [ea]
(not= (:id ea) id)) )
conj
[]
expense-accounts))}))
(re-frame/reg-event-fx
::expense-account-changed
(fn [_ [_ event expense-accounts field value]]
{:dispatch (into event [(assoc-in expense-accounts field value)
(if (= (list :account :id) (drop 1 field))
(if-let [location (:location @(re-frame/subscribe [::subs/account value]))]
[[(first field) :location] location]))])}))
;; VIEWS
(defn expense-accounts-field [{expense-accounts :value max-value :max locations :locations event :event}]
(let [chooseable-expense-accounts @(re-frame/subscribe [::subs/chooseable-expense-accounts])
accounts-by-id @(re-frame/subscribe [::subs/accounts-for-client-by-id])]
[:div
[:div.columns
[:div.column
[:h1.subtitle.is-4.is-inline "Expense Accounts"]]
[:div.column.is-narrow
[:p.buttons
[:a.button {:on-click (dispatch-event [::add-expense-account event expense-accounts])} "Add"]]]]
(for [[index {:keys [account id location amount] :as expense-account}] (map vector (range) expense-accounts)
:let [account (accounts-by-id (:id account))]]
^{:key id}
[:div.box
[:div.columns
[:div.column
[:h1.subtitle.is-6 (if account
(str (:name account) " - "
location ": "
(gstring/format "$%.2f" (or amount 0) ))
[:i "New expense account"])]]
[:div.column.is-narrow
[:a.button {:on-click (dispatch-event [::remove-expense-account event expense-accounts 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))]) chooseable-expense-accounts)
:type "typeahead"
:field [index :account :id]
:event [::expense-account-changed event expense-accounts]
:subscription expense-accounts}]]]]
[:div.column.is-narrow
[:p.help "Location"]
[:div.control
(if-let [forced-location (:location account)]
[:div.select
[:select {:disabled "disabled" :style {:width "5em"} :value forced-location} [:option {:value forced-location} forced-location]]]
[:div.select
[bind-field
[:select {:type "select"
:disabled (if (:location account)
"disabled"
"")
:style {:width "5em"}
:field [index :location]
:allow-nil? true
:spec (set locations)
:event [::expense-account-changed event expense-accounts]
:subscription expense-accounts}
(map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])]]]]
[:div.field
[:p.help "Amount"]
[:div.control
[:div.field.has-addons.is-extended
[:p.control [:a.button.is-static "$"]]
[:p.control
[bind-field
[:input.input {:type "number"
:field [index :amount]
:style {:text-align "right" :width "7em"}
:event [::expense-account-changed event expense-accounts]
:subscription expense-accounts
:value (get-in expense-account [:amount])
:max max-value
:step "0.01"}]]]]]]])]))