From f752bfa6328c0619ee551ba62524126f37b0a26d Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Thu, 11 Apr 2019 07:26:17 -0700 Subject: [PATCH] separating transaction stuff out. --- .../views/pages/transactions/edit.cljs | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 src/cljs/auto_ap/views/pages/transactions/edit.cljs diff --git a/src/cljs/auto_ap/views/pages/transactions/edit.cljs b/src/cljs/auto_ap/views/pages/transactions/edit.cljs new file mode 100644 index 00000000..e531411d --- /dev/null +++ b/src/cljs/auto_ap/views/pages/transactions/edit.cljs @@ -0,0 +1,128 @@ +(ns auto-ap.views.pages.transactions.edit + (: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]] + [re-frame.core :as re-frame])) + +(re-frame/reg-event-db + ::transaction-editing + (fn [db [_ which]] + (-> db + (forms/start-form ::edit-transaction {:id (:id which) + :vendor-id (:id (:vendor which)) + :vendor-name (:name (:vendor which))})))) + +(re-frame/reg-sub + ::can-submit-edit-transaction + :<- [::forms/form ::edit-transaction] + (fn [{:keys [data status]} _] + (not= :loading status))) + + +(re-frame/reg-event-fx + ::transaction-edited + (fn [{:keys [db]} [_ edit-completed {:keys [edit-transaction]}]] + {:db (-> db + (forms/stop-form ::edit-transaction)) + :dispatch (conj edit-completed edit-transaction)})) + +(re-frame/reg-event-fx + ::edit-transaction-saving + (fn [{:keys [db]} [_ edit-completed]] + (when @(re-frame/subscribe [::can-submit-edit-transaction]) + (let [{{:keys [id vendor-id]} :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}} + + [:id + :amount + [:vendor [:name :id]] + :date + :post_date + :status + :description_original + [:payment [:check_number :s3_url]] + [:client [:name :id]] + [:bank-account [:name :yodlee-account-id]]]]}]} + :on-success [::transaction-edited edit-completed] + :on-error [::forms/save-error ::edit-transaction]}})))) + + +(defn edit-transaction-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]) + change-event [::forms/change ::edit-transaction]] + ^{:key id} + [:form { :on-submit (fn [e] + (when (.-stopPropagation e) + (.stopPropagation e) + (.preventDefault e)) + (re-frame/dispatch-sync [::edit-transaction-saving edit-completed]))} + [:h1.title.is-2 "Edit Transaction"] + + [:div.notification + [:p "This transaction matches Invoice 'ABC' for 'DBI Beverages'. " [:a "Create payment and match"] "."]] + + + [:div.field + [:p.help "Merchant"] + [:div.control + [bind-field + [:input.input {:type "text" + :field [:merchant-name] + :disabled "disabled" + :subscription data}]]]] + + + [:div.field + [:p.help "Vendor"] + [:div.control + [bind-field + [typeahead {:matches (map (fn [x] [(:id x) (:name x)]) @(re-frame/subscribe [::subs/vendors])) + :type "typeahead" + :auto-focus true + :field [:vendor-id] + :text-field [:vendor-name] + :event change-event + :subscription data}]]]] + + [:div.field + + [:p.control + [:label.checkbox + [:input.checkbox {:type "checkbox" + :field [:always-map] + :subscription data}] + " Always match Merchant '" (:merchant-name data) "' to '" (:vendor-name data) "'" ]]] + + [:div.field + [:p.help "Credit Account"] + [:div.control + [bind-field + [typeahead {:matches (map (fn [x] [(:id x) (str (:id x) " - " (:name x))]) @(re-frame/subscribe [::subs/chooseable-expense-accounts])) + :type "typeahead" + :field [:expense-account-id] + :event [::change id] + :subscription data}]]]] + + (when error + ^{:key error} [:div.notification.is-warning.animated.fadeInUp + error]) + + [:button.button.is-medium.is-primary.is-fullwidth {:disabled (if @(re-frame/subscribe [::can-submit-edit-transaction]) + "" + "disabled") + :class (str @(re-frame/subscribe [::forms/loading-class ::edit-transaction]) + (when error " animated shake"))} "Save"] + ])] + )