mirror of
https://github.com/penpot/penpot.git
synced 2025-02-10 17:18:21 -05:00
Properly unpack embedded page fields on fetching projects.
This commit is contained in:
parent
bf03a87c17
commit
152b20e7b4
2 changed files with 49 additions and 25 deletions
|
@ -8,28 +8,44 @@
|
||||||
(ns uxbox.main.data.projects
|
(ns uxbox.main.data.projects
|
||||||
(:require [cuerdas.core :as str]
|
(:require [cuerdas.core :as str]
|
||||||
[beicon.core :as rx]
|
[beicon.core :as rx]
|
||||||
[uxbox.util.rstore :as rs]
|
|
||||||
[uxbox.util.router :as r]
|
|
||||||
[uxbox.main.state :as st]
|
[uxbox.main.state :as st]
|
||||||
[uxbox.main.repo :as rp]
|
[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.i18n :refer (tr)]
|
||||||
[uxbox.util.schema :as sc]
|
[uxbox.util.schema :as sc]
|
||||||
[uxbox.main.data.pages :as udp]))
|
[uxbox.main.data.pages :as udp]))
|
||||||
|
|
||||||
;; --- Helpers
|
;; --- 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
|
(defn assoc-project
|
||||||
"A reduce function for assoc the project
|
"A reduce function for assoc the project to the state map."
|
||||||
to the state map."
|
[state {:keys [id] :as project}]
|
||||||
[state proj]
|
(let [project (dissoc project
|
||||||
(let [id (:id proj)]
|
:page-name :page-version
|
||||||
(update-in state [:projects id] merge proj)))
|
:page-data :page-metadata
|
||||||
|
:page-created-at :page-modified-at)]
|
||||||
|
(update-in state [:projects id] merge project)))
|
||||||
|
|
||||||
(defn dissoc-project
|
(defn dissoc-project
|
||||||
"A reduce function for dissoc the project
|
"A reduce function for dissoc the project from the state map."
|
||||||
from the state map."
|
|
||||||
[state id]
|
[state id]
|
||||||
(update-in state [:projects] dissoc id))
|
(update state :projects dissoc id))
|
||||||
|
|
||||||
;; --- Initialize
|
;; --- Initialize
|
||||||
|
|
||||||
|
@ -54,7 +70,9 @@
|
||||||
(defrecord ProjectsFetched [projects]
|
(defrecord ProjectsFetched [projects]
|
||||||
rs/UpdateEvent
|
rs/UpdateEvent
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
(reduce assoc-project state projects)))
|
(as-> state $
|
||||||
|
(reduce assoc-project-page $ projects)
|
||||||
|
(reduce assoc-project $ projects))))
|
||||||
|
|
||||||
(defn projects-fetched
|
(defn projects-fetched
|
||||||
[projects]
|
[projects]
|
||||||
|
|
|
@ -9,26 +9,32 @@
|
||||||
(:require [beicon.core :as rx]
|
(:require [beicon.core :as rx]
|
||||||
[uxbox.config :refer (url)]
|
[uxbox.config :refer (url)]
|
||||||
[uxbox.main.repo.pages :as pages]
|
[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
|
(defmethod request :fetch/projects
|
||||||
[type data]
|
[type data]
|
||||||
(let [url (str url "/projects")]
|
(letfn [(decode-payload [{:keys [payload] :as response}]
|
||||||
(send! {:url url :method :get})))
|
(assoc response :payload (mapv decode-page payload)))
|
||||||
|
(decode-page [{:keys [page-metadata page-data] :as project}]
|
||||||
(defmethod request :fetch/project
|
(assoc project
|
||||||
[_ id]
|
:page-metadata (t/decode page-metadata)
|
||||||
(let [url (str url "/projects/" id)]
|
:page-data (t/decode page-data)))]
|
||||||
(send! {:url url :method :get})))
|
;; 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
|
(defmethod request :fetch/project-by-token
|
||||||
[_ token]
|
[_ token]
|
||||||
(let [url (str url "/projects-by-token/" token)]
|
(letfn [(decode-pages [response]
|
||||||
(->> (send! {:url url :method :get})
|
(let [pages (->> (get-in response [:payload :pages])
|
||||||
(rx/map (fn [response]
|
(mapv pages/decode-page))]
|
||||||
(update-in response [:payload :pages]
|
(assoc-in response [:payload :pages] pages)))]
|
||||||
(fn [pages]
|
(->> (send! {:url (str url "/projects-by-token/" token)
|
||||||
(mapv pages/decode-page pages))))))))
|
:method :get})
|
||||||
|
(rx/map decode-pages))))
|
||||||
|
|
||||||
(defmethod request :create/project
|
(defmethod request :create/project
|
||||||
[_ data]
|
[_ data]
|
||||||
|
|
Loading…
Add table
Reference in a new issue