0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-13 10:38:13 -05:00

Move auth persistence to specific event.

Instead to have a state atom useless subscription.
This commit is contained in:
Andrey Antukh 2016-03-19 19:09:06 +02:00
parent 63f2f3f0a7
commit d988300a4c
2 changed files with 21 additions and 22 deletions

View file

@ -6,7 +6,8 @@
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
(ns uxbox.data.auth
(:require [beicon.core :as rx]
(:require [hodgepodge.core :refer [local-storage]]
[beicon.core :as rx]
[promesa.core :as p]
[uxbox.repo :as rp]
[uxbox.rstore :as rs]
@ -16,6 +17,21 @@
[uxbox.locales :refer (tr)]
[uxbox.ui.messages :as uum]))
;; --- Logged In
(defrecord LoggedIn [data]
rs/UpdateEvent
(-apply-update [this state]
(assoc state :auth data))
rs/WatchEvent
(-apply-watch [this state s]
(rx/of (r/navigate :dashboard/projects)))
rs/EffectEvent
(-apply-effect [this state]
(assoc! local-storage ::auth data)))
;; --- Login
(defrecord Login [username password]
@ -25,11 +41,9 @@
(uum/error (tr "errors.auth"))
(rx/empty))
(on-success [{value :payload}]
(rx/of (rs/swap #(assoc % :auth value))
(r/navigate :dashboard/projects)))]
(->LoggedIn value))]
(->> (rp/do :login (merge (into {} this) {:scope "webapp"}))
(rx/mapcat on-success)
(rx/map on-success)
(rx/catch on-error)))))
(def ^:const ^:private +login-schema+

View file

@ -11,17 +11,13 @@
[lentes.core :as l]
[uxbox.rstore :as rs]))
(def +storage+ local-storage)
(def ^:const ^:private +persistent-keys+
[:auth])
(defonce state (atom {}))
(defonce stream
(rs/init {:dashboard {:project-order :name
:project-filter ""}
:route nil
:auth (::auth +storage+)
:auth (::auth local-storage)
:workspace nil
:shapes-by-id {}
:elements-by-id {}
@ -30,18 +26,7 @@
:projects-by-id {}
:pages-by-id {}}))
(defn- persist-state!
[state]
(assoc! +storage+ ::auth (:auth state)))
(defn init
"Initialize the state materialization."
[]
(rx/to-atom stream state)
(let [lens (l/select-keys +persistent-keys+)
stream (->> (l/focus-atom lens state)
(rx/from-atom)
(rx/dedupe)
(rx/debounce 1000)
(rx/tap #(println "[save]")))]
(rx/on-value stream #(persist-state! %))))
(rx/to-atom stream state))