diff --git a/src/uxbox/main/data/projects.cljs b/src/uxbox/main/data/projects.cljs index 6d8de93e5..42b46a996 100644 --- a/src/uxbox/main/data/projects.cljs +++ b/src/uxbox/main/data/projects.cljs @@ -8,28 +8,44 @@ (ns uxbox.main.data.projects (:require [cuerdas.core :as str] [beicon.core :as rx] - [uxbox.util.rstore :as rs] - [uxbox.util.router :as r] [uxbox.main.state :as st] [uxbox.main.repo :as rp] + [uxbox.main.data.pages :as udp] + [uxbox.util.rstore :as rs] + [uxbox.util.router :as r] [uxbox.util.i18n :refer (tr)] [uxbox.util.schema :as sc] [uxbox.main.data.pages :as udp])) ;; --- Helpers +(defn assoc-project-page + "Assoc to the state the project's embedded page." + [state project] + (let [page {:id (:page-id project) + :name (:page-name project) + :version (:page-version project) + :data (:page-data project) + :created-at (:page-created-at project) + :modified-at (:page-modified-at project) + :metadata (:page-metadata project)}] + (-> state + (udp/assoc-page page) + (udp/assoc-packed-page page)))) + (defn assoc-project - "A reduce function for assoc the project - to the state map." - [state proj] - (let [id (:id proj)] - (update-in state [:projects id] merge proj))) + "A reduce function for assoc the project to the state map." + [state {:keys [id] :as project}] + (let [project (dissoc project + :page-name :page-version + :page-data :page-metadata + :page-created-at :page-modified-at)] + (update-in state [:projects id] merge project))) (defn dissoc-project - "A reduce function for dissoc the project - from the state map." + "A reduce function for dissoc the project from the state map." [state id] - (update-in state [:projects] dissoc id)) + (update state :projects dissoc id)) ;; --- Initialize @@ -54,7 +70,9 @@ (defrecord ProjectsFetched [projects] rs/UpdateEvent (-apply-update [_ state] - (reduce assoc-project state projects))) + (as-> state $ + (reduce assoc-project-page $ projects) + (reduce assoc-project $ projects)))) (defn projects-fetched [projects] diff --git a/src/uxbox/main/repo/projects.cljs b/src/uxbox/main/repo/projects.cljs index 34ebe9bb6..a9782853d 100644 --- a/src/uxbox/main/repo/projects.cljs +++ b/src/uxbox/main/repo/projects.cljs @@ -9,26 +9,32 @@ (:require [beicon.core :as rx] [uxbox.config :refer (url)] [uxbox.main.repo.pages :as pages] - [uxbox.main.repo.impl :refer (request send!)])) + [uxbox.main.repo.impl :refer (request send!)] + [uxbox.util.transit :as t])) (defmethod request :fetch/projects [type data] - (let [url (str url "/projects")] - (send! {:url url :method :get}))) - -(defmethod request :fetch/project - [_ id] - (let [url (str url "/projects/" id)] - (send! {:url url :method :get}))) + (letfn [(decode-payload [{:keys [payload] :as response}] + (assoc response :payload (mapv decode-page payload))) + (decode-page [{:keys [page-metadata page-data] :as project}] + (assoc project + :page-metadata (t/decode page-metadata) + :page-data (t/decode page-data)))] + ;; Obtain the list of projects and decode the embedded + ;; page data in order to have it usable. + (->> (send! {:url (str url "/projects") + :method :get}) + (rx/map decode-payload)))) (defmethod request :fetch/project-by-token [_ token] - (let [url (str url "/projects-by-token/" token)] - (->> (send! {:url url :method :get}) - (rx/map (fn [response] - (update-in response [:payload :pages] - (fn [pages] - (mapv pages/decode-page pages)))))))) + (letfn [(decode-pages [response] + (let [pages (->> (get-in response [:payload :pages]) + (mapv pages/decode-page))] + (assoc-in response [:payload :pages] pages)))] + (->> (send! {:url (str url "/projects-by-token/" token) + :method :get}) + (rx/map decode-pages)))) (defmethod request :create/project [_ data]