diff --git a/src/uxbox/data/colors.cljs b/src/uxbox/data/colors.cljs index 60eff4007..ebf17c88a 100644 --- a/src/uxbox/data/colors.cljs +++ b/src/uxbox/data/colors.cljs @@ -9,10 +9,38 @@ (:require [clojure.set :as set] [beicon.core :as rx] [uuid.core :as uuid] + [uxbox.state :as st] [uxbox.rstore :as rs] [uxbox.state.colors :as stc] [uxbox.repo :as rp])) +;; --- Initialize + +(declare fetch-collections) +(declare collections-fetched?) + +(defrecord Initialize [] + rs/EffectEvent + (-apply-effect [_ state] + (when-not (seq (:colors-by-id state)) + (reset! st/loader true))) + + rs/WatchEvent + (-apply-watch [_ state s] + (let [colors (seq (:colors-by-id state))] + (if colors + (rx/empty) + (rx/merge + (rx/of (fetch-collections)) + (->> (rx/filter collections-fetched? s) + (rx/take 1) + (rx/do #(reset! st/loader false)) + (rx/ignore))))))) + +(defn initialize + [] + (Initialize.)) + ;; --- Collections Fetched (defrecord CollectionFetched [items] @@ -24,6 +52,10 @@ [items] (CollectionFetched. items)) +(defn collections-fetched? + [v] + (instance? CollectionFetched v)) + ;; --- Fetch Collections (defrecord FetchCollections [] diff --git a/src/uxbox/data/dashboard.cljs b/src/uxbox/data/dashboard.cljs index ec6f69ebf..c25ca4b5d 100644 --- a/src/uxbox/data/dashboard.cljs +++ b/src/uxbox/data/dashboard.cljs @@ -19,40 +19,13 @@ ;; --- Events -(defn- setup-dashboard-state - [state section] - (update state :dashboard assoc - :section section - :collection-type :builtin - :collection-id 1)) - (defrecord InitializeDashboard [section] rs/UpdateEvent (-apply-update [_ state] - (let [state (setup-dashboard-state state section)] - (if (seq (:projects-by-id state)) - state - (assoc state :loader true)))) - - rs/WatchEvent - (-apply-watch [_ state s] - (let [projects (seq (:projects-by-id state)) - color-collections (seq (:colors-by-id state))] - (rx/merge - ;; Load projects if needed - (if projects - (rx/empty) - (rx/of (dp/fetch-projects))) - - (rx/of (dc/fetch-collections)) - - (when (:loader state) - (if projects - (rx/of #(assoc % :loader false)) - (->> (rx/filter dp/projects-fetched? s) - (rx/take 1) - (rx/delay 1000) - (rx/map (fn [_] #(assoc % :loader false)))))))))) + (update state :dashboard assoc + :section section + :collection-type :builtin + :collection-id 1))) (defn initialize [section] diff --git a/src/uxbox/data/projects.cljs b/src/uxbox/data/projects.cljs index 95a194f56..3dc2bccd4 100644 --- a/src/uxbox/data/projects.cljs +++ b/src/uxbox/data/projects.cljs @@ -10,12 +10,40 @@ [beicon.core :as rx] [uxbox.rstore :as rs] [uxbox.router :as r] + [uxbox.state :as st] [uxbox.repo :as rp] [uxbox.locales :refer (tr)] [uxbox.schema :as sc] [uxbox.state.project :as stpr] [uxbox.data.pages :as udp])) +;; --- Initialize + +(declare fetch-projects) +(declare projects-fetched?) + +(defrecord Initialize [] + rs/EffectEvent + (-apply-effect [_ state] + (when-not (seq (:projects-by-id state)) + (reset! st/loader true))) + + rs/WatchEvent + (-apply-watch [_ state s] + (let [projects (seq (:projects-by-id state))] + (if projects + (rx/empty) + (rx/merge + (rx/of (fetch-projects)) + (->> (rx/filter projects-fetched? s) + (rx/take 1) + (rx/do #(reset! st/loader false)) + (rx/ignore))))))) + +(defn initialize + [] + (Initialize.)) + ;; --- Projects Fetched (defrecord ProjectsFetched [projects] @@ -102,20 +130,25 @@ ;; --- Go To & Go To Page (defrecord GoTo [projectid] + rs/EffectEvent + (-apply-effect [_ state] + (reset! st/loader true)) + rs/WatchEvent (-apply-watch [_ state s] (letfn [(navigate [pages] (let [pageid (:id (first pages)) params {:project-uuid projectid :page-uuid pageid}] - (rx/of (r/navigate :workspace/page params))))] + (r/navigate :workspace/page params)))] (rx/merge (rx/of #(assoc % :loader true) (udp/fetch-pages projectid)) (->> (rx/filter udp/pages-fetched? s) (rx/take 1) (rx/map :pages) - (rx/flat-map navigate)))))) + (rx/do #(reset! st/loader false)) + (rx/map navigate)))))) (defrecord GoToPage [projectid pageid] rs/WatchEvent diff --git a/src/uxbox/data/workspace.cljs b/src/uxbox/data/workspace.cljs index f24aa1bbb..394c212eb 100644 --- a/src/uxbox/data/workspace.cljs +++ b/src/uxbox/data/workspace.cljs @@ -10,9 +10,11 @@ [uuid.core :as uuid] [uxbox.constants :as c] [uxbox.rstore :as rs] + [uxbox.state :as st] [uxbox.state.shapes :as stsh] [uxbox.schema :as sc] [uxbox.data.core :refer (worker)] + [uxbox.data.projects :as dp] [uxbox.data.pages :as udp] [uxbox.data.shapes :as uds] [uxbox.data.forms :as udf] @@ -37,65 +39,59 @@ (declare initialize-alignment-index) -(defn- setup-workspace-state - [state project page] - (if (:workspace state) - (update state :workspace merge - {:project project - :page page - :selected #{} - :drawing nil}) - (assoc state :workspace - {:project project - :zoom 1 - :page page - :flags #{:layers :element-options} - :selected #{} - :drawing nil}))) - (defrecord InitializeWorkspace [project page] rs/UpdateEvent (-apply-update [_ state] - (let [state (setup-workspace-state state project page)] - (if (get-in state [:pages-by-id page]) - state - (assoc state :loader true)))) + (if (:workspace state) + (update state :workspace merge + {:project project + :page page + :selected #{} + :drawing nil}) + (assoc state :workspace + {:project project + :zoom 1 + :page page + :flags #{:layers :element-options} + :selected #{} + :drawing nil}))) - rs/WatchEvent - (-apply-watch [_ state s] - (let [page' (get-in state [:pages-by-id page])] - (rx/merge - ;; Alignment index initialization - (if page' - (rx/of (initialize-alignment-index page)) - (->> (rx/filter udp/pages-fetched? s) - (rx/take 1) - (rx/map #(initialize-alignment-index page)))) + rs/WatchEvent + (-apply-watch [_ state s] + (let [page-id page + page (get-in state [:pages-by-id page-id])] - ;; Disable loader if it is enabled - (when (:loader state) - (if page' - (->> (rx/of #(assoc % :loader false)) - (rx/delay 1000)) - (->> (rx/filter udp/pages-fetched? s) - (rx/take 1) - (rx/delay 2000) - (rx/map (fn [_] #(assoc % :loader false)))))) + ;; Activate loaded if page is not fetched. + (when-not page (reset! st/loader true)) - ;; Page fetching if does not fetched - (when-not page' - (rx/of (udp/fetch-pages project))) + (rx/merge + (if page + (rx/of (initialize-alignment-index page-id)) + (rx/merge + (rx/of (udp/fetch-pages project)) + (->> (rx/filter udp/pages-fetched? s) + (rx/take 1) + (rx/do #(reset! st/loader false)) + (rx/map #(initialize-alignment-index page-id))))) - ;; Initial history loading - (rx/of - (udh/fetch-page-history page) - (udh/fetch-pinned-page-history page)))))) + ;; Initial history loading + (rx/of + (udh/fetch-page-history page-id) + (udh/fetch-pinned-page-history page-id))))) + + rs/EffectEvent + (-apply-effect [_ state] + ;; Optimistic prefetch of projects if them are not already fetched + (when-not (seq (:projects-by-id state)) + (rs/emit! (dp/fetch-projects))))) (defn initialize "Initialize the workspace state." [project page] (InitializeWorkspace. project page)) +;; --- Toggle Flag + (defn toggle-flag "Toggle the enabled flag of the specified tool." [key] diff --git a/src/uxbox/ui/dashboard/colors.cljs b/src/uxbox/ui/dashboard/colors.cljs index a0280a688..5e6294e70 100644 --- a/src/uxbox/ui/dashboard/colors.cljs +++ b/src/uxbox/ui/dashboard/colors.cljs @@ -231,7 +231,8 @@ (defn colors-page-will-mount [own] - (rs/emit! (dd/initialize :dashboard/colors)) + (rs/emit! (dd/initialize :dashboard/colors) + (dc/initialize)) own) (defn colors-page-transfer-state diff --git a/src/uxbox/ui/dashboard/projects.cljs b/src/uxbox/ui/dashboard/projects.cljs index 23fff87f3..eefad4ad6 100644 --- a/src/uxbox/ui/dashboard/projects.cljs +++ b/src/uxbox/ui/dashboard/projects.cljs @@ -230,7 +230,8 @@ (defn projects-page-will-mount [own] - (rs/emit! (dd/initialize :dashboard/projects)) + (rs/emit! (dd/initialize :dashboard/projects) + (dp/initialize)) own) (defn projects-page-transfer-state