added ability to remove a transaction rule.

This commit is contained in:
Bryce Covert
2020-08-03 16:54:12 -07:00
parent a22eb01008
commit d989279058
10 changed files with 185 additions and 51 deletions

View File

@@ -10,17 +10,15 @@
[auto-ap.views.pages.admin.rules.common :refer [default-read]]
[auto-ap.views.utils :refer [dispatch-event with-user]]
[vimsical.re-frame.cofx.inject :as inject]
[vimsical.re-frame.fx.track :as track]
[day8.re-frame.forward-events-fx]
[auto-ap.events :as events]
[auto-ap.utils :refer [replace-by]]
[auto-ap.utils :refer [replace-by merge-by]]
[re-frame.core :as re-frame]
[auto-ap.status :as status]))
;; SUBS
(re-frame/reg-sub
::notification
(fn [db]
(-> db ::notification)))
(re-frame/reg-sub
::page
@@ -57,34 +55,22 @@
(update :transaction-rules (fn [rules]
(mapv ungraphql-transaction-rule rules))))))))
(re-frame/reg-sub
::last-params
(fn [db]
(-> db ::last-params)))
(re-frame/reg-sub
::params
:<- [::last-params]
:<- [::subs/client]
:<- [::side-bar/filter-params]
:<- [::table/table-params]
(fn [[last-params client filter-params table-params]]
(let [params (cond-> {}
client (assoc :client-id (:id client))
(seq filter-params) (merge filter-params)
(seq table-params) (merge table-params))]
(when (not= params last-params)
(re-frame/dispatch [::params-change]))
params)))
(fn [[client filter-params table-params]]
(cond-> {}
client (assoc :client-id (:id client))
(seq filter-params) (merge filter-params)
(seq table-params) (merge table-params))))
(re-frame/reg-event-fx
::params-change
[with-user (re-frame/inject-cofx ::inject/sub [::params])]
(fn [{:keys [db user] ::keys [params] :as cofx} _]
{:db (-> db
(assoc-in [::last-params] params))
:graphql {:token user
[with-user ]
(fn [{:keys [db user] :as cofx} [_ params]]
{:graphql {:token user
:owns-state {:single ::page}
:query-obj {:venia/queries [[:transaction_rule_page
(or params {})
@@ -102,13 +88,34 @@
(fn [{:keys [db]} _]
{:dispatch [::form/adding {:client @(re-frame/subscribe [::subs/client])}]}))
;; VIEWS
(re-frame/reg-event-db
::deleted-transaction-rule
[(re-frame/path [::page :transaction-rules])]
(fn [transaction-rules [_ [_ {id :delete-transaction-rule}]]]
(merge-by transaction-rules :id {:id id :class "live-removed"})))
(re-frame/reg-event-fx
::mounted
(fn [{:keys [db]}]
{:dispatch-n [[::events/yodlee-merchants-needed]]
:forward-events {:register ::page
:events #{::table/deleted-transaction-rule}
:dispatch-to [::deleted-transaction-rule]}
::track/register {:id ::params
:subscription [::params]
:event-fn (fn [params] [::params-change params])}}))
(re-frame/reg-event-fx
::unmounted
(fn [{:keys [db]}]
{:forward-events {:unregister ::page}
::track/dispose {:id ::params}}))
;; VIEWS
(def rules-content
(with-meta
(fn []
(let [notification (re-frame/subscribe [::notification])
current-client @(re-frame/subscribe [::subs/client])
(let [current-client @(re-frame/subscribe [::subs/client])
user @(re-frame/subscribe [::subs/user])]
[:div
[:h1.title "Transaction Rules"]
@@ -123,16 +130,14 @@
(println "CHANGING PARAMS TO" params)
(re-frame/dispatch [::params-change params]))}]
]))
{:component-will-mount #(do (re-frame/dispatch-sync [::params-change {}])
(re-frame/dispatch [::events/yodlee-merchants-needed])) }))
{:component-did-mount (dispatch-event [::mounted ])
:component-will-unmount #(re-frame/dispatch-sync [::unmounted])}))
(defn admin-rules-page []
(let [{:keys [active?]} @(re-frame/subscribe [::forms/form ::form/form])
params @(re-frame/subscribe [::params])]
(let [{:keys [active?]} @(re-frame/subscribe [::forms/form ::form/form])]
[side-bar-layout {:side-bar [admin-side-bar {}
[:<>
[side-bar/rule-side-bar]
]]
[side-bar/rule-side-bar]]]
:main [rules-content]
:right-side-bar [appearing-side-bar {:visible? active?}
[form/form {:rule-saved [::edit-completed]}]]

View File

@@ -8,10 +8,13 @@
[auto-ap.views.components.sorter :refer [sorted-column toggle-sort-by sort-icon]]
[auto-ap.views.components.buttons :as buttons]
[auto-ap.views.components.grid :as grid]
[auto-ap.views.components.modal :refer [simple-modal]]
[auto-ap.events :as events]
[auto-ap.status :as status]
[re-frame.core :as re-frame]
[reagent.core :as reagent]
[reagent.core :as r]))
[reagent.core :as r]
[auto-ap.views.components.modal :as modal]))
(re-frame/reg-event-fx
::run-clicked
@@ -63,6 +66,39 @@
{:db (merge table-params params)}))
(re-frame/reg-event-fx
::deleted-transaction-rule
(fn []
{:dispatch [::modal/modal-closed]}))
(re-frame/reg-event-fx
::delete-transaction-rule
[with-user]
(fn [{:keys [db user]} [_ id]]
{:graphql
{:token user
:owns-state {:single ::delete-transaction-rule}
:query-obj {:venia/operation {:operation/type :mutation
:operation/name "DeleteTransactionRule"}
:venia/queries [{:query/data [:delete-transaction-rule
{:transaction-rule-id id}]}]}
:on-success [::deleted-transaction-rule]}}))
;; TODO count how many transactions
(re-frame/reg-event-fx
::request-delete
(fn [_ [_ which]]
{:dispatch [::modal/modal-requested {:title "Confirmation"
:body [:div "Are you sure you want to delete transaction rule '" (:description which) "'? Any previously transactions will remain updated, but the rule association will be lost."]
:cancel? true
:confirm {:value "Delete Transaction Rule"
:status-from [::status/single ::delete-transaction-rule]
:on-click (dispatch-event [::delete-transaction-rule (:id which)] )}
:close-event [::status/completed ::delete-transaction-rule]}]}))
(defn table* [{:keys [id rule-page on-params-change params status]}]
(let [{:keys [sort asc]} @params
{:keys [transaction-rules start end count total]} @rule-page
@@ -70,33 +106,34 @@
opc (fn [p]
(re-frame/dispatch [::params-changed p]))
states @(re-frame/subscribe [::status/multi ::run])]
[grid/grid {:on-params-change opc
:params @(re-frame/subscribe [::table-params])
:status status
:column-count 6}
[grid/controls {:start start :end end :count count :total total}]
[grid/table {:fullwidth true }
[:div
[grid/grid {:on-params-change opc
:params @(re-frame/subscribe [::table-params])
:status status
:column-count 6}
[grid/controls {:start start :end end :count count :total total}]
[grid/table {:fullwidth true }
[grid/header
[grid/row {}
[grid/sortable-header-cell {:sort-key "client"
:sort-name "Client"}
:sort-name "Client"}
"Client"]
[grid/sortable-header-cell {:sort-key "bank-account"
:sort-name "Bank Account"}
:sort-name "Bank Account"}
"Bank Account"]
[grid/sortable-header-cell {:sort-key "description"
:sort-name "Description"}
:sort-name "Description"}
"Description"]
[grid/header-cell {:style {:width "12em"}} "Amount"]
[grid/sortable-header-cell {:sort-key "note"
:sort-name "Note"}
:sort-name "Note"}
"Note"]
[grid/header-cell {:style {:width (str (inc (* 2 44)) "px")}}]]]
[grid/header-cell {:style {:width (str (inc (* 3 44)) "px")}}]]]
[grid/body
(for [{:keys [client bank-account description amount-lte amount-gte note id] :as r} transaction-rules]
^{:key id}
@@ -120,7 +157,8 @@
[grid/cell {}
[:div.buttons
[buttons/fa-icon {:event [::run-clicked r] :icon :fa-play :class (status/class-for (get states (:id r)))}]
[buttons/fa-icon {:event [::form/editing r] :icon :fa-pencil}]]]])]]]))
[buttons/sl-icon {:event [::request-delete r] :icon :icon-bin-2}]
[buttons/fa-icon {:event [::form/editing r] :icon :fa-pencil}]]]])]]]]))
(defn table [params]
(r/create-class {:component-will-unmount (dispatch-event [::unmounted])