/
event.cljs
166 lines (145 loc) · 6.29 KB
/
event.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
(ns flybot.client.web.core.db.event
(:require [clojure.string :as str]
[day8.re-frame.http-fx]
[flybot.client.common.db.event :refer [http-xhrio-default]]
[flybot.client.common.utils :as client.utils]
[flybot.client.web.core.utils :as web.utils]
[flybot.common.utils :as utils :refer [toggle]]
[re-frame.core :as rf]
[reitit.frontend.easy :as rfe]
[sg.flybot.pullable :as pull]))
;; ---------- http success/failure ----------
(rf/reg-event-fx
:fx.http/send-post-success
(fn [_ [_ {:keys [posts]}]]
(let [{:post/keys [id last-edit-date] :as post} (:new-post posts)
post-title (client.utils/post->title post)]
{:fx [[:dispatch [:evt.post/add-post post]]
[:dispatch [:evt.form/clear :form/fields]]
[:dispatch [:evt.post/set-modes :read]]
[:fx.log/message ["Post " id " sent."]]
[:dispatch [:evt.notif/set-notif
:success
(if last-edit-date "Post edited" "New post created")
post-title]]]})))
;; ---------------- Routing -----------------
;; Redirecting incorrect/old post URLs
(rf/reg-event-fx
:evt.nav/redirect-post-url
(fn [{:keys [db]} [_]]
(let [[name redirect-name page-name id-ending url-identifier]
((pull/qfn {:app/current-view
{:data
{:name '?name
:redirect-name '?redirect-name
:page-name '?page-name}
:path-params
{:id-ending '?id-ending
:url-identifier '?url-identifier}}}
[?name
?redirect-name
?page-name
?id-ending
?url-identifier])
db)
matches-page? (fn [post] (-> post :post/page (= page-name)))
matches-id-ending? (fn [id] (str/ends-with? (str id) id-ending))
matches-url-identifier? (fn [post]
(= url-identifier
(web.utils/post->url-identifier post)))
queried-post (->>
(:app/posts db)
(into {}
(filter (fn [[id post]]
(and (matches-id-ending? id)
(matches-page? post)))))
vals
first)]
(when (and queried-post (not (matches-url-identifier? queried-post)))
;; Redirect on partial match (when everything matches except post title)
{:fx.router/replace-state
[(or name redirect-name)
{:id-ending id-ending
:url-identifier (web.utils/post->url-identifier queried-post)}]}))))
;; ---------- App ----------
;; Initialization
(rf/reg-event-fx
:evt.app/initialize
[(rf/inject-cofx :cofx.app/local-store-theme :theme)]
(fn [{:keys [db local-store-theme]} _]
(let [app-theme (or local-store-theme :dark)
current-view (or (:app/current-view db) (rfe/push-state :flybot/home))]
{:db (assoc
db
:app/current-view current-view
:app/theme app-theme
:user/mode :reader
:nav/navbar-open? false)
:http-xhrio (merge http-xhrio-default
{:params {:posts
{(list :all :with [])
[{:post/id '?
:post/page '?
:post/css-class '?
:post/creation-date '?
:post/last-edit-date '?
:post/author {:user/id '?
:user/name '?}
:post/last-editor {:user/id '?
:user/name '?}
:post/md-content '?
:post/image-beside {:image/src '?
:image/src-dark '?
:image/alt '?}
:post/default-order '?}]}
:users
{:auth
{(list :logged :with [])
{:user/id '?
:user/email '?
:user/name '?
:user/picture '?
:user/roles [{:role/name '?
:role/date-granted '?}]}}}}
:on-success [:fx.http/all-success]})
:fx [[:fx.app/update-html-class app-theme]]})))
;; Theme (dark/light)
(rf/reg-event-fx
:evt.app/toggle-theme
(fn [{:keys [db]} [_]]
(let [cur-theme (:app/theme db)
next-theme (toggle cur-theme [:light :dark])]
{:db (assoc db :app/theme next-theme)
:fx [[:fx.app/set-theme-local-store next-theme]
[:fx.app/toggle-css-class [cur-theme next-theme]]]})))
;; Code syntax highlighting
(rf/reg-event-fx
:evt.app/highlight-code
(fn [_ [_ html-id]]
{:fx [[:fx.app/highlight-code html-id]]}))
;; ---------- Navbar ----------
(rf/reg-event-db
:evt.nav/toggle-navbar
(fn [db [_]]
(update db :nav/navbar-open? not)))
(rf/reg-event-db
:evt.nav/close-navbar
(fn [db [_]]
(assoc db :nav/navbar-open? false)))
;; ---------- View ----------
(rf/reg-event-fx
:evt.page/set-title
(fn [_ [_ page-title]]
{:fx [[:fx.app/set-html-title page-title]]}))
(rf/reg-event-fx
:evt.page/set-current-view
(fn [{:keys [db]} [_ new-match]]
{:db (assoc db :app/current-view new-match)
:fx [[:fx.app/scroll-to (:fragment new-match)]]}))
;; ------ Notification ------
;; Pop-ups (toasts)
(rf/reg-event-fx
:evt.app/toast-notify
(fn [{:keys [db]} [_ notification]]
(let [theme (-> db :app/theme name)]
{:fx [[:fx.app/toast-notify [notification {"theme" theme}]]]})))