Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
purescript-gargantext
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Grégoire Locqueville
purescript-gargantext
Commits
998ad895
Commit
998ad895
authored
Oct 15, 2019
by
Alexandre Delanoë
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[FIX] session persistence DecodeJson Instances of Sessions and Backend.
parent
38061569
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
26 deletions
+36
-26
Ends.purs
src/Gargantext/Ends.purs
+1
-1
Sessions.purs
src/Gargantext/Sessions.purs
+35
-25
No files found.
src/Gargantext/Ends.purs
View file @
998ad895
...
@@ -58,7 +58,7 @@ instance encodeJsonBackend :: EncodeJson Backend where
...
@@ -58,7 +58,7 @@ instance encodeJsonBackend :: EncodeJson Backend where
instance decodeJsonBackend :: DecodeJson Backend where
instance decodeJsonBackend :: DecodeJson Backend where
decodeJson json = do
decodeJson json = do
obj <- decodeJson json
obj <- decodeJson json
name <- obj .: "
objet
"
name <- obj .: "
name
"
baseUrl <- obj .: "baseUrl"
baseUrl <- obj .: "baseUrl"
prePath <- obj .: "prePath"
prePath <- obj .: "prePath"
version <- obj .: "version"
version <- obj .: "version"
...
...
src/Gargantext/Sessions.purs
View file @
998ad895
-- | A module for authenticating to create sessions and handling them
-- | A module for authenticating to create sessions and handling them
module Gargantext.Sessions where
module Gargantext.Sessions where
import Prelude (class Eq, class Show, Unit, const, otherwise, pure, show, unit, ($), (*>), (<*), (<$>), (<>), (==), (/=), (>>=), (>=>), (<<<), bind)
import Prelude (class Eq, class Show, Unit, const, otherwise, pure, show, unit, ($), (*>), (<*), (<$>), (<>), (==), (/=), (>>=), (>=>), (<<<), bind
, map
)
import Control.Monad.Except (runExcept)
import Control.Monad.Except (runExcept)
import Data.Argonaut ( class DecodeJson, decodeJson, class EncodeJson, encodeJson, (:=), (~>), (.:), Json)
import Data.Argonaut ( class DecodeJson, decodeJson, class EncodeJson, encodeJson, (:=), (~>), (.:), Json)
import Data.Argonaut.Core (Json, fromArray, jsonEmptyObject, stringify)
import Data.Argonaut.Core (Json, fromArray, jsonEmptyObject, stringify)
...
@@ -26,6 +26,7 @@ import Gargantext.Components.Login.Types
...
@@ -26,6 +26,7 @@ import Gargantext.Components.Login.Types
import Gargantext.Config.REST as REST
import Gargantext.Config.REST as REST
import Gargantext.Ends (class ToUrl, Backend, backendUrl, toUrl, sessionPath)
import Gargantext.Ends (class ToUrl, Backend, backendUrl, toUrl, sessionPath)
import Gargantext.Routes (SessionRoute)
import Gargantext.Routes (SessionRoute)
import Gargantext.Prelude (logs)
import Gargantext.Types (NodePath, SessionId(..), nodePath)
import Gargantext.Types (NodePath, SessionId(..), nodePath)
import Gargantext.Utils.Reactix as R2
import Gargantext.Utils.Reactix as R2
...
@@ -85,7 +86,7 @@ instance decodeJsonSession :: DecodeJson Session where
...
@@ -85,7 +86,7 @@ instance decodeJsonSession :: DecodeJson Session where
------------------------------------------------------------------------
------------------------------------------------------------------------
newtype Sessions = Sessions (Seq Session)
data Sessions = Sessions { sessions :: (Seq Session)}
derive instance genericSessions :: Generic Sessions _
derive instance genericSessions :: Generic Sessions _
...
@@ -95,21 +96,23 @@ instance eqSessions :: Eq Sessions where
...
@@ -95,21 +96,23 @@ instance eqSessions :: Eq Sessions where
instance decodeJsonSessions :: DecodeJson Sessions where
instance decodeJsonSessions :: DecodeJson Sessions where
decodeJson json = do
decodeJson json = do
ss <- decodeSessions json
ss <- decodeSessions json
pure (Sessions
(Seq.fromFoldable ss)
)
pure (Sessions
{sessions:Seq.fromFoldable ss}
)
where
where
decodeSessions :: Json -> Either String (Array Session)
decodeSessions :: Json -> Either String (Array Session)
decodeSessions json = decodeJson json >>= traverse decodeJson
decodeSessions json = decodeJson json
>>= \obj -> obj .: "sessions"
>>= traverse decodeJson
instance encodeJsonSessions :: EncodeJson Sessions where
instance encodeJsonSessions :: EncodeJson Sessions where
encodeJson (Sessions
ss
) = "sessions" := (encodeSessions ss)
encodeJson (Sessions
{sessions:ss}
) = "sessions" := (encodeSessions ss)
~> jsonEmptyObject
~> jsonEmptyObject
where
where
encodeSessions :: Seq Session -> Json
encodeSessions :: Seq Session -> Json
encodeSessions ss2 = fromArray $ encodeJson <$> (Seq.toUnfoldable ss2)
encodeSessions ss2 = fromArray $ encodeJson <$> (Seq.toUnfoldable ss2)
unSessions :: Sessions -> Array Session
unSessions :: Sessions -> Array Session
unSessions (Sessions
s
) = A.fromFoldable s
unSessions (Sessions
{sessions:s}
) = A.fromFoldable s
useSessions :: R.Hooks (R2.Reductor Sessions Action)
useSessions :: R.Hooks (R2.Reductor Sessions Action)
useSessions = R2.useReductor actAndSave (const loadSessions) unit
useSessions = R2.useReductor actAndSave (const loadSessions) unit
...
@@ -118,11 +121,11 @@ useSessions = R2.useReductor actAndSave (const loadSessions) unit
...
@@ -118,11 +121,11 @@ useSessions = R2.useReductor actAndSave (const loadSessions) unit
actAndSave s a = act s a >>= saveSessions
actAndSave s a = act s a >>= saveSessions
lookup :: SessionId -> Sessions -> Maybe Session
lookup :: SessionId -> Sessions -> Maybe Session
lookup sid (Sessions
ss
) = Seq.head (Seq.filter f ss) where
lookup sid (Sessions
{sessions:ss}
) = Seq.head (Seq.filter f ss) where
f s = sid == sessionId s
f s = sid == sessionId s
cons :: Session -> Sessions -> Sessions
cons :: Session -> Sessions -> Sessions
cons s (Sessions
ss) = Sessions (Seq.cons s ss)
cons s (Sessions
{sessions:ss}) = Sessions {sessions:(Seq.cons s ss)}
tryCons :: Session -> Sessions -> Either Unit Sessions
tryCons :: Session -> Sessions -> Either Unit Sessions
tryCons s ss = try (lookup sid ss) where
tryCons s ss = try (lookup sid ss) where
...
@@ -131,7 +134,7 @@ tryCons s ss = try (lookup sid ss) where
...
@@ -131,7 +134,7 @@ tryCons s ss = try (lookup sid ss) where
try _ = Left unit
try _ = Left unit
remove :: SessionId -> Sessions -> Sessions
remove :: SessionId -> Sessions -> Sessions
remove sid (Sessions
ss) = Sessions (Seq.filter f ss)
where
remove sid (Sessions
{sessions:ss}) = Sessions {sessions: Seq.filter f ss}
where
f s = sid /= sessionId s
f s = sid /= sessionId s
tryRemove :: SessionId -> Sessions -> Either Unit Sessions
tryRemove :: SessionId -> Sessions -> Either Unit Sessions
...
@@ -160,24 +163,31 @@ localStorageKey :: String
...
@@ -160,24 +163,31 @@ localStorageKey :: String
localStorageKey = "garg-sessions"
localStorageKey = "garg-sessions"
empty :: Sessions
empty :: Sessions
empty = Sessions
Seq.empty
empty = Sessions
{sessions:Seq.empty}
-- True if there are no sessions stored
-- True if there are no sessions stored
null :: Sessions -> Boolean
null :: Sessions -> Boolean
null (Sessions seq) = Seq.null seq
null (Sessions {sessions:seq}) = Seq.null seq
-- | Will attempt to load saved sessions from localstorage. should log
-- | Will attempt to load saved sessions from localstorage. should log
-- | if decoding fails
-- | if decoding fails
loadSessions :: Effect Sessions
loadSessions :: Effect Sessions
loadSessions = getls >>= getItem localStorageKey >>= handleMaybe where
loadSessions = do
-- getls >>= getItem localStorageKey >>= handleMaybe
tls <- getls
ls <- getItem localStorageKey tls
_ <- logs (show <$> ls)
handleMaybe ls
where
-- a localstorage lookup can find nothing
-- a localstorage lookup can find nothing
handleMaybe (Just val) = handleEither (parse val >>= decode)
handleMaybe (Just val) = handleEither (parse val >>= decode)
handleMaybe Nothing = pure empty
handleMaybe Nothing = pure empty
-- either parsing or decoding could fail, hence two errors
-- either parsing or decoding could fail, hence two errors
handleEither (Left err) = err *> pure empty
handleEither (Left err) = err *> pure empty
handleEither (Right ss) = pure ss
handleEither (Right ss) = pure ss
parse s = mapLeft (log2 "Error parsing serialised sessions:") (jsonParser s)
parse s = mapLeft (log2 "Error parsing serialised sessions:") (jsonParser s)
decode j = mapLeft (log2 "Error decoding serialised sessions:") (decodeJson j)
decode j = mapLeft (log2 "Error decoding serialised sessions:") (decodeJson j)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment