0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-21 06:02:32 -05:00

WIP: add basic data persistence.

This commit is contained in:
Andrey Antukh 2015-12-14 23:57:47 +02:00
parent 6b6aba7358
commit ebec1eb268
9 changed files with 73 additions and 47 deletions

View file

@ -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 %))))

View file

@ -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})))

View file

@ -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 "#<event:u.s.p/go-to-project>"))))
(-write writer "#<event:u.s.p/initialize-workspace>"))))
(defn go-to-project
"A shortcut event that redirects the user to the

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -14,7 +14,6 @@
(defn set!
[kind]
(println "lightbox$set!" kind)
(reset! +current+ kind))
(defn close!

View file

@ -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?)