Add logout buttons
This commit is contained in:
parent
6b62af8727
commit
934c499e93
2 changed files with 61 additions and 19 deletions
|
|
@ -6,6 +6,17 @@
|
||||||
[computersandblues.lodestone.database :as db]
|
[computersandblues.lodestone.database :as db]
|
||||||
[computersandblues.lodestone.match :refer [query->matching-fn]]))
|
[computersandblues.lodestone.match :refer [query->matching-fn]]))
|
||||||
|
|
||||||
|
(def posts-init-state
|
||||||
|
{:query nil
|
||||||
|
; TODO: pagination
|
||||||
|
; :page 0
|
||||||
|
; :max-displayed-id nil
|
||||||
|
; :order [:date :desc]
|
||||||
|
:per-page 50
|
||||||
|
:loading #{}
|
||||||
|
:total 0
|
||||||
|
:displayed-posts []})
|
||||||
|
|
||||||
(defonce state (r/atom {:root nil
|
(defonce state (r/atom {:root nil
|
||||||
|
|
||||||
:section :login
|
:section :login
|
||||||
|
|
@ -13,15 +24,7 @@
|
||||||
:section/login {}
|
:section/login {}
|
||||||
|
|
||||||
; TODO: Handle other lists
|
; TODO: Handle other lists
|
||||||
:section/posts {:query nil
|
:section/posts posts-init-state}))
|
||||||
:per-page 50
|
|
||||||
:loading #{}
|
|
||||||
; TODO: pagination
|
|
||||||
; :page 0
|
|
||||||
; :max-displayed-id nil
|
|
||||||
; :order [:date :desc]
|
|
||||||
:total 0
|
|
||||||
:displayed-posts []}}))
|
|
||||||
|
|
||||||
; TODO Ensure that cached data is up to date
|
; TODO Ensure that cached data is up to date
|
||||||
; TODO Manually fetch older / newer favorites
|
; TODO Manually fetch older / newer favorites
|
||||||
|
|
@ -266,11 +269,11 @@
|
||||||
|
|
||||||
(defn search []
|
(defn search []
|
||||||
[:input {:placeholder "Start typing to search…"
|
[:input {:placeholder "Start typing to search…"
|
||||||
|
:type "search"
|
||||||
:on-change (fn [e]
|
:on-change (fn [e]
|
||||||
(let [query (.. e -target -value)]
|
(let [query (.. e -target -value)]
|
||||||
(swap! state assoc-in [:section/posts :query] (if (str/blank? query) nil query))
|
(swap! state assoc-in [:section/posts :query] (if (str/blank? query) nil query))
|
||||||
(refresh-displayed-posts! (:section/posts @state))))
|
(refresh-displayed-posts! (:section/posts @state))))}])
|
||||||
:value (-> @state :section/posts :query)}])
|
|
||||||
|
|
||||||
(defn- debug
|
(defn- debug
|
||||||
"Implements a lazy pretty-printer for whatever is passed in as `obj`. The
|
"Implements a lazy pretty-printer for whatever is passed in as `obj`. The
|
||||||
|
|
@ -288,7 +291,7 @@
|
||||||
[:pre @pprinted]])))
|
[:pre @pprinted]])))
|
||||||
|
|
||||||
(defn user [{:keys [user]}]
|
(defn user [{:keys [user]}]
|
||||||
(:username user))
|
[:em (:username user)])
|
||||||
|
|
||||||
(defn- list-accounts [accounts]
|
(defn- list-accounts [accounts]
|
||||||
[:<>
|
[:<>
|
||||||
|
|
@ -329,9 +332,11 @@
|
||||||
[:nav
|
[:nav
|
||||||
[:ul.controls
|
[:ul.controls
|
||||||
[:li.control-element.url
|
[:li.control-element.url
|
||||||
[:a {:href (:url post) :target "_blank"} "↗ Open original post"]]
|
[:a.control-button {:href (:url post) :target "_blank"} "↗ Open original post"]]
|
||||||
[:li.control-element.clipboard
|
[:li.control-element.clipboard
|
||||||
[:a {:href "#" :on-click (fn [e]
|
[:a.control-button
|
||||||
|
{:href "#"
|
||||||
|
:on-click (fn [e]
|
||||||
(.preventDefault e)
|
(.preventDefault e)
|
||||||
(js/navigator.clipboard.writeText (:url post)))} "◎ Copy URL to clipboard"]]]]
|
(js/navigator.clipboard.writeText (:url post)))} "◎ Copy URL to clipboard"]]]]
|
||||||
#_[debug post]])
|
#_[debug post]])
|
||||||
|
|
@ -365,6 +370,15 @@
|
||||||
(debounced-refresh! (:section/posts @state)))}]
|
(debounced-refresh! (:section/posts @state)))}]
|
||||||
(fetch-favorites! (merge defaults opts))))
|
(fetch-favorites! (merge defaults opts))))
|
||||||
|
|
||||||
|
(defn- disconnect-account! [e]
|
||||||
|
(.preventDefault e)
|
||||||
|
(when (js/confirm "Are you sure? This will log you out and clear your local cache.")
|
||||||
|
(. (js/Promise.all #js [(db/clear! ::db/posts)
|
||||||
|
(db/clear! ::db/application)])
|
||||||
|
(then (fn [_]
|
||||||
|
(swap! state #(-> (assoc % :section :login)
|
||||||
|
(assoc :section/posts posts-init-state))))))))
|
||||||
|
|
||||||
(defn posts-section [{:keys [posts]}]
|
(defn posts-section [{:keys [posts]}]
|
||||||
(let [{:keys [per-page query total displayed-posts loading]} posts]
|
(let [{:keys [per-page query total displayed-posts loading]} posts]
|
||||||
[:section.posts
|
[:section.posts
|
||||||
|
|
@ -374,11 +388,14 @@
|
||||||
(str "Loaded " total " posts"
|
(str "Loaded " total " posts"
|
||||||
(when (or query (> total per-page))
|
(when (or query (> total per-page))
|
||||||
(str ", displaying " (count displayed-posts) (when query " matches"))))]
|
(str ", displaying " (count displayed-posts) (when query " matches"))))]
|
||||||
[:div.search-form
|
[:section.search-form
|
||||||
[search]
|
[search]
|
||||||
(when (seq loading) " …")
|
(when (seq loading) " …")
|
||||||
#_(cond (= api-state :loading) " …"
|
#_(cond (= api-state :loading) " …"
|
||||||
(= api-state :error) " API Error!")]]
|
(= api-state :error) " API Error!")]
|
||||||
|
[:section.buttons
|
||||||
|
[:a.control-button {:href "#"
|
||||||
|
:on-click disconnect-account!} "⊘ Disconnect account"]]]
|
||||||
[:ul.results (map-indexed (fn [idx p]
|
[:ul.results (map-indexed (fn [idx p]
|
||||||
^{:key idx} [:li.result [post {:post p}]]) displayed-posts)]
|
^{:key idx} [:li.result [post {:post p}]]) displayed-posts)]
|
||||||
#_[:div.load-buttons
|
#_[:div.load-buttons
|
||||||
|
|
@ -396,13 +413,33 @@
|
||||||
(seq (:body response)))}))}
|
(seq (:body response)))}))}
|
||||||
"Load all"]]]))
|
"Load all"]]]))
|
||||||
|
|
||||||
|
;; help section
|
||||||
|
|
||||||
|
(defn help-section []
|
||||||
|
[:section.help
|
||||||
|
[:h2 "Help"]
|
||||||
|
[:h3 "Search Syntax"]
|
||||||
|
[:ul.search-syntax
|
||||||
|
[:li "Three are several places that we take into account when looking for query results"
|
||||||
|
[:ul
|
||||||
|
[:li "Post content"]
|
||||||
|
[:li "Creator of the post"]
|
||||||
|
[:li "Any mentioned account"]]]
|
||||||
|
[:li "Upper- and lowercase does not make a difference."]
|
||||||
|
[:li "All words are matched separately. They do not have to appear in the post in the same order, but they all have to appear."]
|
||||||
|
[:li "If you want to search verbatim for a phrase, \"quote it like this\""]
|
||||||
|
[:li "Lodestone tries to turn words into regular expressions. " [:code "fossi.*ergy"] " will match \"fossile energy\"." [:code "bo?ar"] " will match both boar and bar."]]])
|
||||||
|
|
||||||
|
;; the component tying it all toger
|
||||||
|
|
||||||
(defn app []
|
(defn app []
|
||||||
(let [section (:section @state)
|
(let [section (:section @state)
|
||||||
posts (:section/posts @state)]
|
posts (:section/posts @state)]
|
||||||
[:main#app
|
[:main#app
|
||||||
(case section
|
(case section
|
||||||
:login [login-section]
|
:login [login-section]
|
||||||
:posts [posts-section {:posts posts}])]))
|
:posts [posts-section {:posts posts}]
|
||||||
|
:help [help-section])]))
|
||||||
|
|
||||||
;; database
|
;; database
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,11 @@
|
||||||
request (.getAll store key-range)]
|
request (.getAll store key-range)]
|
||||||
(promisify request)))
|
(promisify request)))
|
||||||
|
|
||||||
|
(defn clear! [store-id]
|
||||||
|
(let [store (open-store @+db+ store-id "readwrite")
|
||||||
|
request (.clear store)]
|
||||||
|
(promisify request)))
|
||||||
|
|
||||||
(defn open-cursor!
|
(defn open-cursor!
|
||||||
([store-id key-range] (open-cursor! store-id key-range "next"))
|
([store-id key-range] (open-cursor! store-id key-range "next"))
|
||||||
([store-id key-range direction]
|
([store-id key-range direction]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue