diff --git a/frontend/uxbox/core.cljs b/frontend/uxbox/core.cljs index 5a4e905cb..20f87198f 100644 --- a/frontend/uxbox/core.cljs +++ b/frontend/uxbox/core.cljs @@ -1,29 +1,20 @@ (ns uxbox.core - (:require [uxbox.state] - [uxbox.ui :as ui] + (:require [uxbox.ui :as ui] [uxbox.router] + [uxbox.state :as s] [uxbox.rstore :as rs] [uxbox.data.projects :as dp] - [goog.dom :as dom])) + [uxbox.data.load :as dl] + [goog.dom :as dom] + [beicon.core :as rx])) + (enable-console-print!) (let [dom (dom/getElement "app")] (ui/mount! dom)) -(defonce +setup-stuff+ +(defonce +setup+ (do - (rs/emit! (dp/create-project {:name "foo" - :width 600 - :height 600 - :layout :mobile})) - (rs/emit! (dp/create-project {:name "bar" - :width 600 - :height 600 - :layout :mobile})) - (rs/emit! (dp/create-project {:name "baz" - :width 600 - :height 600 - :layout :mobile})) - nil)) - + (rs/emit! (dl/load-data)) + (rx/on-value s/stream #(dl/persist-state %)))) diff --git a/frontend/uxbox/data/load.cljs b/frontend/uxbox/data/load.cljs new file mode 100644 index 000000000..1eb986302 --- /dev/null +++ b/frontend/uxbox/data/load.cljs @@ -0,0 +1,27 @@ +(ns uxbox.data.load + (:require [hodgepodge.core :refer [local-storage]] + [uxbox.rstore :as rs] + [uxbox.router :as r] + [uxbox.state :as st] + [uxbox.schema :as sc] + [uxbox.data.projects :as dp] + [bouncer.validators :as v])) + +(defn load-data + "Load data from local storage." + [] + (reify + rs/UpdateEvent + (-apply-update [_ state] + (if-let [data (get local-storage :data nil)] + (as-> state $ + (reduce dp/assoc-project $ (:projects data)) + (reduce dp/assoc-page $ (:pages data))) + state)))) + +(defn persist-state + [state] + (let [pages (into #{} (vals (:pages-by-id state))) + projects (into #{} (vals (:projects-by-id state)))] + (assoc! local-storage :data {:pages pages + :projects projects}))) diff --git a/frontend/uxbox/data/projects.cljs b/frontend/uxbox/data/projects.cljs index fd08a0996..bdaeb720b 100644 --- a/frontend/uxbox/data/projects.cljs +++ b/frontend/uxbox/data/projects.cljs @@ -21,6 +21,33 @@ :height [v/required v/integer] :project [v/required sc/uuid]}) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Helpers +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(defn assoc-project + "A reduce function for assoc the project + to the state map." + [state proj] + (let [uuid (:id proj)] + (update-in state [:projects-by-id] assoc uuid proj))) + +(defn assoc-page + "A reduce function for assoc the page + to the state map." + [state page] + (let [uuid (:id page)] + (update-in state [:pages-by-id] assoc uuid page))) + +;; (defn project-pages +;; "Get a ordered list of pages that +;; belongs to a specified project." +;; [state projectid] +;; (let [xf (comp +;; (filter #(= projectid (:project %))) +;; (map #(get-in state [:pages-by-id %])))] +;; (into [] xf (:pages state)))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Events ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -31,16 +58,12 @@ (reify rs/UpdateEvent (-apply-update [_ state] - (let [uuid (random-uuid) - page {:id uuid + (let [page {:id (random-uuid) :project project :name name :width width :height height}] - (-> state - (update-in [:pages] conj uuid) - (update-in [:projects-by-id project :pages] conj uuid) - (update-in [:pages-by-id] assoc uuid page)))) + (assoc-page state page))) IPrintWithWriter (-pr-writer [mv writer _] @@ -57,10 +80,8 @@ :name name :width width :height height - :pages []}] - (-> state - (update-in [:projects] conj uuid) - (update-in [:projects-by-id] assoc uuid proj)))) + :layout layout}] + (assoc-project state proj))) rs/EffectEvent (-apply-effect [_ state] @@ -82,7 +103,7 @@ IPrintWithWriter (-pr-writer [mv writer _] - (-write writer "#")))) + (-write writer "#")))) (defn go-to-project "A shortcut event that redirects the user to the diff --git a/frontend/uxbox/router.cljs b/frontend/uxbox/router.cljs index 133fb3421..290dcca76 100644 --- a/frontend/uxbox/router.cljs +++ b/frontend/uxbox/router.cljs @@ -43,7 +43,6 @@ (let [loc (merge {:handler name} (when params {:route-params params}))] - (println "navigate" loc) (bidi.router/set-location! +router+ loc)))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -70,7 +69,7 @@ (defonce +router+ (bidi.router/start-router! routes {:on-navigate on-navigate - :default-location {:handler :auth/login}})) + :default-location {:handler :main/dashboard}})) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Public Api diff --git a/frontend/uxbox/state.cljs b/frontend/uxbox/state.cljs index 25cb0cb3a..81e6f1e5c 100644 --- a/frontend/uxbox/state.cljs +++ b/frontend/uxbox/state.cljs @@ -10,13 +10,7 @@ (rs/init {:user {:fullname "Cirilla" :avatar "http://lorempixel.com/50/50/"} :workspace nil - :projects [] - :pages [] :projects-by-id {} :pages-by-id {}})) -(defonce +setup-stuff+ - (do - (rx/to-atom stream state) - (rx/on-value stream #(println "state:" %)))) - +(rx/to-atom stream state) diff --git a/frontend/uxbox/ui.cljs b/frontend/uxbox/ui.cljs index ca577187c..c3eb007a3 100644 --- a/frontend/uxbox/ui.cljs +++ b/frontend/uxbox/ui.cljs @@ -18,7 +18,6 @@ (defn app-render [own] (let [{:keys [location location-params] :as state} (rum/react state)] - (println 1111 location location-params) (html [:section (ui.lb/lightbox) diff --git a/frontend/uxbox/ui/dashboard.cljs b/frontend/uxbox/ui/dashboard.cljs index 4e3dc9579..651d7cecc 100644 --- a/frontend/uxbox/ui/dashboard.cljs +++ b/frontend/uxbox/ui/dashboard.cljs @@ -197,7 +197,6 @@ (defn project-render [own project] - (println "project-render" project) (let [on-click #(rs/emit! (dp/go-to-project (:id project)))] (html [:div.grid-item.project-th {:on-click on-click @@ -237,7 +236,7 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (def ^:static grid-state - (as-> (l/select-keys [:projects :projects-by-id]) $ + (as-> (l/select-keys [:projects-by-id]) $ (l/focus-atom $ s/state))) (defn grid-render @@ -253,9 +252,8 @@ [:div.dashboard-grid-content [:div.grid-item.add-project {:on-click on-click} [:span "+ New project"]] - (for [pid (:projects state)] - (let [item (get-in state [:projects-by-id pid])] - (rum/with-key (project-item item) (str pid))))]]])))) + (for [item (vals (:projects-by-id state))] + (rum/with-key (project-item item) (:id item)))]]])))) (def grid (util/component diff --git a/frontend/uxbox/ui/lightbox.cljs b/frontend/uxbox/ui/lightbox.cljs index 538df5007..87de7a526 100644 --- a/frontend/uxbox/ui/lightbox.cljs +++ b/frontend/uxbox/ui/lightbox.cljs @@ -14,7 +14,6 @@ (defn set! [kind] - (println "lightbox$set!" kind) (reset! +current+ kind)) (defn close! diff --git a/frontend/uxbox/ui/workspace.cljs b/frontend/uxbox/ui/workspace.cljs index f9dc63840..dcb87ab4c 100644 --- a/frontend/uxbox/ui/workspace.cljs +++ b/frontend/uxbox/ui/workspace.cljs @@ -89,8 +89,6 @@ (defn workspace-render [own projectid] - (println "workspace-render" @s/state) - (println 22222 @page-state) (html [:div "hello world" ;; (header conn page ws/grid? project-bar-visible?)