[front/feat] basic history feature
This commit is contained in:
parent
27dadca343
commit
29babcb300
2 changed files with 51 additions and 10 deletions
15
src/main/frontend/db.cljs
Normal file
15
src/main/frontend/db.cljs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
(ns frontend.db
|
||||||
|
(:require [cljs.reader]
|
||||||
|
[re-frame.core :refer [reg-cofx]]))
|
||||||
|
|
||||||
|
(defn set-ls-history
|
||||||
|
[user]
|
||||||
|
(.setItem js/localStorage "history" (str user)))
|
||||||
|
|
||||||
|
(reg-cofx
|
||||||
|
:local-store-history
|
||||||
|
(fn [cofx _]
|
||||||
|
(assoc cofx :local-store-history
|
||||||
|
(into (list)
|
||||||
|
(some->> (.getItem js/localStorage "history")
|
||||||
|
(cljs.reader/read-string))))))
|
|
@ -1,7 +1,8 @@
|
||||||
(ns frontend.events
|
(ns frontend.events
|
||||||
(:require [re-frame.core :refer [reg-event-db reg-event-fx]]
|
(:require [re-frame.core :refer [reg-event-db reg-event-fx inject-cofx path after trim-v]]
|
||||||
[superstructor.re-frame.fetch-fx]
|
[superstructor.re-frame.fetch-fx]
|
||||||
[clojure.string :as str]))
|
[clojure.string :as str]
|
||||||
|
[frontend.db :refer [set-ls-history]]))
|
||||||
|
|
||||||
|
|
||||||
(if js/goog.DEBUG
|
(if js/goog.DEBUG
|
||||||
|
@ -13,6 +14,10 @@
|
||||||
[& params]
|
[& params]
|
||||||
(str/join "/" (cons api-url params)))
|
(str/join "/" (cons api-url params)))
|
||||||
|
|
||||||
|
(def set-history-interceptor [(path :history)
|
||||||
|
(after set-ls-history)
|
||||||
|
trim-v])
|
||||||
|
|
||||||
(reg-event-fx
|
(reg-event-fx
|
||||||
:set-active-page
|
:set-active-page
|
||||||
(fn [{:keys [db]} [_ {:keys [new-match id query]}]]
|
(fn [{:keys [db]} [_ {:keys [new-match id query]}]]
|
||||||
|
@ -82,12 +87,32 @@
|
||||||
:db (-> db
|
:db (-> db
|
||||||
(assoc-in [:loading :question] true))}))
|
(assoc-in [:loading :question] true))}))
|
||||||
|
|
||||||
(reg-event-db
|
(reg-event-fx
|
||||||
|
:set-history
|
||||||
|
set-history-interceptor
|
||||||
|
(fn [{:keys [db]} history]
|
||||||
|
(let [history-item (first history)
|
||||||
|
update? (fn [x]
|
||||||
|
(and (= (:id x) (:id history-item))
|
||||||
|
(= (:type x) (:type history-item))))]
|
||||||
|
{:db (as-> db $
|
||||||
|
(remove update? $)
|
||||||
|
(conj $ history-item))})))
|
||||||
|
|
||||||
|
(reg-event-fx
|
||||||
:get-question-success
|
:get-question-success
|
||||||
(fn [db [_ {body :body}]]
|
(fn [{:keys [db]} [_ {body :body}]]
|
||||||
(-> db
|
(let [id (get-in db [:current-route :path-params :id])
|
||||||
(assoc-in [:loading :question] false)
|
type (get-in db [:current-route :data :name])
|
||||||
(assoc-in [:post] body))))
|
title (get-in body [:question :title])
|
||||||
|
detil (get-in body [:question :detail])]
|
||||||
|
{:db (-> db
|
||||||
|
(assoc-in [:loading :question] false)
|
||||||
|
(assoc-in [:post] body))
|
||||||
|
:dispatch [:set-history {:id id
|
||||||
|
:type type
|
||||||
|
:title title
|
||||||
|
:detil detil}]})))
|
||||||
|
|
||||||
(reg-event-db
|
(reg-event-db
|
||||||
:get-question-failure
|
:get-question-failure
|
||||||
|
@ -122,7 +147,8 @@
|
||||||
(update-in db-path into (:answers body))
|
(update-in db-path into (:answers body))
|
||||||
(assoc-in [:post :paging] (:paging body))))))
|
(assoc-in [:post :paging] (:paging body))))))
|
||||||
|
|
||||||
(reg-event-db
|
(reg-event-fx
|
||||||
:initialize-db
|
:initialize-db
|
||||||
(fn [_ _]
|
[(inject-cofx :local-store-history)]
|
||||||
{:current-route nil}))
|
(fn [{:keys [local-store-history]} _]
|
||||||
|
{:db (assoc {} :history local-store-history)}))
|
||||||
|
|
Loading…
Reference in a new issue