add money field.
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
(:require [auto-ap.forms :as forms]
|
(:require [auto-ap.forms :as forms]
|
||||||
[auto-ap.subs :as subs]
|
[auto-ap.subs :as subs]
|
||||||
[auto-ap.views.components.typeahead :refer [typeahead typeahead-entity]]
|
[auto-ap.views.components.typeahead :refer [typeahead typeahead-entity]]
|
||||||
|
[auto-ap.views.components.money-field :refer [money-field]]
|
||||||
[auto-ap.views.utils :refer [bind-field dispatch-event ->$]]
|
[auto-ap.views.utils :refer [bind-field dispatch-event ->$]]
|
||||||
[goog.string :as gstring]
|
[goog.string :as gstring]
|
||||||
[re-frame.core :as re-frame]
|
[re-frame.core :as re-frame]
|
||||||
@@ -37,7 +38,7 @@
|
|||||||
|
|
||||||
(update :amount js/parseFloat)
|
(update :amount js/parseFloat)
|
||||||
(assoc :amount-percentage (* 100 (/ (js/parseFloat (:amount a))
|
(assoc :amount-percentage (* 100 (/ (js/parseFloat (:amount a))
|
||||||
(Math/abs (js/parseFloat total)))))
|
(Math/abs total))))
|
||||||
(assoc :amount-mode "$")))
|
(assoc :amount-mode "$")))
|
||||||
accounts))
|
accounts))
|
||||||
[{:id (str "new-" (random-uuid))
|
[{:id (str "new-" (random-uuid))
|
||||||
@@ -78,7 +79,7 @@
|
|||||||
(assoc ea :amount
|
(assoc ea :amount
|
||||||
(js/parseFloat
|
(js/parseFloat
|
||||||
(goog.string/format "%.2f"
|
(goog.string/format "%.2f"
|
||||||
(* (/ (js/parseFloat (:amount-percentage ea)) 100.0) (js/parseFloat total))))))
|
(* (/ (js/parseFloat (:amount-percentage ea)) 100.0) total)))))
|
||||||
expense-accounts))
|
expense-accounts))
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
|
|||||||
23
src/cljs/auto_ap/views/components/money_field.cljs
Normal file
23
src/cljs/auto_ap/views/components/money_field.cljs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
(ns auto-ap.views.components.money-field
|
||||||
|
(:require [reagent.core :as r]
|
||||||
|
[clojure.string :as str]))
|
||||||
|
|
||||||
|
(defn money-field [{:keys [min max disabled on-change value]}]
|
||||||
|
(let [parsed-amount (r/atom {:parsed value
|
||||||
|
:raw (str value)})]
|
||||||
|
(fn [{:keys [min max disabled on-change value]}]
|
||||||
|
[:input.input {:type "number"
|
||||||
|
:disabled disabled
|
||||||
|
:on-change (fn [e]
|
||||||
|
(let [raw (.. e -target -value)
|
||||||
|
new-value (when (and raw (not (str/blank? raw)))
|
||||||
|
(js/parseFloat raw))]
|
||||||
|
(swap! parsed-amount assoc
|
||||||
|
:raw raw
|
||||||
|
:parsed new-value)
|
||||||
|
(when (not= value new-value)
|
||||||
|
(on-change new-value))))
|
||||||
|
:value (:raw @parsed-amount)
|
||||||
|
:min min
|
||||||
|
:max max
|
||||||
|
:step "0.01"}])))
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
[auto-ap.views.components.dropdown :refer [drop-down]]
|
[auto-ap.views.components.dropdown :refer [drop-down]]
|
||||||
[auto-ap.views.components.expense-accounts-field :as expense-accounts-field :refer [expense-accounts-field recalculate-amounts]]
|
[auto-ap.views.components.expense-accounts-field :as expense-accounts-field :refer [expense-accounts-field recalculate-amounts]]
|
||||||
[auto-ap.views.components.layouts :as layouts]
|
[auto-ap.views.components.layouts :as layouts]
|
||||||
|
[auto-ap.views.components.money-field :refer [money-field]]
|
||||||
[auto-ap.views.components.typeahead :refer [typeahead-entity]]
|
[auto-ap.views.components.typeahead :refer [typeahead-entity]]
|
||||||
[auto-ap.views.pages.invoices.common :refer [invoice-read]]
|
[auto-ap.views.pages.invoices.common :refer [invoice-read]]
|
||||||
[auto-ap.views.utils :refer [date->str date-picker dispatch-event standard with-user]]
|
[auto-ap.views.utils :refer [date->str date-picker dispatch-event standard with-user]]
|
||||||
@@ -133,7 +134,7 @@
|
|||||||
:client (:client edit-invoice)
|
:client (:client edit-invoice)
|
||||||
:expense-accounts (expense-accounts-field/from-graphql (:expense-accounts which)
|
:expense-accounts (expense-accounts-field/from-graphql (:expense-accounts which)
|
||||||
accounts-by-id
|
accounts-by-id
|
||||||
(:amount which)
|
(:total which)
|
||||||
locations)})))))
|
locations)})))))
|
||||||
|
|
||||||
|
|
||||||
@@ -268,7 +269,7 @@
|
|||||||
|
|
||||||
|
|
||||||
[field "Total"
|
[field "Total"
|
||||||
[:input.input {:type "number"
|
[money-field {:type "money"
|
||||||
:field [:total]
|
:field [:total]
|
||||||
:disabled (if can-change-amount? "" "disabled")
|
:disabled (if can-change-amount? "" "disabled")
|
||||||
:min min-total
|
:min min-total
|
||||||
|
|||||||
@@ -247,6 +247,21 @@
|
|||||||
keys (dissoc keys :field :subscription :event :spec)]
|
keys (dissoc keys :field :subscription :event :spec)]
|
||||||
(into [dom keys] (with-keys rest))))
|
(into [dom keys] (with-keys rest))))
|
||||||
|
|
||||||
|
(defmethod do-bind "money" [dom {:keys [field event subscription class spec] :as keys} & rest]
|
||||||
|
(let [field (if (keyword? field) [field] field)
|
||||||
|
event (if (keyword? event) [event] event)
|
||||||
|
keys (assoc keys
|
||||||
|
:on-change (fn [x]
|
||||||
|
(re-frame/dispatch (-> event
|
||||||
|
(conj field)
|
||||||
|
(conj x))))
|
||||||
|
:value (get-in subscription field)
|
||||||
|
:class (str class
|
||||||
|
(when (and spec (not (s/valid? spec (get-in subscription field))))
|
||||||
|
" is-danger")))
|
||||||
|
keys (dissoc keys :field :subscription :event :spec)]
|
||||||
|
(into [dom keys] (with-keys rest))))
|
||||||
|
|
||||||
(defmethod do-bind :default [dom {:keys [field event subscription class spec] :as keys} & rest]
|
(defmethod do-bind :default [dom {:keys [field event subscription class spec] :as keys} & rest]
|
||||||
(let [field (if (keyword? field) [field] field)
|
(let [field (if (keyword? field) [field] field)
|
||||||
event (if (keyword? event) [event] event)
|
event (if (keyword? event) [event] event)
|
||||||
|
|||||||
Reference in New Issue
Block a user