mirror of
https://github.com/penpot/penpot.git
synced 2025-02-15 11:38:24 -05:00
Move auth persistence to specific event.
Instead to have a state atom useless subscription.
This commit is contained in:
parent
63f2f3f0a7
commit
d988300a4c
2 changed files with 21 additions and 22 deletions
|
@ -6,7 +6,8 @@
|
||||||
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
|
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
|
||||||
|
|
||||||
(ns uxbox.data.auth
|
(ns uxbox.data.auth
|
||||||
(:require [beicon.core :as rx]
|
(:require [hodgepodge.core :refer [local-storage]]
|
||||||
|
[beicon.core :as rx]
|
||||||
[promesa.core :as p]
|
[promesa.core :as p]
|
||||||
[uxbox.repo :as rp]
|
[uxbox.repo :as rp]
|
||||||
[uxbox.rstore :as rs]
|
[uxbox.rstore :as rs]
|
||||||
|
@ -16,6 +17,21 @@
|
||||||
[uxbox.locales :refer (tr)]
|
[uxbox.locales :refer (tr)]
|
||||||
[uxbox.ui.messages :as uum]))
|
[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
|
;; --- Login
|
||||||
|
|
||||||
(defrecord Login [username password]
|
(defrecord Login [username password]
|
||||||
|
@ -25,11 +41,9 @@
|
||||||
(uum/error (tr "errors.auth"))
|
(uum/error (tr "errors.auth"))
|
||||||
(rx/empty))
|
(rx/empty))
|
||||||
(on-success [{value :payload}]
|
(on-success [{value :payload}]
|
||||||
(rx/of (rs/swap #(assoc % :auth value))
|
(->LoggedIn value))]
|
||||||
(r/navigate :dashboard/projects)))]
|
|
||||||
|
|
||||||
(->> (rp/do :login (merge (into {} this) {:scope "webapp"}))
|
(->> (rp/do :login (merge (into {} this) {:scope "webapp"}))
|
||||||
(rx/mapcat on-success)
|
(rx/map on-success)
|
||||||
(rx/catch on-error)))))
|
(rx/catch on-error)))))
|
||||||
|
|
||||||
(def ^:const ^:private +login-schema+
|
(def ^:const ^:private +login-schema+
|
||||||
|
|
|
@ -11,17 +11,13 @@
|
||||||
[lentes.core :as l]
|
[lentes.core :as l]
|
||||||
[uxbox.rstore :as rs]))
|
[uxbox.rstore :as rs]))
|
||||||
|
|
||||||
(def +storage+ local-storage)
|
|
||||||
(def ^:const ^:private +persistent-keys+
|
|
||||||
[:auth])
|
|
||||||
|
|
||||||
(defonce state (atom {}))
|
(defonce state (atom {}))
|
||||||
|
|
||||||
(defonce stream
|
(defonce stream
|
||||||
(rs/init {:dashboard {:project-order :name
|
(rs/init {:dashboard {:project-order :name
|
||||||
:project-filter ""}
|
:project-filter ""}
|
||||||
:route nil
|
:route nil
|
||||||
:auth (::auth +storage+)
|
:auth (::auth local-storage)
|
||||||
:workspace nil
|
:workspace nil
|
||||||
:shapes-by-id {}
|
:shapes-by-id {}
|
||||||
:elements-by-id {}
|
:elements-by-id {}
|
||||||
|
@ -30,18 +26,7 @@
|
||||||
:projects-by-id {}
|
:projects-by-id {}
|
||||||
:pages-by-id {}}))
|
:pages-by-id {}}))
|
||||||
|
|
||||||
(defn- persist-state!
|
|
||||||
[state]
|
|
||||||
(assoc! +storage+ ::auth (:auth state)))
|
|
||||||
|
|
||||||
(defn init
|
(defn init
|
||||||
"Initialize the state materialization."
|
"Initialize the state materialization."
|
||||||
[]
|
[]
|
||||||
(rx/to-atom stream state)
|
(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! %))))
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue