aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/tau/events.cljs
blob: f52ebd272cbff75fbaaa970dadbd69c683308117 (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
(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-fx
 ::navigated
 (fn [{:keys [db]} [_ new-match]]
   {::scroll-to-top nil
    :db (assoc db :current-match new-match)
    :fx [[:dispatch [::reset-page-scroll]]]}))

(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]]
   (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 [_ id]]
   (assoc db :service-color
          (case id
            0 "#cc0000"
            1 "#ff7700"
            2 "#333333"
            3 "#F2690D"
            4 "#629aa9"))))

(rf/reg-event-fx
 ::change-service-id
 (fn [{:keys [db]} [_ id]]
   {:db (assoc db :service-id id)
    :fx [[:dispatch [::change-service-color 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
 ::scroll-channel-pagination
 (fn [{:keys [db]} [_ uri next-page-url]]
   (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-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-db
 ::reset-page-scroll
 (fn [db _]
   (assoc db :page-scroll 0)))

(rf/reg-event-db
 ::page-scroll
 (fn [db _]
   (assoc db :page-scroll (+ (.-scrollY js/window) (.-innerHeight js/window)))))

(rf/reg-event-fx
 ::scroll-search-pagination
 (fn [{:keys [db]} [_ query id next-page-url]]
   (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-fx
 ::switch-to-global-player
 (fn [{:keys [db]} [_ res]]
   {:db (assoc db :show-global-player true)
    :fx [[:dispatch [::change-global-search res]]]}))

(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-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-kiosk
 (fn [{:keys [db]} [_ {:keys [service-id 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))))

(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)))

(rf/reg-event-fx
 ::get-search-results
 (fn [{:keys [db]} [_ {:keys [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))))