Commit 99d061a0 authored by Alex Sarmanov's avatar Alex Sarmanov

routes, ajax, fixes

parent 40ff54bf
......@@ -7,3 +7,4 @@ figwheel_server.log
pom.xml
pom.xml.asc
.sass-cache
.idea
......@@ -11,6 +11,7 @@
[secretary "1.2.3"]
[kibu/pushy "0.3.6"]
[alandipert/storage-atom "2.0.1"] ;; persistent atom storage HTML 5
[cljs-ajax "0.5.8"]
]
:eval-in-leiningen true
:plugins [[lein-cljsbuild "1.1.4"]
......
#!/bin/sh
# then in main repl
# (use 'figwheel-sidecar.repl-api)
# (figwheel-sidecar.repl-api/start-figwheel!)
# (cljs-repl)
# shift+cmd+p - eval expr in cursive
# lein repl
(ns bcore.ajax
(:require [ajax.core :as ajax]))
(defn default-headers [request]
(-> request
(update :uri #(str js/context %))
(update
:headers
#(merge
%
{"Accept" "application/transit+json"
"x-csrf-token" js/csrfToken}))))
(defn load-interceptors! []
(swap! ajax/default-interceptors
conj
(ajax/to-interceptor {:name "default headers"
:request default-headers})))
......@@ -7,7 +7,8 @@
[redux.core :as r]
[utils.sys :as sys]
[cognitect.transit :as t]
[clojure.data :as cldata]))
[clojure.data :as cldata]
[components.state-history :as state-history]))
(defn dev-setup []
(when config/debug?
......
......@@ -6,13 +6,19 @@
[goog.history.EventType :as EventType]
[redux.core :as r]
[rum.core :as rum]
[containers.not-found-page :as not-found-page]
[containers.index-page :as index-page]
[containers.about-page :as about-page]))
(defmulti page-container identity)
(defmethod page-container :index [] index-page/index-page)
(defmethod page-container :about [] about-page/about-page)
(defmethod page-container :default [] [:div "route not found"])
(defmethod page-container :not-found [] not-found-page/not-found-page)
(defmulti page-path identity)
(defmethod page-path :index [] "")
(defmethod page-path :about [] "/about")
(defmethod page-path :not-found [] "/not-found")
(defn mount [container & [node]]
(let [point-node (or node "#app")]
......@@ -32,28 +38,33 @@
(add-watch r/!state
:url
(fn [k r old-state new-state]
(def new-uri (:uri (:url new-state)))
(pushy/set-token! history (str "/#" new-uri))
(mount (page-container (:page (:url new-state))))))
;; Start event listeners
(pushy/start! history)
;; Maybe i can make a universal macro route later
(let [new-params (js->clj (:params (:url new-state)))
new-page-path (:page (:url new-state))]
(pushy/set-token! history
(str "/#"
(page-path new-page-path)
(when-not (= new-params nil)
(str "/?"
(secretary/encode-query-params new-params)))))
(mount (page-container (:page (:url new-state)))))))
(defn app-routes []
(secretary/set-config! :prefix "#")
(defroute about-page-route "/about*" []
(mount about-page/about-page))
;; define routes here
(defroute index-page-route "/*" []
(mount index-page/index-page))
;; index
(defroute index-page-route "/" {:keys [query-params]}
[]
(r/dispatch! {:type :mount-page :page :index :params query-params}))
;; about
(defroute about-page-route "/about*" {:keys [query-params]}
[]
(r/dispatch! {:type :mount-page :page :about :params query-params}))
;; Catch all
;; (defroute "*" []
;; (set-html! application "<h1>LOL! YOU LOST!</h1>"))
(defroute "*"
[]
(r/dispatch! {:type :mount-page :page :not-found :params {}}))
(hook-browser-navigation!))
......@@ -8,7 +8,7 @@
"Root rum/react component"
(let [state (rum/react r/!state)]
[:div
[:button {:on-click #(r/dispatch! {:type :mount-page :page :index :uri "/?zzz=aaa"})} "To home page"]
[:button {:on-click #(r/dispatch! {:type :mount-page :page :index :params {:zzz "zzzzzzzzz"}})} "To home page"]
"About bmain"
[:div (state-history/state-history)]
[:pre.state "current state: " (str @r/!state)]]))
......@@ -4,8 +4,7 @@
[redux.core :as r]
[utils.sys :as sys]
[clojure.data :as cldata]
[components.state-history :as state-history]
[secretary.core :as secretary]))
[components.state-history :as state-history]))
(rum/defc index-page < rum/reactive []
"Root rum/react component"
......@@ -13,14 +12,14 @@
;; (println sys/get-query-params)
[:div "[" (:count state) "] " (pr-str (:message state))]
[:div
[:button {:on-click #(r/dispatch! {:type :mount-page :page :about :uri "/about"})} "To About page"]
[:div (state-history/state-history)]
[:pre.state "current state: " (str @r/!state)]
[:button {:on-click #(r/dispatch! {:type :mount-page :page :about :params {:aaa "xxx"}})} "To About page"]
[:div (state-history/state-history)]
[:pre.state "current state: " (str @r/!state)]
;; [:pre "actions: " (str @r/!actions-history)]
;; [:pre "state-history: " (str @r/!state-history)]
[:button {:on-click #(r/dispatch! {:type :some-greeting :name "YourName"})} "Hello"]
[:button {:on-click #(r/dispatch! {:type :math-increment :num 1})} "+1"]
[:button {:on-click #(r/dispatch! {:type :math-increment :num 10})} "+10"]
[:button {:on-click #(r/dispatch! {:type :math-decrement :num 1})} "-1"]
[:button {:on-click #(r/dispatch! {:type :math-decrement :num 10})} "-10"]
[:button {:on-click #(r/dispatch! {:type :HelloWorld})} "Error"]]))
[:button {:on-click #(r/dispatch! {:type :some-greeting :name "YourName"})} "Hello"]
[:button {:on-click #(r/dispatch! {:type :math-increment :num 1})} "+1"]
[:button {:on-click #(r/dispatch! {:type :math-increment :num 10})} "+10"]
[:button {:on-click #(r/dispatch! {:type :math-decrement :num 1})} "-1"]
[:button {:on-click #(r/dispatch! {:type :math-decrement :num 10})} "-10"]
[:button {:on-click #(r/dispatch! {:type :HelloWorld})} "Error"]]))
(ns containers.not-found-page
(:require
[rum.core :as rum]
[redux.core :as r]))
(rum/defc not-found-page < rum/static []
"Root rum/react component"
[:div
"404 =("])
......@@ -2,7 +2,11 @@
(:require [redux.reducer :refer [Action]]))
(defmethod Action :math-increment [{:keys [num]} state]
(assoc-in state [:math :counter] (+ (get-in state [:math :counter]) num)))
(-> state
(assoc-in [:math :counter] (+ (get-in state [:math :counter]) num))
(assoc-in [:timestamp] (.getTime (js/Date.)))))
(defmethod Action :math-decrement [{:keys [num]} state]
(assoc-in state [:math :counter] (- (get-in state [:math :counter]) num)))
(-> state
(assoc-in [:math :counter] (- (get-in state [:math :counter]) num))
(assoc-in [:timestamp] (.getTime (js/Date.)))))
(ns reducers.socket
(:require [redux.reducer :refer [Action]]))
;; (defmethod Action :socket-on-message [answer]
;; (assoc-in state [:socket :message] (:message answer)))
;; (defmethod Action :socket-on-message [answer state]
;; (-> state
;; (assoc-in [:socket :message] (:message answer))
;; (assoc-in [:timestamp] (.getTime (js/Date.)))))
......@@ -2,4 +2,6 @@
(:require [redux.reducer :refer [Action]]))
(defmethod Action :some-greeting [{:keys [name]} state]
(assoc-in state [:text :some-greeting] (gensym name)))
(-> state
(assoc-in [:text :some-greeting] (gensym name))
(assoc-in [:timestamp] (.getTime (js/Date.)))))
(ns reducers.url
(:require [redux.reducer :refer [Action]]
[rum.core :as rum]
))
(:require [redux.reducer :refer [Action]]))
(defmethod Action :mount-page [{:keys [page uri]} state]
(let [url-state {:page page :uri uri}]
(assoc-in state [:url] url-state)))
(defmethod Action :mount-page [{:keys [page params]} state]
(-> state
(assoc-in [:url] {:page page :params params})
(assoc-in [:timestamp] (.getTime (js/Date.)))))
(ns redux.core
(:require [rum.core :as r]
(:require [rum.core :as rum]
[redux.reducer :refer [Action]]
[bmain.config :as config]
[alandipert.storage-atom :refer [local-storage]] ;; HTML 5 atom storage
......@@ -10,7 +10,9 @@
(def dev-tools js/devToolsExtension)
(def initial-state {:math {:counter 0} :url {:page :index :uri "/"}})
(def initial-state {:math {:counter 0} :url {:page :index :params {}}})
;;(def initial-state {:math {:counter 0} :url {}})
(defonce !actions-history (atom []))
(defonce !state-history (atom []))
......@@ -23,10 +25,13 @@
(do
(if (.getItem js/localStorage ":!state")
(defonce !state (atom initial-state))
(def !state (local-storage (atom {}) :!state))))
(if (not= "" (.-hash (.-location js/window)))
(set! (.-location js/window) "/")
(defonce !state (atom initial-state))))
(defonce !state (local-storage (atom {}) :!state))))
(defonce !state (atom initial-state)))
;; (if (not= "" (.-hash (.-location js/window)))
;; ;;(set! (.-location js/window) "/")
;; (defonce !state (atom initial-state)))
;; )
(add-watch !actions-history :reduce
(fn [_ _ _ new-state]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment