making it possible to see a list of checks

This commit is contained in:
Bryce Covert
2018-06-07 15:17:07 -07:00
parent fbccc0b209
commit 970a27da56
7 changed files with 256 additions and 3 deletions

View File

@@ -0,0 +1,142 @@
(ns auto-ap.views.pages.checks
(:require [re-frame.core :as re-frame]
[auto-ap.entities.companies :as company]
[auto-ap.entities.vendors :as vendor]
[reagent.core :as reagent]
[goog.string :as gstring]
[auto-ap.views.components.sorter :refer [sorted-column]]
[auto-ap.views.components.paginator :refer [paginator]]
[auto-ap.events :as events]
[auto-ap.views.utils :refer [dispatch-event date->str ]]
[auto-ap.utils :refer [by]]
[auto-ap.views.pages.check :as check]
[auto-ap.views.components.invoice-table :refer [invoice-table] :as invoice-table]
[auto-ap.subs :as subs]))
(re-frame/reg-sub
::check-page
(fn [db]
(-> db ::check-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 [[:check_page
(assoc params :company-id (:id @(re-frame/subscribe [::subs/company])))
[[:checks [:id :amount :check_number :s3_url :date [:vendor [:name :id]] [:company [:name :id]]]]
:total
:start
:end]]]}
:on-success [::received]}}))
(re-frame/reg-event-db
::received
(fn [db [_ data]]
(-> db
(assoc ::check-page (first (:check-page data)))
(assoc-in [:status :loading] false))))
(re-frame/reg-event-fx
::invalidated
(fn [cofx [_ params]]
{:dispatch [::params-change @(re-frame/subscribe [::params])]}))
(defn check-table [{:keys [id check-page status on-params-change vendors params check-boxes checked on-check-changed expense-event]}]
(let [state (reagent/atom (or @params {}))
opc (fn [p]
(swap! state merge p)
(on-params-change p))]
(fn [{:keys [id check-page status on-params-change vendors checked]}]
(println @check-page)
(let [{:keys [sort-by asc]} @state
{:keys [checks start end count total]} @check-page]
[:div
[paginator {:start start :end end :count count :total total
:on-change (fn [p ]
(on-params-change (swap! state merge p)))}]
"Showing " (inc start) "-" end "/" total
[:table.table.is-fullwidth
[:thead
[:tr
[sorted-column {:on-sort opc
:style {:width "33%" :cursor "pointer"}
:sort-key "company"
:sort-by sort-by
:asc asc}
"Company"]
[sorted-column {:on-sort opc
:style {:width "33%" :cursor "pointer"}
:sort-key "vendor"
:sort-by sort-by
:asc asc}
"Vendor"]
[sorted-column {:on-sort opc
:style {:width "33%" :cursor "pointer"}
:sort-key "invoice-number"
:sort-by sort-by
:asc asc}
"Check #"]
[sorted-column {:on-sort opc
:style {:width "8em" :cursor "pointer"}
:sort-key "date"
:sort-by sort-by
:asc asc}
"Date"]
[sorted-column {:on-sort opc
:style {:width "8em" :cursor "pointer"}
:sort-key "total"
:sort-by sort-by
:asc asc}
"Amount"]
[:th {:style {:width "10em"}} "" ]]]
[:tbody
(if (:loading @status)
[:tr
[:td {:col-span 5}
[:i.fa.fa-spin.fa-spinner]]]
(for [{:keys [company s3-url checks check-number date amount id vendor] :as i} (:checks @check-page)]
^{:key id}
[:tr {:class (:class i)}
[:td (:name company)]
[:td (:name vendor)]
[:td check-number]
[:td (date->str date) ]
[:td (gstring/format "$%.2f" amount )]
[:td [:a.tag {:href s3-url :target "_new"} [:i.fa.fa-money-check] [:span.icon [:i.fa.fa-money]] (str " " check-number " (" (gstring/format "$%.2f" amount ) ")")]]
]))]]]))))
(def checks-page
(with-meta
(fn []
(let [current-company @(re-frame/subscribe [::subs/company])]
[:div
[:h1.title "Payments"]
[check-table {:id :checks
:params (re-frame/subscribe [::params])
:check-page (re-frame/subscribe [::check-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 {}]) }))