pagination.

This commit is contained in:
Bryce Covert
2018-04-13 15:16:37 -07:00
parent 330a276f60
commit 121a38f20d
5 changed files with 139 additions and 64 deletions

View File

@@ -71,12 +71,23 @@
q))) q)))
(defn get-graphql [{:keys [imported company-id sort-by asc]}]
(defn base-graphql [{:keys [imported company-id]}]
(cond-> base-query
(not (nil? imported)) (helpers/merge-where [:= :imported imported])
(not (nil? company-id)) (helpers/merge-where [:= :company-id company-id])
))
(defn get-graphql [{:keys [start sort-by asc] :as args}]
(query (query
(cond-> base-query (cond-> (base-graphql args)
(not (nil? imported)) (helpers/merge-where [:= :imported imported]) (not (nil? sort-by) ) (add-sort-by sort-by asc)
(not (nil? company-id)) (helpers/merge-where [:= :company-id company-id]) true (assoc :limit 20)
(not (nil? sort-by) ) (add-sort-by sort-by asc)))) start (assoc :offset start))))
(defn count-graphql [args]
(:count (first (query
(assoc (base-graphql args) :select [:%count.*])))))
(defn import [parsed-invoices companies vendors] (defn import [parsed-invoices companies vendors]
(insert-multi! (insert-multi!

View File

@@ -36,15 +36,22 @@
:resolve :get-vendor-for-invoice} :resolve :get-vendor-for-invoice}
:company {:type :company :company {:type :company
:resolve :get-company-for-invoice}}}} :resolve :get-company-for-invoice}}}
:invoice_page {:fields {:invoices {:type '(list :invoice)}
:count {:type 'Int}
:total {:type 'Int}
:start {:type 'Int}
:end {:type 'Int}}}}
:queries :queries
{:invoice {:type '(list :invoice) {:invoice_page {:type '(list :invoice_page)
:args {:imported {:type 'Boolean} :args {:imported {:type 'Boolean}
:company_id {:type 'Int} :company_id {:type 'Int}
:sort_by {:type 'String} :start {:type 'Int}
:asc {:type 'Boolean}} :sort_by {:type 'String}
:asc {:type 'Boolean}}
:resolve :get-invoice}
:resolve :get-invoice-page}
:company {:type '(list :company) :company {:type '(list :company)
:resolve :get-company} :resolve :get-company}
:vendor {:type '(list :vendor) :vendor {:type '(list :vendor)
@@ -93,17 +100,22 @@
node)) node))
m)) m))
(defn get-invoice [context args value] (defn get-invoice-page [context args value]
(println (<-graphql args))
(let [extra-context (let [extra-context
(cond-> {} (cond-> {}
(executor/selects-field? context :invoice/vendor) (assoc :vendor-cache (by (vendors/get-all) :id )) (executor/selects-field? context :invoice/vendor) (assoc :vendor-cache (by (vendors/get-all) :id ))
(executor/selects-field? context :invoice/company) (assoc :company-cache (by (companies/get-all) :id )))] (executor/selects-field? context :invoice/company) (assoc :company-cache (by (companies/get-all) :id )))
invoices (map
->graphql
(invoices/get-graphql (<-graphql args)))
invoice-count (invoices/count-graphql (<-graphql args))]
(resolve/with-context (resolve/with-context
(map [{:invoices invoices
->graphql :total invoice-count
(invoices/get-graphql (<-graphql args))) extra-context))) :count (count invoices)
:start (:start args 0)
:end (+ (:start args 0) (count invoices))}] extra-context)))
(defn get-vendor-for-invoice [context args value] (defn get-vendor-for-invoice [context args value]
(->graphql (->graphql
@@ -127,7 +139,7 @@
(def schema (def schema
(-> integreat-schema (-> integreat-schema
(attach-resolvers {:get-invoice get-invoice (attach-resolvers {:get-invoice-page get-invoice-page
:get-vendor-for-invoice get-vendor-for-invoice :get-vendor-for-invoice get-vendor-for-invoice
:get-company-for-invoice get-company-for-invoice :get-company-for-invoice get-company-for-invoice
:get-company get-company :get-company get-company

View File

@@ -3,6 +3,7 @@
[auto-ap.subs :as subs] [auto-ap.subs :as subs]
[auto-ap.views.utils :refer [date->str]] [auto-ap.views.utils :refer [date->str]]
[reagent.core :as reagent] [reagent.core :as reagent]
[clojure.string :as str]
[cljs-time.format :as format])) [cljs-time.format :as format]))
(defn toggle-sort-by [params key] (defn toggle-sort-by [params key]
@@ -26,39 +27,90 @@
[:i.fa.fa-sort]])) [:i.fa.fa-sort]]))
(defn query [params] (defn query [params]
{:venia/queries [[:invoice {:venia/queries [[:invoice_page
(assoc params (assoc params
:company-id (:id @(re-frame/subscribe [::subs/company]))) :company-id (:id @(re-frame/subscribe [::subs/company])))
[:id :total :invoice-number :date [:vendor [:name :id]] [:company [:name :id]]]]]}) [[:invoices [:id :total :invoice-number :date [:vendor [:name :id]] [:company [:name :id]]]]
:total
:start
:end]]]})
(defn invoice-table [{:keys [id invoices status on-params-change vendors params]}] (defn bound [x y z]
(cond
(< z x)
x
(< y x)
x
(> y z)
z
:else
y))
(defn buttons-for [start end count total on-params-change state]
(let [per-page 20
max-buttons 5
buttons-before (Math/floor (/ max-buttons 2))
total-pages (Math/ceil (/ total per-page))
current-page (Math/floor (/ start per-page))
first-page-button (bound 0 (- current-page buttons-before) (- total-pages max-buttons))
all-buttons (into [] (for [x (range total-pages)]
^{:key x} [:button.button {:class (str/join " " [(when (= current-page x)
"is-active")])
:on-click (fn [e] (on-params-change (swap! state assoc :start (* x per-page))))} (inc x)]))
last-page-button (Math/min total-pages (+ max-buttons first-page-button))
extended-last-page-button (when (not= last-page-button total-pages)
[:span "..." [:button.button {:on-click (fn [e] (on-params-change (swap! state assoc :start (* (dec total-pages) per-page))))} total-pages]])
extended-first-page-button (when (not= first-page-button 0)
[:span [:button.button {:on-click (fn [e] (on-params-change (swap! state assoc :start 0)))} 1] "..."])
]
[:div.is-pulled-right
[:div
extended-first-page-button
(apply list (subvec all-buttons first-page-button last-page-button))
extended-last-page-button]]
))
(defn invoice-table [{:keys [id invoice-page status on-params-change vendors params]}]
(let [state (reagent/atom (or @params {}))] (let [state (reagent/atom (or @params {}))]
(fn [{:keys [id invoices status on-params-change vendors]}] (fn [{:keys [id invoice-page status on-params-change vendors]}]
(let [{:keys [sort-by asc]} @state] (let [{:keys [sort-by asc]} @state
[:table.table.is-fullwidth {:keys [invoices start end count total]} @invoice-page]
[:thead [:div
[:tr [buttons-for start end count total on-params-change state]
[:th {:on-click (fn [e] "Showing " (inc start) "-" end "/" total
(on-params-change (swap! state toggle-sort-by "vendor"))) :style {:width "25%" :cursor "pointer"}} "Vendor"
(sort-icon "vendor" sort-by asc)] [:table.table.is-fullwidth
[:th {:on-click (fn [e] (on-params-change (swap! state toggle-sort-by "company"))) :style {:width "25%" :cursor "pointer"}} "Company" [:thead
(sort-icon "company" sort-by asc)] [:tr
[:th {:on-click (fn [e] (on-params-change (swap! state toggle-sort-by "invoice-number"))) :style {:width "18%" :cursor "pointer"}} "Invoice #" [:th {:on-click (fn [e]
(sort-icon "invoice-number" sort-by asc)] (on-params-change (swap! state toggle-sort-by "vendor"))) :style {:width "25%" :cursor "pointer"}} "Vendor"
[:th {:on-click (fn [e] (on-params-change (swap! state toggle-sort-by "date"))) :style {:width "16%" :cursor "pointer"}} "Date" (sort-icon "vendor" sort-by asc)]
(sort-icon "date" sort-by asc)] [:th {:on-click (fn [e] (on-params-change (swap! state toggle-sort-by "company"))) :style {:width "25%" :cursor "pointer"}} "Company"
[:th {:on-click (fn [e] (on-params-change (swap! state toggle-sort-by "total"))) :style {:width "16%" :cursor "pointer"}} "Amount" (sort-icon "company" sort-by asc)]
(sort-icon "total" sort-by asc)]]] [:th {:on-click (fn [e] (on-params-change (swap! state toggle-sort-by "invoice-number"))) :style {:width "18%" :cursor "pointer"}} "Invoice #"
[:tbody (sort-icon "invoice-number" sort-by asc)]
(if (:loading @status) [:th {:on-click (fn [e] (on-params-change (swap! state toggle-sort-by "date"))) :style {:width "16%" :cursor "pointer"}} "Date"
[:tr (sort-icon "date" sort-by asc)]
[:td {:col-span 5} [:th {:on-click (fn [e] (on-params-change (swap! state toggle-sort-by "total"))) :style {:width "16%" :cursor "pointer"}} "Amount"
[:i.fa.fa-spin.fa-spinner]]] (sort-icon "total" sort-by asc)]]]
(for [{:keys [company invoice-number date total id vendor] :as i} @invoices] [:tbody
^{:key id} (if (:loading @status)
[:tr [:tr
[:td (:name vendor)] [:td {:col-span 5}
[:td (:name company)] [:i.fa.fa-spin.fa-spinner]]]
[:td invoice-number] (for [{:keys [company invoice-number date total id vendor] :as i} (:invoices @invoice-page)]
[:td (date->str date) ] ^{:key id}
[:td total]]))]])))) [:tr
[:td (:name vendor)]
[:td (:name company)]
[:td invoice-number]
[:td (date->str date) ]
[:td total]]))]]]))))

View File

@@ -34,9 +34,9 @@
:previewTemplate "<div class='dz-hidden-preview'></div>"})))}))) :previewTemplate "<div class='dz-hidden-preview'></div>"})))})))
(re-frame/reg-sub (re-frame/reg-sub
::invoices ::invoice-page
(fn [db] (fn [db]
(-> db ::invoices))) (-> db ::invoice-page)))
(re-frame/reg-sub (re-frame/reg-sub
::params ::params
@@ -63,7 +63,7 @@
::received ::received
(fn [db [_ data]] (fn [db [_ data]]
(-> db (-> db
(assoc ::invoices (:invoice data)) (assoc ::invoice-page (first (:invoice-page data)))
(assoc-in [:status :loading] false)))) (assoc-in [:status :loading] false))))
(re-frame/reg-event-fx (re-frame/reg-event-fx
@@ -91,7 +91,7 @@
(def import-invoices-page (def import-invoices-page
(with-meta (with-meta
(fn [] (fn []
(let [invoices (re-frame/subscribe [::invoices]) (let [invoice-page (re-frame/subscribe [::invoice-page])
status (re-frame/subscribe [::subs/status])] status (re-frame/subscribe [::subs/status])]
[:div {:class "inbox-messages"} [:div {:class "inbox-messages"}
[:h1.title "Upload invoices"] [:h1.title "Upload invoices"]
@@ -105,15 +105,15 @@
(if (:loading @status) (if (:loading @status)
[:h1.title [:h1.title
[:i.fa.fa-spin.fa-spinner]] [:i.fa.fa-spin.fa-spinner]]
(if (seq @invoices) (if (seq (:invoices @invoice-page))
[invoice-table {:id :approved [invoice-table {:id :approved
:invoices invoices :invoice-page invoice-page
:status (re-frame/subscribe [::subs/status]) :status (re-frame/subscribe [::subs/status])
:params (re-frame/subscribe [::params]) :params (re-frame/subscribe [::params])
:on-params-change (fn [params] :on-params-change (fn [params]
(re-frame/dispatch [::params-change params])) }] (re-frame/dispatch [::params-change params])) }]
[:span "No pending invoices"]))] [:span "No pending invoices"]))]
(if (and (seq @invoices) (not (:loading @status))) (if (and (seq (:invoices @invoice-page)) (not (:loading @status)))
[:div.card-footer [:div.card-footer
[:a.card-footer-item [:a.card-footer-item
{:on-click (fn [e] {:on-click (fn [e]

View File

@@ -8,9 +8,9 @@
[auto-ap.events :as events])) [auto-ap.events :as events]))
(re-frame/reg-sub (re-frame/reg-sub
::invoices ::invoice-page
(fn [db] (fn [db]
(-> db ::invoices))) (-> db ::invoice-page)))
(re-frame/reg-sub (re-frame/reg-sub
::params ::params
@@ -31,7 +31,7 @@
::received ::received
(fn [db [_ data]] (fn [db [_ data]]
(-> db (-> db
(assoc ::invoices (:invoice data)) (assoc ::invoice-page (first (:invoice-page data)))
(assoc-in [:status :loading] false)))) (assoc-in [:status :loading] false))))
(re-frame/reg-event-fx (re-frame/reg-event-fx
@@ -46,7 +46,7 @@
[:h1.title "Unpaid invoices"] [:h1.title "Unpaid invoices"]
[invoice-table {:id :unpaid [invoice-table {:id :unpaid
:params (re-frame/subscribe [::params]) :params (re-frame/subscribe [::params])
:invoices (re-frame/subscribe [::invoices]) :invoice-page (re-frame/subscribe [::invoice-page])
:status (re-frame/subscribe [::subs/status]) :status (re-frame/subscribe [::subs/status])
:on-params-change (fn [params] :on-params-change (fn [params]
(re-frame/dispatch [::params-change params])) }]]) (re-frame/dispatch [::params-change params])) }]])