File tree 9 files changed +173
-76
lines changed
9 files changed +173
-76
lines changed Original file line number Diff line number Diff line change 113
113
< div id ="pagination "> </ div >
114
114
</ div >
115
115
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 >
125
117
126
118
<!-- Main page layout -->
127
119
< div class ="row ">
Original file line number Diff line number Diff line change 12
12
:by-retweets (sorted-set-by (fn [x y] (> (:retweet_count x) (:retweet_count y))))
13
13
:by-favorites (sorted-set-by (fn [x y] (> (:favorite_count x) (:favorite_count y))))
14
14
:by-id (sorted-set-by >)
15
- :n 5
16
- :sorted :by-favorites }))
15
+ :n 10
16
+ :sorted :by-followers }))
17
17
18
18
(om/root
19
19
ui/tweets-view
25
25
app-state
26
26
{:target (. js/document (getElementById " tweet-count" ))})
27
27
28
+ (om/root
29
+ ui/sort-buttons-view
30
+ app-state
31
+ {:target (. js/document (getElementById " sort-buttons" ))})
32
+
28
33
(defn add-to-tweets-map [tweet]
29
34
(swap! app-state assoc-in [:tweets-map (keyword (:id_str tweet))] (util/format-tweet tweet)))
30
35
31
36
(defn add-rt-status [tweet]
32
37
(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)})))
34
47
(add-to-tweets-map rt)
35
48
(swap! app-state assoc :by-retweets (conj (:by-retweets @app-state)
36
49
{:retweet_count (:retweet_count rt)
Original file line number Diff line number Diff line change 33
33
:by-retweets tweets-by-retweets
34
34
:by-favorites tweets-by-favorites})
35
35
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
+
36
52
(defn tweet-view [tweet owner]
37
53
" rendering single tweet card"
38
54
(reify
Original file line number Diff line number Diff line change 12
12
:by-retweets (sorted-set-by (fn [x y] (> (:retweet_count x) (:retweet_count y))))
13
13
:by-favorites (sorted-set-by (fn [x y] (> (:favorite_count x) (:favorite_count y))))
14
14
:by-id (sorted-set-by >)
15
- :n 5
16
- :sorted :by-favorites }))
15
+ :n 10
16
+ :sorted :by-followers }))
17
17
18
18
(om/root
19
19
ui/tweets-view
25
25
app-state
26
26
{:target (. js/document (getElementById " tweet-count" ))})
27
27
28
+ (om/root
29
+ ui/sort-buttons-view
30
+ app-state
31
+ {:target (. js/document (getElementById " sort-buttons" ))})
32
+
28
33
(defn add-to-tweets-map [tweet]
29
34
(swap! app-state assoc-in [:tweets-map (keyword (:id_str tweet))] (util/format-tweet tweet)))
30
35
31
36
(defn add-rt-status [tweet]
32
37
(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)})))
34
47
(add-to-tweets-map rt)
35
48
(swap! app-state assoc :by-retweets (conj (:by-retweets @app-state)
36
49
{:retweet_count (:retweet_count rt)
Original file line number Diff line number Diff line change 33
33
:by-retweets tweets-by-retweets
34
34
:by-favorites tweets-by-favorites})
35
35
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
+
36
52
(defn tweet-view [tweet owner]
37
53
" rendering single tweet card"
38
54
(reify
You can’t perform that action at this time.
0 commit comments