Skip to content

Commit 655ee26

Browse files
committed
buttons for sort order
1 parent 972f1d5 commit 655ee26

File tree

9 files changed

+173
-76
lines changed

9 files changed

+173
-76
lines changed

app/views/cljs_om.scala.html

+1-9
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,7 @@
113113
<div id="pagination"></div>
114114
</div>
115115

116-
<div class="btn-group">
117-
<button type="button" class="btn">Sort by</button>
118-
<button type="button" class="btn btn-primary" data-btn-radio="'latest'"
119-
onclick="BirdWatch.sortBy('latest')">latest</button>
120-
<button type="button" class="btn btn-primary" data-btn-radio="'followers'"
121-
onclick="BirdWatch.sortBy('followers')">followers</button>
122-
<button type="button" class="btn btn-primary" data-btn-radio="'retweets'"
123-
onclick="BirdWatch.sortBy('retweets')">retweets</button>
124-
</div>
116+
<div id="sort-buttons"></div>
125117

126118
<!-- Main page layout -->
127119
<div class="row">

cljs-om/src/cljs_om/core.cljs

+16-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
:by-retweets (sorted-set-by (fn [x y] (> (:retweet_count x) (:retweet_count y))))
1313
:by-favorites (sorted-set-by (fn [x y] (> (:favorite_count x) (:favorite_count y))))
1414
:by-id (sorted-set-by >)
15-
:n 5
16-
:sorted :by-favorites }))
15+
:n 10
16+
:sorted :by-followers}))
1717

1818
(om/root
1919
ui/tweets-view
@@ -25,12 +25,25 @@
2525
app-state
2626
{:target (. js/document (getElementById "tweet-count"))})
2727

28+
(om/root
29+
ui/sort-buttons-view
30+
app-state
31+
{:target (. js/document (getElementById "sort-buttons"))})
32+
2833
(defn add-to-tweets-map [tweet]
2934
(swap! app-state assoc-in [:tweets-map (keyword (:id_str tweet))] (util/format-tweet tweet)))
3035

3136
(defn add-rt-status [tweet]
3237
(if (contains? tweet :retweeted_status)
33-
(let [rt (:retweeted_status tweet)]
38+
(let [rt (:retweeted_status tweet) prev ((keyword (:id_str rt)) (:tweets-map @app-state))]
39+
(if (not (nil? prev))
40+
(do
41+
(swap! app-state assoc :by-retweets (disj (:by-retweets @app-state)
42+
{:retweet_count (:retweet_count prev)
43+
:id (:id_str rt)})))
44+
(swap! app-state assoc :by-favorites (disj (:by-favorites @app-state)
45+
{:favorite_count (:favorite_count prev)
46+
:id (:id_str rt)})))
3447
(add-to-tweets-map rt)
3548
(swap! app-state assoc :by-retweets (conj (:by-retweets @app-state)
3649
{:retweet_count (:retweet_count rt)

cljs-om/src/cljs_om/ui.cljs

+16
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@
3333
:by-retweets tweets-by-retweets
3434
:by-favorites tweets-by-favorites})
3535

36+
(defn sort-button [app key]
37+
#js {:onClick (fn [e] (om/update! app [:sorted] key))
38+
:className (str "btn " (if (= key (:sorted app)) "btn-primary"))})
39+
40+
(defn sort-buttons-view [app owner]
41+
"rendering sort buttons"
42+
(reify
43+
om/IRender
44+
(render [this]
45+
(dom/div #js {:className "btn-group"}
46+
(dom/button #js {:className "btn"} "Sort by")
47+
(dom/button (sort-button app :by-id) "latest")
48+
(dom/button (sort-button app :by-followers) "followers")
49+
(dom/button (sort-button app :by-retweets) "retweets")
50+
(dom/button (sort-button app :by-favorites) "favorites")))))
51+
3652
(defn tweet-view [tweet owner]
3753
"rendering single tweet card"
3854
(reify

public/cljs/out/cljs_om/core.cljs

+16-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
:by-retweets (sorted-set-by (fn [x y] (> (:retweet_count x) (:retweet_count y))))
1313
:by-favorites (sorted-set-by (fn [x y] (> (:favorite_count x) (:favorite_count y))))
1414
:by-id (sorted-set-by >)
15-
:n 5
16-
:sorted :by-favorites }))
15+
:n 10
16+
:sorted :by-followers}))
1717

1818
(om/root
1919
ui/tweets-view
@@ -25,12 +25,25 @@
2525
app-state
2626
{:target (. js/document (getElementById "tweet-count"))})
2727

28+
(om/root
29+
ui/sort-buttons-view
30+
app-state
31+
{:target (. js/document (getElementById "sort-buttons"))})
32+
2833
(defn add-to-tweets-map [tweet]
2934
(swap! app-state assoc-in [:tweets-map (keyword (:id_str tweet))] (util/format-tweet tweet)))
3035

3136
(defn add-rt-status [tweet]
3237
(if (contains? tweet :retweeted_status)
33-
(let [rt (:retweeted_status tweet)]
38+
(let [rt (:retweeted_status tweet) prev ((keyword (:id_str rt)) (:tweets-map @app-state))]
39+
(if (not (nil? prev))
40+
(do
41+
(swap! app-state assoc :by-retweets (disj (:by-retweets @app-state)
42+
{:retweet_count (:retweet_count prev)
43+
:id (:id_str rt)})))
44+
(swap! app-state assoc :by-favorites (disj (:by-favorites @app-state)
45+
{:favorite_count (:favorite_count prev)
46+
:id (:id_str rt)})))
3447
(add-to-tweets-map rt)
3548
(swap! app-state assoc :by-retweets (conj (:by-retweets @app-state)
3649
{:retweet_count (:retweet_count rt)

public/cljs/out/cljs_om/core.js

+8-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/cljs/out/cljs_om/core.js.map

+10-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/cljs/out/cljs_om/ui.cljs

+16
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@
3333
:by-retweets tweets-by-retweets
3434
:by-favorites tweets-by-favorites})
3535

36+
(defn sort-button [app key]
37+
#js {:onClick (fn [e] (om/update! app [:sorted] key))
38+
:className (str "btn " (if (= key (:sorted app)) "btn-primary"))})
39+
40+
(defn sort-buttons-view [app owner]
41+
"rendering sort buttons"
42+
(reify
43+
om/IRender
44+
(render [this]
45+
(dom/div #js {:className "btn-group"}
46+
(dom/button #js {:className "btn"} "Sort by")
47+
(dom/button (sort-button app :by-id) "latest")
48+
(dom/button (sort-button app :by-followers) "followers")
49+
(dom/button (sort-button app :by-retweets) "retweets")
50+
(dom/button (sort-button app :by-favorites) "favorites")))))
51+
3652
(defn tweet-view [tweet owner]
3753
"rendering single tweet card"
3854
(reify

0 commit comments

Comments
 (0)