locations.

This commit is contained in:
Bryce Covert
2019-04-14 22:08:36 -07:00
parent c44a36c815
commit b05b2244a0
13 changed files with 170 additions and 67 deletions

View File

@@ -3,6 +3,7 @@
(def transaction-read
[:id
:amount
:location
[:vendor [:name :id]]
[:account [:id :name]]
:date

View File

@@ -11,10 +11,20 @@
(fn [db [_ which]]
(-> db
(forms/start-form ::edit-transaction {:id (:id which)
:location (:location which)
:client-id (:id (:client which))
:account-id (:id (:account which))
:account-name (:name (:account which))
:vendor-id (:id (:vendor which))
:vendor-name (:name (:vendor which))}))))
(re-frame/reg-sub
::request
:<- [::forms/form ::edit-transaction]
(fn [{{:keys [id vendor-id account-id location]} :data}]
{:transaction {:id id
:location location
:vendor-id vendor-id
:account-id account-id}}))
(re-frame/reg-sub
::can-submit
@@ -34,26 +44,33 @@
::saving
(fn [{:keys [db]} [_ edit-completed]]
(when @(re-frame/subscribe [::can-submit])
(let [{{:keys [id vendor-id account-id]} :data :as data} @(re-frame/subscribe [::forms/form ::edit-transaction])]
(let [{{:keys [id vendor-id account-id location]} :data :as data} @(re-frame/subscribe [::forms/form ::edit-transaction])]
{:db (forms/loading db ::edit-transaction )
:graphql
{:token (-> db :user)
:query-obj {:venia/operation {:operation/type :mutation
:operation/name "EditTransaction"}
:venia/queries [{:query/data [:edit-transaction
{:transaction {:id id
:vendor-id vendor-id
:account-id account-id}}
@(re-frame/subscribe [::request])
transaction-read]}]}
:on-success [::edited edit-completed]
:on-error [::forms/save-error ::edit-transaction]}}))))
(re-frame/reg-event-fx
::change-account
(fn [{:keys [db]} [_ f a]]
(if-let [forced-location (and (= f [:account-id])
(:location @(re-frame/subscribe [::subs/account a])))]
{:dispatch-n [[::forms/change ::edit-transaction f a]
[::forms/change ::edit-transaction [:location] forced-location]]}
{:dispatch [::forms/change ::edit-transaction f a]})))
(defn form [{:keys [edit-completed]}]
[forms/side-bar-form {:form ::edit-transaction }
(let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::edit-transaction])
data (assoc data :merchant-name "Hello") ;; TODO - just until merchant is added
current-client @(re-frame/subscribe [::subs/client])
locations @(re-frame/subscribe [::subs/locations-for-client (:client-id data)])
change-event [::forms/change ::edit-transaction]]
^{:key id}
[:form { :on-submit (fn [e]
@@ -98,7 +115,7 @@
:field [:always-map]
:subscription data}]
" Always match Merchant '" (:merchant-name data) "' to '" (:vendor-name data) "'" ]]])
[:div.field
[:p.help "Credit Account"]
[:div.control
@@ -107,9 +124,26 @@
:type "typeahead"
:field [:account-id]
:text-field [:account-name]
:event change-event
:event [::change-account]
:subscription data}]]]]
[:div.field
[:p.help "Location"]
[:div.control
(if-let [forced-location (:location @(re-frame/subscribe [::subs/account (-> data :account-id )]))]
[:div.select
[:select {:disabled "disabled" :value forced-location} [:option {:value forced-location} forced-location]]]
[:div.select
[bind-field
[:select {:type "select"
:allow-nil? true
:field [:location]
:spec (set locations)
:event change-event
:subscription data}
(map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])]]
(when error
^{:key error} [:div.notification.is-warning.animated.fadeInUp
error])