-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
routes.cljs
110 lines (91 loc) · 3.18 KB
/
routes.cljs
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
(ns memory-hole.routes
(:require [re-frame.core :refer [dispatch dispatch-sync subscribe]]
[goog.events :as events]
[goog.history.EventType :as HistoryEventType]
[secretary.core :as secretary]
[accountant.core :as accountant])
(:import goog.History))
(defn logged-in? []
@(subscribe [:user]))
(defn run-events [events]
(doseq [event events]
(if (logged-in?)
(dispatch event)
(dispatch [:add-login-event event]))))
(defn context-url [url]
(str js/context url))
(defn href [url]
{:href (str js/context url)})
(defn navigate! [url]
(accountant/navigate! (context-url url)))
;; Why do you need to scrollTo on these events?
(defn home-page-events [& events]
(.scrollTo js/window 0 0)
(run-events (into
[[:load-tags]
[:load-groups]
[:set-active-page :home]]
events)))
;; -------------------------
;; Routes
(secretary/defroute (context-url "/") []
(home-page-events [:select-tag "Recent"]
[:load-issues :recent]))
(secretary/defroute (context-url "/search/:query") [query]
(home-page-events [:load-issues :search query]))
(secretary/defroute (context-url "/all-issues") []
(home-page-events [:select-tag "All"]
[:load-issues :all]))
(secretary/defroute (context-url "/recent-issues") []
(home-page-events [:select-tag "Recent"]
[:load-issues :recent]))
(secretary/defroute (context-url "/most-viewed-issues") []
(home-page-events [:select-tag "Most Viewed"]
[:load-issues :most-viewed]))
(secretary/defroute (context-url "/issues/:tag") [tag]
(home-page-events [:select-tag tag]
[:load-issues :tag tag]))
(secretary/defroute (context-url "/create-issue") []
(dispatch [:close-issue])
(run-events
[[:load-tags]
[:load-groups]
[:set-active-page :edit-issue]]))
(secretary/defroute (context-url "/edit-issue") []
(if-not (or (logged-in?)
(nil? @(subscribe [:issue])))
(navigate! "/")
(run-events [[:load-tags]
[:load-groups]
[:set-active-page :edit-issue]])))
(secretary/defroute (context-url "/issue/:id") [id]
(dispatch [:close-issue])
(run-events [[:load-tags]
[:load-groups]
[:load-issue (js/parseInt id)]
[:set-active-page :view-issue]]))
(secretary/defroute (context-url "/users") []
(run-events [[:load-groups]
[:set-active-page :users]]))
(secretary/defroute (context-url "/groups") []
(run-events [[:load-groups]
[:set-active-page :groups]]))
;; Consider adding a group-detail page for batch removing/adding users to groups
;; -------------------------
;; History
;; must be called after routes have been defined
(defn hook-browser-navigation! []
(doto (History.)
(events/listen
HistoryEventType/NAVIGATE
(fn [event]
(secretary/dispatch! (.-token event))))
(.setEnabled true))
(accountant/configure-navigation!
{:nav-handler
(fn [path]
(secretary/dispatch! path))
:path-exists?
(fn [path]
(secretary/locate-route path))})
(accountant/dispatch-current!))