blob: e09e5b3033b91b3352b033fcb84764ad03079d8c (
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
|
(ns tubo.search.events
(:require
[re-frame.core :as rf]
[tubo.api :as api]))
(rf/reg-event-fx
:search/fetch
(fn [{:keys [db]} [_ service-id on-success on-error params]]
(api/get-request (str "/services/" service-id "/search")
on-success on-error params)))
(rf/reg-event-fx
:search/load-page
(fn [{:keys [db]} [_ res]]
(let [search-res (js->clj res :keywordize-keys true)]
{:db (assoc db :search-results search-res
:show-page-loading false)
:fx [[:dispatch [:services/fetch search-res]]]})))
(rf/reg-event-fx
:search/fetch-page
(fn [{:keys [db]} [_ service-id query]]
{:db (assoc db
:show-page-loading true
:show-search-form true)
:fx [[:dispatch [:search/fetch service-id
[:search/load-page] [:bad-response] {:q query}]]
[:document-title (str "Search for \"" query "\"")]]}))
(rf/reg-event-db
:search/load-paginated
(fn [db [_ res]]
(let [search-res (js->clj res :keywordize-keys true)]
(if (empty? (:items search-res))
(-> db
(assoc-in [:search-results :next-page] nil)
(assoc :show-pagination-loading false))
(-> db
(update-in [:search-results :items] #(apply conj %1 %2)
(:items search-res))
(assoc-in [:search-results :next-page] (:next-page search-res))
(assoc :show-pagination-loading false))))))
(rf/reg-event-fx
:search/fetch-paginated
(fn [{:keys [db]} [_ query id next-page-url]]
(if (empty? next-page-url)
{:db (assoc db :show-pagination-loading false)}
{:fx [[:dispatch [:search/fetch id
[:search/load-paginated] [:bad-response]
{:q query
:nextPage (js/encodeURIComponent next-page-url)}]]]
:db (assoc db :show-pagination-loading true)})))
(rf/reg-event-db
:search/show-form
(fn [db [_ show?]]
(when-not (= (-> db :current-match :path) "search")
(assoc db :show-search-form show?))))
(rf/reg-event-db
:search/change-query
(fn [db [_ res]]
(assoc db :search-query res)))
|