(ns auto-ap.views.pages.admin.rules (:require [auto-ap.forms :as forms] [auto-ap.subs :as subs] [auto-ap.views.components.admin.side-bar :refer [admin-side-bar]] [auto-ap.views.components.layouts :refer [appearing-side-bar side-bar-layout]] [auto-ap.views.pages.admin.rules.table :as table] [auto-ap.views.utils :refer [dispatch-event with-user]] [re-frame.core :as re-frame])) (re-frame/reg-sub ::notification (fn [db] (-> db ::notification))) (re-frame/reg-sub ::page (fn [db] (-> db ::page))) (re-frame/reg-sub ::params (fn [db] (-> db (::params {})))) (def rule-read [:description-matches]) (re-frame/reg-event-db ::received (fn [db [_ data]] (-> db (update ::page merge (first (:rule-page data))) (assoc-in [:status :loading] false)))) (re-frame/reg-event-fx ::params-change [with-user] (fn [{:keys [db user]} [_ params]] {:db (-> db (assoc-in [:status :loading] true) (assoc-in [::params] params)) :graphql {:token user :query-obj {:venia/queries [[:rule_page (assoc params :client-id (:id @(re-frame/subscribe [::subs/client]))) [[:rules rule-read] :total :start :end]]]} :on-success [::received]}})) (def rules-content (with-meta (fn [] (let [notification (re-frame/subscribe [::notification]) current-client @(re-frame/subscribe [::subs/client]) user @(re-frame/subscribe [::subs/user])] [:div [:h1.title "Transaction Rules"] (when (= "admin" (:user/role user)) [:div.is-pulled-right [:button.button.is-outlined.is-primary {:on-click (dispatch-event [::create-rule-clicked])} "New Rule"]]) [table/table {:id :transactions :params (re-frame/subscribe [::params]) :rule-page (re-frame/subscribe [::page]) :on-params-change (fn [params] (re-frame/dispatch [::params-change params]))}] ])) {:component-will-mount #(re-frame/dispatch-sync [::params-change {}]) })) (defn admin-rules-page [] (let [{:keys [active?]} @(re-frame/subscribe [::forms/form ::new-client])] [side-bar-layout {:side-bar [admin-side-bar {}] :main [rules-content] :right-side-bar [appearing-side-bar {:visible? active?} [:div]] }]))