-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.cljs
244 lines (206 loc) · 6.72 KB
/
login.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
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
(ns app.login
(:require ["react-dom/client" :as rdom]
[app.lib :refer [defnc]]
[clojure.pprint :refer [pprint]]
[helix.core :refer [$]]
[helix.dom :as d]
[helix.hooks :as hooks]
[refx.alpha :as refx]
[refx.db :as db]
[reitit.coercion.schema :as rsc]
[reitit.frontend :as rf]
[reitit.frontend.controllers :as rfc]
[reitit.frontend.easy :as rfe]
[schema.core :as s]))
;;; Effects ;;;
;; Triggering navigation from events.
(refx/reg-fx :push-state
(fn [route]
(apply rfe/push-state route)))
;;; Events ;;;
(refx/reg-event-db ::initialize-db
(fn [db _]
(if db
db
{:current-route nil
:current-user nil})))
(refx/reg-event-fx ::push-state
(fn [_ [_ & route]]
{:push-state route}))
(refx/reg-event-db ::navigated
(fn [db [_ new-match]]
(let [old-match (:current-route db)
controllers (rfc/apply-controllers (:controllers old-match) new-match)]
(assoc db :current-route (assoc new-match :controllers controllers)))))
(refx/reg-event-fx
::login-done
(fn
[{db :db} [_ [_ response]]]
{:db (-> db
(assoc :login-loading? false)
(assoc :current-user response))}))
(refx/reg-event-db
::login-error
(fn
[db _]
(-> db
(assoc :login-loading? false)
(assoc :current-user nil))))
(refx/reg-fx
:login
(fn [{:keys [user on-success]} _]
(js/setTimeout #(refx/dispatch (conj on-success user)) 500)))
(refx/reg-event-fx
::login
(fn
[{db :db} user]
;; we return a map of (side) effects
{:login {:user user
:on-success [::login-done]
:on-failure [::login-error]}
:db (assoc db :login-loading? true)}))
(refx/reg-event-db
::logout
(fn
[db _]
(-> db
(assoc :current-user nil))))
;;; Subscriptions ;;;
(refx/reg-sub ::current-user
(fn [db]
(:current-user db)))
(refx/reg-sub ::current-route
(fn [db]
(:current-route db)))
(refx/reg-sub ::login-loading
(fn [db]
(:login-loading? db)))
;;; Views ;;;
(defnc home-page []
(d/div
(d/h2 "Welcome to frontend")
(d/p "Look at console log for controller calls.")))
(defnc item-page [match]
(let [{:keys [path query]} (:parameters match)
{:keys [id]} path]
(d/div
(d/ul
(d/li (d/a {:href (rfe/href ::item {:id 1})} "Item 1"))
(d/li (d/a {:href (rfe/href ::item {:id 2} {:foo "bar"})} "Item 2")))
(when id
(d/h2 "Selected item " id))
(when (:foo query)
(d/p "Optional foo query param: " (:foo query))))))
(defnc login-view []
(let [loading? (refx/use-sub [::login-loading])
[state set-state] (hooks/use-state {:username "" :password ""})]
(d/div
(d/form
{:disabled loading?
:on-submit (fn [e]
(.preventDefault e)
(when (and (:username state)
(:password state))
(refx/dispatch [::login state])))}
(d/label "Username")
(d/input
{:value (:username state)
:disabled loading?
:on-change #(set-state assoc :username (.. % -target -value))})
(d/label "Password")
(d/input
{:value (:password state)
:disabled loading?
:on-change #(set-state assoc :password (.. % -target -value))})
(d/button
{:type "submit"}
"Login")
(when loading?
(d/p "Loading..."))))))
(defnc about-page []
(d/div
(d/p "This view is public.")))
(defnc main-view []
(let [{:keys [username] :as user} (refx/use-sub [::current-user])
match (refx/use-sub [::current-route])
route-data (:data match)]
(d/div
(d/ul
(d/li (d/a {:href (rfe/href ::frontpage)} "Frontpage"))
(d/li (d/a {:href (rfe/href ::about)} "About (public)"))
(d/li (d/a {:href (rfe/href ::item-list)} "Item list"))
(when user
(d/div
(d/li (d/a {:on-click (fn [e]
(.preventDefault e)
(refx/dispatch [::logout]))
:href "#"}
(str "Logout (" username ")"))))))
;; If user is authenticated
;; or if this route has been defined as public, else login view
(if (or user (:public? route-data))
(when-let [view (:view route-data)]
($ view {:match match}))
($ login-view))
(d/pre (with-out-str (pprint @db/app-db))))))
;;; Routes ;;;
(defn log-fn [& params]
(fn [_]
(apply js/console.log params)))
(def routes
(rf/router
["/"
[""
{:name ::frontpage
:view home-page
:controllers [{:start (log-fn "start" "frontpage controller")
:stop (log-fn "stop" "frontpage controller")}]}]
["about"
{:name ::about
:view about-page
:public? true}]
["items"
;; Shared data for sub-routes
{:view item-page
:controllers [{:start (log-fn "start" "items controller")
:stop (log-fn "stop" "items controller")}]}
[""
{:name ::item-list
:controllers [{:start (log-fn "start" "item-list controller")
:stop (log-fn "stop" "item-list controller")}]}]
["/:id"
{:name ::item
:parameters {:path {:id s/Int}
:query {(s/optional-key :foo) s/Keyword}}
:controllers [{:identity (fn [match]
(:path (:parameters match)))
:start (fn [parameters]
(js/console.log "start" "item controller" (:id parameters)))
:stop (fn [parameters]
(js/console.log "stop" "item controller" (:id parameters)))}]}]]]
{:data {:controllers [{:start (log-fn "start" "root-controller")
:stop (log-fn "stop" "root controller")}]
:coercion rsc/coercion
:public? false}}))
(defn on-navigate [new-match]
(when new-match
(refx/dispatch [::navigated new-match])))
(defn init-routes! []
(js/console.log "initializing routes")
(rfe/start!
routes
on-navigate
{:use-fragment true}))
;;; Setup ;;;
(def debug? ^boolean goog.DEBUG)
(defn dev-setup []
(when debug?
(enable-console-print!)
(println "dev mode")))
(defn ^:export init []
(refx/clear-subscription-cache!)
(refx/dispatch-sync [::initialize-db])
(dev-setup)
(init-routes!)
(doto (rdom/createRoot (js/document.getElementById "app"))
(.render ($ main-view))))