added apply all rule.

This commit is contained in:
Bryce Covert
2020-01-24 19:28:52 -08:00
parent bb40bff203
commit 54fc606cf0
8 changed files with 85 additions and 19 deletions

View File

@@ -5,8 +5,9 @@
[auto-ap.subs :as subs]
[auto-ap.views.utils :refer [with-keys]]))
(defn modal [{:keys [title foot hide-event]} & body]
[:div.modal.is-active
(defn modal [{:keys [title foot hide-event class]} & body]
[:div.modal.is-active (cond-> {}
class (assoc :class class))
[:div.modal-background {:on-click (fn [] (re-frame/dispatch hide-event ))}]
[:div.modal-card

View File

@@ -23,6 +23,11 @@
(fn [db]
(::checked db)))
(re-frame/reg-sub
::all-checked
(fn [db]
(::all-checked db nil)))
(re-frame/reg-sub
::checked-count
:<- [::checked]
@@ -35,11 +40,12 @@
::opening
(fn [{:keys [db]} [_ results transaction-rule-id runnable?]]
{:db (-> db
(assoc ::all-checked false)
(assoc ::test-results results)
(assoc ::transaction-rule-id transaction-rule-id)
(assoc ::checked #{})
(assoc ::runnable? runnable?))
:dispatch [::events/modal-status ::test-results {:visible? true}]}))
:dispatch [::events/modal-status ::test-results {:visible? true :error-message nil}]}))
(re-frame/reg-event-db
::toggle
@@ -50,6 +56,32 @@
(disj checked which)
(conj checked which)))))
(re-frame/reg-event-db
::toggle-all
(fn [db [_ which]]
(let [{::keys [all-checked checked test-results]} db]
(assoc db
::all-checked (not all-checked)
::checked (if all-checked
#{}
(into #{} (map :id test-results)))))))
(re-frame/reg-event-fx
::apply-failed
[with-user]
(fn [{:keys [db user]} [_ params]]
{:graphql
{:token user
:query-obj {:venia/operation {:operation/type :mutation
:operation/name "MatchTransactionRules"}
:venia/queries [{:query/data [:match-transaction-rules
{:transaction-ids (seq @(re-frame/subscribe [::checked]))
:all (boolean (:all? params))
:transaction-rule-id (::transaction-rule-id db)}
transaction-read]}]}
:on-success [::events/modal-status ::test-results {:visible? false}]
:on-error [:events/modal-failed ::test-results]}}))
(re-frame/reg-event-fx
::apply-rule
[with-user]
@@ -60,27 +92,42 @@
:operation/name "MatchTransactionRules"}
:venia/queries [{:query/data [:match-transaction-rules
{:transaction-ids (seq @(re-frame/subscribe [::checked]))
:all (boolean (:all? params))
:transaction-rule-id (::transaction-rule-id db)}
transaction-read]}]}
:on-success [::events/modal-status ::test-results {:visible? false}]
#_#_:on-error [:forms/save-error ::form]}}))
:on-error [::events/modal-failed ::test-results]}}))
(defn test-results-modal [params]
(let [runnable? @(re-frame/subscribe [::runnable?])
checked @(re-frame/subscribe [::checked])
checked-count @(re-frame/subscribe [::checked-count])]
(when (:visible? @(re-frame/subscribe [::subs/modal-state ::test-results]))
all-checked @(re-frame/subscribe [::all-checked])
checked-count @(re-frame/subscribe [::checked-count])
modal-state @(re-frame/subscribe [::subs/modal-state ::test-results])]
(when (:visible? modal-state)
[modal {:title "Rule results"
:class "wide"
:hide-event [::events/modal-status ::test-results {:visible? false}]
:foot (when runnable?
[:button.button.is-primary {:disabled (boolean (= checked-count 0))
:on-click (dispatch-event [::apply-rule params])}
(str "Apply to " checked-count " transactions")])}
[:div
(when (:error-message modal-state)
[:div.notification.is-warning
(:error-message modal-state)])
[:button.button.is-primary {:disabled (boolean (= checked-count 0))
:on-click (dispatch-event [::apply-rule params])}
(str "Apply to " checked-count " transactions")]
[:button.button.is-warning {:disabled (not all-checked)
:on-click (dispatch-event [::apply-rule (assoc params :all? true)])}
(str "Apply to all transactions")]])}
[:table.table.is-fullwidth.compact
[:tr
(when runnable?
[:th {:style {:width "2em"}}])
[:th {:style {:width "2em"}}
[:input.checkbox {:type "checkbox"
:checked all-checked
:on-change (dispatch-event [::toggle-all])}]])
[:th "Client"]
[:th "Bank Account"]
[:th "Description"]