Pagination

This commit is contained in:
kagel 2016-09-28 04:12:33 +03:00
parent 161b5b8052
commit e65fabf60e
4 changed files with 77 additions and 27 deletions

View File

@ -1,10 +1,13 @@
(ns commiteth.db) (ns commiteth.db)
(def default-db (def default-db
{:page :issues {:page :issues
:user nil :user nil
:user-profile nil :user-profile nil
:repos [] :repos []
:enabled-repos {} :enabled-repos {}
:all-bounties [] :all-bounties []
:owner-bounties []}) :owner-bounties []
:pagination {}
:pagination-props {:page-size 10
:pages-max 10}})

View File

@ -26,6 +26,19 @@
(fn [db [_ page]] (fn [db [_ page]]
(assoc db :page page))) (assoc db :page page)))
(reg-event-db
:set-page
(fn [db [_ table page]]
(assoc-in db [:pagination table :page] page)))
(reg-event-db
:init-pagination
(fn [db [_ bounties]]
(let [{page-size :page-size} (:pagination-props db)]
(assoc-in db [:pagination :all-bounties]
{:page 0
:pages (Math/ceil (/ (count bounties) page-size))}))))
(reg-event-fx (reg-event-fx
:set-active-user :set-active-user
(fn [{:keys [db]} [_ user]] (fn [{:keys [db]} [_ user]]
@ -43,10 +56,11 @@
:url "/api/bounties" :url "/api/bounties"
:on-success #(dispatch [:set-bounties %])}})) :on-success #(dispatch [:set-bounties %])}}))
(reg-event-db (reg-event-fx
:set-bounties :set-bounties
(fn [db [_ bounties]] (fn [{:keys [db]} [_ bounties]]
(assoc db :all-bounties bounties))) {:db (assoc db :all-bounties bounties)
:dispatch [:init-pagination bounties]}))
(reg-event-fx (reg-event-fx
:load-owner-bounties :load-owner-bounties

View File

@ -2,20 +2,43 @@
(:require [reagent.core :as r] (:require [reagent.core :as r]
[re-frame.core :as rf])) [re-frame.core :as rf]))
(defn pagination [] (defn- page-btn
[table selected-page current-page]
^{:key current-page}
[:a {:class (when (= selected-page current-page) "current")
:on-click #(rf/dispatch [:set-page table current-page])}
(str (inc current-page))])
(defn pagination [table]
(fn [] (fn []
[:div.paginate-container (let [{page :page
[:div.pagination pages :pages} @(rf/subscribe [:pagination table])
[:span.previous-page.disabled "Previous"] {pages-max :pages-max} @(rf/subscribe [:get-in [:pagination-props]])]
[:em.current "1"] (when (> pages 1)
[:a {:rel "next", :href "#"} "2"] (let [last-page (dec pages)
[:a {:href "#"} "3"] start-page (max 1 (min (- pages pages-max) (max 1 (- page (quot pages-max 2)))))
[:a {:href "#"} "4"] end-page (min (dec pages) (+ pages-max start-page))
[:a {:href "#"} "5"] gap-left (> start-page 1)
[:span.gap "…"] gap-right (< end-page last-page)]
[:a {:href "#"} "10"] [:div.paginate-container
[:a {:href "#"} "11"] [:div.pagination
[:a.next-page {:rel "next", :href "#"} "Next"]]])) [:span.previous-page
(let [disabled (= page 0)]
{:class (when disabled "disabled")
:on-click (when-not disabled
#(rf/dispatch [:set-page table (dec page)]))})
"Previous"]
(page-btn table page 0)
(when gap-left [:span.gap "…"])
(map #(page-btn table page %) (range start-page end-page))
(when gap-right [:span.gap "…"])
(page-btn table page last-page)
[:span.next-page
(let [disabled (= page last-page)]
{:class (when disabled "disabled")
:on-click (when-not disabled
#(rf/dispatch [:set-page table (inc page)]))})
"Next"]]])))))
(defn repo-link (defn repo-link
[owner repo] [owner repo]
@ -63,14 +86,19 @@
(defn issues-list-table (defn issues-list-table
[issues-path issue-row-fn] [issues-path issue-row-fn]
(let [issues (rf/subscribe issues-path)] (fn []
(fn [] (let [{page :page} @(rf/subscribe (into [:pagination] issues-path))
{page-size :page-size} @(rf/subscribe [:get-in [:pagination-props]])
issues (rf/subscribe issues-path)
issues-page (->> @issues
(drop (* page page-size))
(take page-size))]
[:div.issues-list-table [:div.issues-list-table
[:ul.issues-list [:ul.issues-list
(map issue-row-fn @issues)]]))) (map issue-row-fn issues-page)]])))
(defn issues-page [] (defn issues-page []
(fn [] (fn []
[:div.container [:div.container
[(issues-list-table [:all-bounties] issue-row)] [(issues-list-table [:all-bounties] issue-row)]
[pagination]])) [(pagination :all-bounties)]]))

View File

@ -26,6 +26,11 @@
(fn [db _] (fn [db _]
(:owner-bounties db))) (:owner-bounties db)))
(reg-sub
:pagination
(fn [db [_ table]]
(get-in db [:pagination table])))
(reg-sub (reg-sub
:get-in :get-in
(fn [db [_ path]] (fn [db [_ path]]