287 lines
12 KiB
Clojure
287 lines
12 KiB
Clojure
(ns auto-ap.views.pages.transactions
|
|
(:require [auto-ap.events :as events]
|
|
[auto-ap.forms :as forms]
|
|
[auto-ap.subs :as subs]
|
|
[bidi.bidi :as bidi]
|
|
[auto-ap.routes :as routes]
|
|
[auto-ap.views.components.bank-account-filter :refer [bank-account-filter]]
|
|
[auto-ap.views.components.date-range-filter :refer [date-range-filter]]
|
|
[auto-ap.views.components.number-filter :refer [number-filter]]
|
|
[auto-ap.views.components.vendor-filter :refer [vendor-filter]]
|
|
[auto-ap.views.components.typeahead :refer [typeahead-entity]]
|
|
[auto-ap.views.components.layouts :refer [appearing-side-bar side-bar-layout]]
|
|
[auto-ap.views.components.modal :refer [action-modal]]
|
|
[auto-ap.views.components.paginator :refer [paginator]]
|
|
[auto-ap.views.components.sorter :refer [sorted-column]]
|
|
[auto-ap.views.pages.transactions.form :as edit]
|
|
[auto-ap.views.pages.transactions.table :as table]
|
|
[auto-ap.views.pages.transactions.common :refer [transaction-read]]
|
|
[auto-ap.utils :refer [replace-by]]
|
|
[auto-ap.views.pages.transactions.manual :as manual]
|
|
[auto-ap.views.utils :refer [bind-field date->str dispatch-event nf date-picker active-when]]
|
|
[goog.string :as gstring]
|
|
[re-frame.core :as re-frame]))
|
|
|
|
|
|
|
|
(re-frame/reg-event-db
|
|
::edit-completed
|
|
(fn [db [_ edit-transaction]]
|
|
(-> db
|
|
(update-in [::transaction-page :transactions]
|
|
replace-by :id (assoc edit-transaction :class "live-added")))))
|
|
|
|
(re-frame/reg-event-fx
|
|
::manual-import-completed
|
|
(fn [{:keys [db]} [_ {:keys [imported errors]}]]
|
|
{:dispatch [::params-change {}]
|
|
:db (-> db
|
|
(assoc-in [::notification :message] (str "Successfully imported " imported " transactions"))
|
|
(assoc-in [::notification :errors] errors))}))
|
|
|
|
(re-frame/reg-sub
|
|
::transaction-page
|
|
(fn [db]
|
|
(-> db ::transaction-page)))
|
|
|
|
(re-frame/reg-sub
|
|
::params
|
|
(fn [db]
|
|
(-> db (::params {}))))
|
|
|
|
(re-frame/reg-event-fx
|
|
::params-change
|
|
(fn [cofx [_ params]]
|
|
|
|
{:db (-> (:db cofx)
|
|
(assoc-in [:status :loading] true)
|
|
(assoc-in [::params] params))
|
|
:graphql {:token (-> cofx :db :user)
|
|
:query-obj {:venia/queries [[:transaction_page
|
|
(assoc params :client-id (:id @(re-frame/subscribe [::subs/client])))
|
|
[[:transactions transaction-read]
|
|
:total
|
|
:start
|
|
:end]]]}
|
|
:on-success [::received]}}))
|
|
|
|
(re-frame/reg-event-fx
|
|
::unapprove-all
|
|
(fn [cofx [_ params]]
|
|
|
|
{:db (-> (:db cofx)
|
|
(assoc-in [:status :loading] true)
|
|
(assoc-in [::params] params))
|
|
:graphql {:token (-> cofx :db :user)
|
|
:query-obj
|
|
{:venia/operation {:operation/type :mutation
|
|
:operation/name "UnapproveTransactions"}
|
|
:venia/queries [{:query/data
|
|
[:unapprove-transactions
|
|
(assoc (::params (:db cofx)) :client-id (:id @(re-frame/subscribe [::subs/client])))
|
|
[[:transactions transaction-read]
|
|
:total
|
|
:start
|
|
:end]]}]}
|
|
|
|
:on-success [::received]}}))
|
|
|
|
(re-frame/reg-event-db
|
|
::received
|
|
(fn [db [_ data]]
|
|
(-> db
|
|
(update ::transaction-page merge (first (:transaction-page data)))
|
|
(assoc-in [:status :loading] false))))
|
|
|
|
(re-frame/reg-event-db
|
|
::change-selected-bank-account
|
|
(fn [db [_ key value]]
|
|
(let [[key] key
|
|
updated (assoc-in db [::transaction-page :bank-account-filter key] value)]
|
|
(if (and (= key :id)
|
|
(not= value (get-in db [::params :bank-account-id])))
|
|
(do
|
|
(re-frame/dispatch [::params-change (assoc (::params updated) :bank-account-id value)])
|
|
(assoc-in updated [::params :bank-account-id] value))
|
|
updated))))
|
|
|
|
|
|
(re-frame/reg-event-fx
|
|
::change-selected-date-range
|
|
(fn [{:keys [db]} [_ key value]]
|
|
(let [[key] key
|
|
updated (-> db
|
|
(assoc-in [::transaction-page :date-range-filter key] value)
|
|
(assoc-in [::params :date-range key] value))]
|
|
{:dispatch [::params-change (::params updated)]
|
|
:db updated})))
|
|
|
|
(re-frame/reg-event-fx
|
|
::change-selected-amount-range
|
|
(fn [{:keys [db]} [_ key value]]
|
|
(let [[key] key
|
|
updated (-> db
|
|
(assoc-in [::transaction-page :amount-range-filter key] value)
|
|
(assoc-in [::params key] value))]
|
|
{:dispatch [::params-change (::params updated)]
|
|
:db updated})))
|
|
|
|
(re-frame/reg-event-fx
|
|
::change-selected-vendor
|
|
(fn [{:keys [db]} [_ vendor]]
|
|
(let [updated (-> db
|
|
(assoc-in [::transaction-page :vendor-filter] vendor)
|
|
(assoc-in [::params :vendor-id] (:id vendor)))]
|
|
{:dispatch [::params-change (::params updated)]
|
|
:db updated})))
|
|
|
|
(re-frame/reg-event-fx
|
|
::description-settled
|
|
(fn [{:keys [db]} [_ description]]
|
|
(let [updated (-> db
|
|
(assoc-in [::params :description] description))]
|
|
{:dispatch [::params-change (::params updated)]
|
|
:db updated})))
|
|
|
|
|
|
(re-frame/reg-event-fx
|
|
::description-changed
|
|
(fn [{:keys [db]} [_ description]]
|
|
{:dispatch-debounce
|
|
{:event [::description-settled description]
|
|
:time 500
|
|
:key ::description}
|
|
:db (assoc-in db [::transaction-page :description] description)}))
|
|
|
|
(re-frame/reg-sub
|
|
::notification
|
|
(fn [db]
|
|
(-> db ::notification)))
|
|
|
|
(defn content [{:keys [approval-status]}]
|
|
(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 "Transactions"]
|
|
(when (= "admin" (:user/role user))
|
|
[:div
|
|
(when (:message @notification)
|
|
(list
|
|
[:div.notification
|
|
(:message @notification)]
|
|
(when (seq (:errors @notification))
|
|
[:div.notification.is-danger
|
|
[:h3 (count (:errors @notification)) " errors:"]
|
|
[:ul
|
|
(for [transaction (:errors @notification)
|
|
error (:errors transaction)]
|
|
[:li (:description-original transaction) "-" (:details error)])]])))
|
|
[:div.is-pulled-right
|
|
[:div.buttons
|
|
[:button.button.is-outlined.is-primary {:on-click (dispatch-event [::manual/opening])}
|
|
"Manual Yodlee Import"]
|
|
[:button.button.is-outlined.is-danger {:on-click (dispatch-event [::unapprove-all])}
|
|
"Unapprove all"]]]])
|
|
[table/table {:id :transactions
|
|
:params (re-frame/subscribe [::params])
|
|
:transaction-page (re-frame/subscribe [::transaction-page])
|
|
:status (re-frame/subscribe [::subs/status])
|
|
:on-params-change (fn [params]
|
|
(re-frame/dispatch [::params-change params]))}]
|
|
]))
|
|
{:component-will-mount #(re-frame/dispatch-sync [::params-change {:approval-status approval-status}]) }))
|
|
|
|
(def main-content {nil (content {:approval-status nil})
|
|
:unapproved (content {:approval-status :unapproved})
|
|
:requires-feedback (content {:approval-status :requires-feedback})
|
|
:approved (content {:approval-status :approved})
|
|
:excluded (content {:approval-status :excluded})})
|
|
|
|
|
|
(defn transactions-page [{:keys [approval-status]}]
|
|
(let [{transaction-bar-active? :active?} @(re-frame/subscribe [::forms/form ::edit/form])
|
|
ap @(re-frame/subscribe [::subs/active-page])]
|
|
[side-bar-layout
|
|
{:side-bar
|
|
|
|
[:div
|
|
[:div [:p.menu-label "Type"]
|
|
[:ul.menu-list
|
|
[:li.menu-item
|
|
[:a.item {:href (bidi/path-for routes/routes :transactions)
|
|
:class [(active-when ap = :transactions)]}
|
|
[:span {:class "icon" :style {:font-size "25px"}}]
|
|
[:span {:class "name"} "All"]]]
|
|
[:li.menu-item
|
|
[:a.item {:href (bidi/path-for routes/routes :unapproved-transactions)
|
|
:class [(active-when ap = :unapproved-transactions)]}
|
|
[:span {:class "icon icon-task-list-text-1" :style {:font-size "25px"}}]
|
|
[:span {:class "name"} "Unapproved"]]]
|
|
[:li.menu-item
|
|
[:a.item {:href (bidi/path-for routes/routes :requires-feedback-transactions)
|
|
:class [(active-when ap = :requires-feedback-transactions)]}
|
|
|
|
[:span {:class "icon icon-task-list-question" :style {:font-size "25px"}}]
|
|
|
|
[:span {:class "name"} "Client Review"]]]
|
|
[:li.menu-item
|
|
[:a.item {:href (bidi/path-for routes/routes :approved-transactions)
|
|
:class [(active-when ap = :approved-transactions)]}
|
|
|
|
[:span {:class "icon icon-task-list-check-1" :style {:font-size "25px"}}]
|
|
|
|
[:span {:class "name"} "Approved"]]]
|
|
[:li.menu-item
|
|
[:a.item {:href (bidi/path-for routes/routes :excluded-transactions)
|
|
:class [(active-when ap = :excluded-transactions)]}
|
|
|
|
[:span {:class "icon icon-task-list-disable" :style {:font-size "25px"}}]
|
|
|
|
[:span {:class "name"} "Excluded"]]]
|
|
]]
|
|
[:p.menu-label "Bank Account"]
|
|
[:div
|
|
[bank-account-filter
|
|
{:on-change-event [::change-selected-bank-account]
|
|
:value (:bank-acount-filter @(re-frame/subscribe [::transaction-page]))
|
|
:bank-accounts @(re-frame/subscribe [::subs/bank-accounts])}]]
|
|
|
|
[:p.menu-label "Date Range"]
|
|
[:div
|
|
[date-range-filter
|
|
{:on-change-event [::change-selected-date-range]
|
|
:value (:date-range-filter @(re-frame/subscribe [::transaction-page]))}]]
|
|
|
|
[:p.menu-label "Amount"]
|
|
[:div
|
|
[number-filter
|
|
{:on-change-event [::change-selected-amount-range]
|
|
:value (:amount-range-filter @(re-frame/subscribe [::transaction-page]))}]]
|
|
|
|
[:p.menu-label "Vendor"]
|
|
[:div
|
|
[typeahead-entity {:matches @(re-frame/subscribe [::subs/vendors])
|
|
:on-change #(re-frame/dispatch [::change-selected-vendor %])
|
|
:match->text :name
|
|
:type "typeahead-entity"
|
|
:value (:vendor-filter @(re-frame/subscribe [::transaction-page]))}]]
|
|
|
|
[:p.menu-label "Description"]
|
|
[:div
|
|
[:div.field
|
|
[:div.control [:input.input {:placeholder "CHECK 123 ABC"
|
|
:value (:description @(re-frame/subscribe [::transaction-page]))
|
|
:on-change (fn [e]
|
|
(re-frame/dispatch [::description-changed (.. e -target -value) ]))} ]]]]]
|
|
:main [(get main-content approval-status)]
|
|
|
|
:bottom [:div
|
|
[manual/modal {:import-completed [::manual-import-completed ]}]]
|
|
:right-side-bar [appearing-side-bar
|
|
{:visible? transaction-bar-active?}
|
|
[edit/form {:edit-completed [::edit-completed]}]]}]))
|
|
|