|
@@ -4,6 +4,25 @@
|
|
|
[tubo.api :as api]
|
|
|
[tubo.layout.views :as layout]))
|
|
|
|
|
|
+(defonce !timeouts (atom {}))
|
|
|
+
|
|
|
+(rf/reg-fx
|
|
|
+ :debounce
|
|
|
+ (fn [{:keys [id event time]}]
|
|
|
+ (js/clearTimeout (get @!timeouts id))
|
|
|
+ (swap! !timeouts assoc
|
|
|
+ id
|
|
|
+ (js/setTimeout (fn []
|
|
|
+ (rf/dispatch event)
|
|
|
+ (swap! !timeouts dissoc id))
|
|
|
+ time))))
|
|
|
+
|
|
|
+(rf/reg-fx
|
|
|
+ :stop-debounce
|
|
|
+ (fn [id]
|
|
|
+ (js/clearTimeout (get @!timeouts id))
|
|
|
+ (swap! !timeouts dissoc id)))
|
|
|
+
|
|
|
(rf/reg-event-fx
|
|
|
:search/fetch
|
|
|
(fn [_ [_ service-id on-success on-error params]]
|
|
@@ -14,10 +33,12 @@
|
|
|
|
|
|
(rf/reg-event-fx
|
|
|
:search/load-page
|
|
|
- (fn [{:keys [db]} [_ res]]
|
|
|
+ (fn [{:keys [db]} [_ {:keys [query filter]} res]]
|
|
|
(let [search-res (js->clj res :keywordize-keys true)]
|
|
|
{:db (assoc db
|
|
|
:search/results search-res
|
|
|
+ :search/query query
|
|
|
+ :search/filter filter
|
|
|
:show-page-loading false)
|
|
|
:fx [[:dispatch [:services/fetch search-res]]]})))
|
|
|
|
|
@@ -31,16 +52,23 @@
|
|
|
|
|
|
(rf/reg-event-fx
|
|
|
:search/fetch-page
|
|
|
- (fn [{:keys [db]} [_ service-id query]]
|
|
|
- {:db (assoc db
|
|
|
- :show-page-loading true
|
|
|
- :search/show-form true
|
|
|
- :search/results nil)
|
|
|
- :fx [[:dispatch
|
|
|
- [:search/fetch service-id
|
|
|
- [:search/load-page] [:search/bad-page-response service-id query]
|
|
|
- {:q query}]]
|
|
|
- [:document-title (str "Search for \"" query "\"")]]}))
|
|
|
+ (fn [{:keys [db]} [_ service-id query filter]]
|
|
|
+ (let [default-filter (-> db
|
|
|
+ :settings
|
|
|
+ :default-filter
|
|
|
+ (get (js/parseInt service-id)))]
|
|
|
+ {:db (assoc db
|
|
|
+ :show-page-loading true
|
|
|
+ :search/show-form true
|
|
|
+ :search/results nil)
|
|
|
+ :fx [[:dispatch
|
|
|
+ [:search/fetch service-id
|
|
|
+ [:search/load-page {:query query :filter filter}]
|
|
|
+ [:search/bad-page-response service-id query]
|
|
|
+ (into {:q query}
|
|
|
+ (when (or (seq filter) default-filter)
|
|
|
+ {:filter (if (seq filter) filter default-filter)}))]]
|
|
|
+ [:document-title (str "Search for \"" query "\"")]]})))
|
|
|
|
|
|
(rf/reg-event-db
|
|
|
:search/load-paginated
|
|
@@ -59,22 +87,80 @@
|
|
|
|
|
|
(rf/reg-event-fx
|
|
|
:search/fetch-paginated
|
|
|
- (fn [{:keys [db]} [_ query id next-page-url]]
|
|
|
+ (fn [{:keys [db]} [_ {:keys [query id next-page-url filter]}]]
|
|
|
(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)}]]]
|
|
|
+ (into
|
|
|
+ {:q query
|
|
|
+ :nextPage (js/encodeURIComponent next-page-url)}
|
|
|
+ (when filter
|
|
|
+ {:filter filter}))]]]
|
|
|
:db (assoc db :show-pagination-loading true)})))
|
|
|
|
|
|
+(rf/reg-event-fx
|
|
|
+ :search/leave-page
|
|
|
+ (fn []
|
|
|
+ {:fx [[:dispatch [:search/show-form false]]
|
|
|
+ [:dispatch [:search/change-filter nil]]]}))
|
|
|
+
|
|
|
(rf/reg-event-db
|
|
|
:search/show-form
|
|
|
(fn [db [_ show?]]
|
|
|
(assoc db :search/show-form show?)))
|
|
|
|
|
|
(rf/reg-event-db
|
|
|
+ :search/set-query
|
|
|
+ (fn [db [_ query]]
|
|
|
+ (assoc db :search/query query)))
|
|
|
+
|
|
|
+(rf/reg-event-fx
|
|
|
:search/change-query
|
|
|
- (fn [db [_ res]]
|
|
|
- (assoc db :search/query res)))
|
|
|
+ (fn [_ [_ query]]
|
|
|
+ {:debounce {:id :search/query
|
|
|
+ :event [:search/set-query query]
|
|
|
+ :time 150}}))
|
|
|
+
|
|
|
+(rf/reg-event-fx
|
|
|
+ :search/clear-query
|
|
|
+ (fn []
|
|
|
+ {:stop-debounce :search/query
|
|
|
+ :fx [[:dispatch [:search/change-query nil]]]}))
|
|
|
+
|
|
|
+(rf/reg-event-fx
|
|
|
+ :search/cancel
|
|
|
+ (fn []
|
|
|
+ {:stop-debounce :search/query
|
|
|
+ :fx [[:dispatch [:search/show-form false]]]}))
|
|
|
+
|
|
|
+(rf/reg-event-db
|
|
|
+ :search/change-filter
|
|
|
+ (fn [db [_ filter]]
|
|
|
+ (assoc db :search/filter filter)))
|
|
|
+
|
|
|
+(rf/reg-event-fx
|
|
|
+ :search/submit
|
|
|
+ (fn [{:keys [db]}]
|
|
|
+ (when (seq (:search/query db))
|
|
|
+ {:stop-debounce :search/query
|
|
|
+ :fx [[:dispatch
|
|
|
+ [:navigation/navigate
|
|
|
+ {:name :search-page
|
|
|
+ :params {}
|
|
|
+ :query (into {:q (:search/query db)
|
|
|
+ :serviceId (:service-id db)}
|
|
|
+ (when (seq (:search/filter db))
|
|
|
+ {:filter (:search/filter db)}))}]]]})))
|
|
|
+
|
|
|
+(rf/reg-event-fx
|
|
|
+ :search/set-filter
|
|
|
+ (fn [{:keys [db]} [_ filter]]
|
|
|
+ {:fx [[:dispatch
|
|
|
+ [:navigation/navigate
|
|
|
+ {:name :search-page
|
|
|
+ :params {}
|
|
|
+ :query {:serviceId (:service-id db)
|
|
|
+ :q (:search/query db)
|
|
|
+ :filter filter}}]]]}))
|