blob: e74d124900d850e88f17a499953cef38dc2dd877 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
(ns tau.events
(:require
[day8.re-frame.http-fx]
[re-frame.core :as rf]
[reitit.frontend.easy :as rfe]
[tau.api :as api]))
(rf/reg-event-db
::initialize-db
(fn [_ _]
{:global-search ""
:service-id 0
:service-color "#cc0000"
:stream {}
:search-results []
:services []
:current-match nil
:page-scroll 0}))
(rf/reg-fx
::scroll-to-top
(fn [_]
(.scrollTo js/window #js {"top" 0 "behavior" "smooth"})))
(rf/reg-fx
::history-back!
(fn [_]
(.back js/window.history)))
(rf/reg-event-fx
::history-back
(fn [_ _]
{::history-back! nil}))
(rf/reg-event-db
::page-scroll
(fn [db _]
(when (> (.-scrollY js/window) 0)
(assoc db :page-scroll (+ (.-scrollY js/window) (.-innerHeight js/window))))))
(rf/reg-event-db
::reset-page-scroll
(fn [db _]
(assoc db :page-scroll 0)))
(rf/reg-event-db
::toggle-mobile-nav
(fn [db _]
(assoc db :show-mobile-nav (not (:show-mobile-nav db)))))
(rf/reg-event-fx
::navigated
(fn [{:keys [db]} [_ new-match]]
{:db (-> db
(assoc :current-match new-match)
(assoc :show-pagination-loading false))
::scroll-to-top nil}))
(rf/reg-event-fx
::navigate
(fn [_ [_ route]]
{::navigate! route}))
(rf/reg-fx
::navigate!
(fn [{:keys [name params query]}]
(rfe/push-state name params query)))
(rf/reg-event-db
::bad-response
(fn [db [_ res]]
(js/console.log res)
(assoc db :http-response (get-in res [:response :error]))))
(rf/reg-event-db
::change-global-search
(fn [db [_ res]]
(assoc db :global-search res)))
(rf/reg-event-db
::change-service-color
(fn [db [_ service-id]]
(assoc db :service-color
(case service-id
0 "#cc0000"
1 "#ff7700"
2 "#333333"
3 "#F2690D"
4 "#629aa9"))))
(rf/reg-event-fx
::change-service-id
(fn [{:keys [db]} [_ service-id]]
{:db (assoc db :service-id service-id)
:fx [[:dispatch [::change-service-color service-id]]]}))
(rf/reg-event-db
::load-paginated-channel-results
(fn [db [_ res]]
(-> db
(update-in [:channel :related-streams] #(apply conj %1 %2)
(:related-streams (js->clj res :keywordize-keys true)))
(assoc-in [:channel :next-page]
(:next-page (js->clj res :keywordize-keys true)))
(assoc :show-pagination-loading false))))
(rf/reg-event-fx
::channel-pagination
(fn [{:keys [db]} [_ uri next-page-url]]
(if (empty? next-page-url)
{:db (assoc db :show-pagination-loading false)}
(assoc
(api/get-request
(str "/api/channels/" (js/encodeURIComponent uri) )
[::load-paginated-channel-results] [::bad-response]
{:nextPage (js/encodeURIComponent next-page-url)})
:db (assoc db :show-pagination-loading true)))))
(rf/reg-event-db
::load-paginated-playlist-results
(fn [db [_ res]]
(-> db
(update-in [:playlist :related-streams] #(apply conj %1 %2)
(:related-streams (js->clj res :keywordize-keys true)))
(assoc-in [:playlist :next-page]
(:next-page (js->clj res :keywordize-keys true)))
(assoc :show-pagination-loading false))))
(rf/reg-event-fx
::playlist-pagination
(fn [{:keys [db]} [_ uri next-page-url]]
(if (empty? next-page-url)
{:db (assoc db :show-pagination-loading false)}
(assoc
(api/get-request
(str "/api/playlists/" (js/encodeURIComponent uri))
[::load-paginated-playlist-results] [::bad-response]
{:nextPage (js/encodeURIComponent next-page-url)})
:db (assoc db :show-pagination-loading true)))))
(rf/reg-event-db
::load-paginated-search-results
(fn [db [_ res]]
(-> db
(update-in [:search-results :items] #(apply conj %1 %2)
(:items (js->clj res :keywordize-keys true)))
(assoc-in [:search-results :next-page]
(:next-page (js->clj res :keywordize-keys true)))
(assoc :show-pagination-loading false))))
(rf/reg-event-fx
::search-pagination
(fn [{:keys [db]} [_ query id next-page-url]]
(if (empty? next-page-url)
{:db (assoc db :show-pagination-loading false)}
(assoc
(api/get-request
(str "/api/services/" id "/search")
[::load-paginated-search-results] [::bad-response]
{:q query
:nextPage (js/encodeURIComponent next-page-url)})
:db (assoc db :show-pagination-loading true)))))
(rf/reg-event-db
::change-global-stream
(fn [db [_ global-stream]]
(assoc db :global-stream global-stream)))
(rf/reg-event-db
::toggle-global-player
(fn [db _]
(assoc db :show-global-player (not (:show-global-player db)))))
(rf/reg-event-fx
::switch-to-global-player
(fn [{:keys [db]} [_ global-stream]]
{:db (assoc db :show-global-player true)
:fx [[:dispatch [::change-global-stream global-stream]]]}))
(rf/reg-event-db
::load-services
(fn [db [_ res]]
(assoc db :services (js->clj res :keywordize-keys true))))
(rf/reg-event-fx
::get-services
(fn [{:keys [db]} _]
(api/get-request "/api/services" [::load-services] [::bad-response])))
(rf/reg-event-db
::load-comments
(fn [db [_ res]]
(-> db
(assoc-in [:stream :comments-page] (js->clj res :keywordize-keys true))
(assoc-in [:stream :show-comments-loading] false))))
(rf/reg-event-fx
::get-comments
(fn [{:keys [db]} [_ url]]
(assoc
(api/get-request (str "/api/comments/" (js/encodeURIComponent url))
[::load-comments] [::bad-response])
:db (-> db
(assoc-in [:stream :show-comments-loading] true)
(assoc-in [:stream :show-comments] true)))))
(rf/reg-event-db
::toggle-comments
(fn [db [_ res]]
(assoc-in db [:stream :show-comments] (not (-> db :stream :show-comments)))))
(rf/reg-event-db
::load-paginated-comments
(fn [db [_ res]]
(-> db
(update-in [:stream :comments-page :comments] #(apply conj %1 %2)
(:comments (js->clj res :keywordize-keys true)))
(assoc-in [:stream :comments-page :next-page]
(:next-page (js->clj res :keywordize-keys true)))
(assoc :show-pagination-loading false))))
(rf/reg-event-fx
::comments-pagination
(fn [{:keys [db]} [_ url next-page-url]]
(if (empty? next-page-url)
{:db (assoc db :show-pagination-loading false)}
(assoc
(api/get-request (str "/api/comments/" (js/encodeURIComponent url))
[::load-paginated-comments] [::bad-response]
{:nextPage (js/encodeURIComponent next-page-url)})
:db (assoc db :show-pagination-loading true)))))
(rf/reg-event-db
::load-kiosks
(fn [db [_ res]]
(assoc db :kiosks (js->clj res :keywordize-keys true))))
(rf/reg-event-fx
::get-kiosks
(fn [{:keys [db]} [_ id]]
(api/get-request (str "/api/services/" id "/kiosks") [::load-kiosks] [::bad-response])))
(rf/reg-event-db
::load-kiosk
(fn [db [_ res]]
(assoc db :kiosk (js->clj res :keywordize-keys true)
:show-page-loading false)))
(rf/reg-event-fx
::get-default-kiosk
(fn [{:keys [db]} [_ service-id]]
(assoc
(api/get-request (str "/api/services/" service-id "/default-kiosk")
[::load-kiosk] [::bad-response])
:db (assoc db :show-page-loading true))))
(rf/reg-event-fx
::get-kiosk
(fn [{:keys [db]} [_ service-id kiosk-id]]
(if kiosk-id
(assoc
(api/get-request (str "/api/services/" service-id "/kiosks/"
(js/encodeURIComponent kiosk-id))
[::load-kiosk] [::bad-response])
:db (assoc db :show-page-loading true))
{:fx [[:dispatch [::get-default-kiosk service-id]]]})))
(rf/reg-event-fx
::change-service
(fn [{:keys [db]} [_ service-id]]
{:fx [[:dispatch
[::navigate {:name :tau.routes/kiosk
:params {}
:query {:serviceId service-id}}]]]}))
(rf/reg-event-db
::load-paginated-kiosk-results
(fn [db [_ res]]
(-> db
(update-in [:kiosk :related-streams] #(apply conj %1 %2)
(:related-streams (js->clj res :keywordize-keys true)))
(assoc-in [:kiosk :next-page]
(:next-page (js->clj res :keywordize-keys true)))
(assoc :show-pagination-loading false))))
(rf/reg-event-fx
::kiosk-pagination
(fn [{:keys [db]} [_ service-id kiosk-id next-page-url]]
(if (empty? next-page-url)
{:db (assoc db :show-pagination-loading false)}
(assoc
(api/get-request
(str "/api/services/" service-id "/kiosks/" (js/encodeURIComponent kiosk-id))
[::load-paginated-kiosk-results] [::bad-response]
{:nextPage (js/encodeURIComponent next-page-url)})
:db (assoc db :show-pagination-loading true)))))
(rf/reg-event-db
::load-stream
(fn [db [_ res]]
(assoc db :stream (js->clj res :keywordize-keys true)
:show-page-loading false)))
(rf/reg-event-fx
::get-stream
(fn [{:keys [db]} [_ uri]]
(assoc
(api/get-request (str "/api/streams/" (js/encodeURIComponent uri))
[::load-stream] [::bad-response])
:db (assoc db :show-page-loading true))))
(rf/reg-event-db
::load-channel
(fn [db [_ res]]
(assoc db :channel (js->clj res :keywordize-keys true)
:show-page-loading false)))
(rf/reg-event-fx
::get-channel
(fn [{:keys [db]} [_ uri]]
(assoc
(api/get-request
(str "/api/channels/" (js/encodeURIComponent uri))
[::load-channel] [::bad-response])
:db (assoc db :show-page-loading true))))
(rf/reg-event-db
::load-playlist
(fn [db [_ res]]
(assoc db :playlist (js->clj res :keywordize-keys true)
:show-page-loading false)))
(rf/reg-event-fx
::get-playlist
(fn [{:keys [db]} [_ uri]]
(assoc
(api/get-request (str "/api/playlists/" (js/encodeURIComponent uri))
[::load-playlist] [::bad-response])
:db (assoc db :show-page-loading true))))
(rf/reg-event-db
::load-search-results
(fn [db [_ res]]
(assoc db :search-results (js->clj res :keywordize-keys true)
:show-page-loading false
:global-search "")))
(rf/reg-event-fx
::get-search-results
(fn [{:keys [db]} [_ service-id query]]
(assoc
(api/get-request (str "/api/services/" service-id "/search")
[::load-search-results] [::bad-response]
{:q query})
:db (assoc db :show-page-loading true))))
|