Commit 93b6d5dc authored by Alexandre Delanoë's avatar Alexandre Delanoë

[CHORE] Intances of Sessions (compiling but not tested with load/save sessions yet).

parent 97d89bae
......@@ -3,7 +3,8 @@ module Gargantext.Ends
-- ( )
where
import Prelude (class Eq, class Show, identity, show, ($), (<>))
import Prelude (class Eq, class Show, identity, show, ($), (<>), bind, pure)
import Data.Argonaut ( class DecodeJson, decodeJson, class EncodeJson, encodeJson, (:=), (~>), jsonEmptyObject, (.:))
import Data.Foldable (foldMap)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Eq (genericEq)
......@@ -45,6 +46,24 @@ instance showBackend :: Show Backend where
instance toUrlBackendString :: ToUrl Backend String where
toUrl = backendUrl
-- JSON instances
instance encodeJsonBackend :: EncodeJson Backend where
encodeJson (Backend {name, baseUrl, prePath, version})
= "name" := name
~> "baseUrl" := baseUrl
~> "prePath" := prePath
~> "version" := show version
~> jsonEmptyObject
instance decodeJsonBackend :: DecodeJson Backend where
decodeJson json = do
obj <- decodeJson json
name <- obj .: "objet"
baseUrl <- obj .: "baseUrl"
prePath <- obj .: "prePath"
version <- obj .: "version"
pure $ Backend {name, baseUrl, prePath, version}
-- | Encapsulates the data needed to construct a url to a frontend
-- | server (either for the app or static content)
newtype Frontend = Frontend
......
-- | A module for authenticating to create sessions and handling them
module Gargantext.Sessions where
import Prelude (class Eq, class Show, Unit, const, otherwise, pure, show, unit, ($), (*>), (<*), (<$>), (<>), (==), (/=), (>>=), (<<<))
import Prelude (class Eq, class Show, Unit, const, otherwise, pure, show, unit, ($), (*>), (<*), (<$>), (<>), (==), (/=), (>>=), (<<<), bind)
import Data.Argonaut ( class DecodeJson, decodeJson, class EncodeJson, encodeJson, (:=), (~>), jsonEmptyObject, (.:), Json, fromArray)
import Data.Array as A
import Data.Traversable (traverse)
import DOM.Simple.Console (log2)
import Data.Either (Either(..))
import Data.Generic.Rep (class Generic)
......@@ -33,6 +35,9 @@ newtype Session = Session
, token :: String
, treeId :: Int }
------------------------------------------------------------------------
-- | Main instances
derive instance genericSession :: Generic Session _
instance eqSession :: Eq Session where
......@@ -56,6 +61,27 @@ sessionId = SessionId <<< show
instance toUrlSessionString :: ToUrl Session String where
toUrl = sessionUrl
--------------------
-- | JSON instances
instance encodeJsonSession :: EncodeJson Session where
encodeJson (Session {backend, username, token, treeId})
= "backend" := encodeJson backend
~> "username" := username
~> "token" := token
~> "treeId" := treeId
~> jsonEmptyObject
instance decodeJsonSession :: DecodeJson Session where
decodeJson json = do
obj <- decodeJson json
backend <- obj .: "backend"
username <- obj .: "username"
token <- obj .: "token"
treeId <- obj .: "treeId"
pure $ Session { backend, username, token, treeId}
------------------------------------------------------------------------
newtype Sessions = Sessions (Seq Session)
derive instance genericSessions :: Generic Sessions _
......@@ -63,6 +89,23 @@ derive instance genericSessions :: Generic Sessions _
instance eqSessions :: Eq Sessions where
eq = genericEq
instance decodeJsonSessions :: DecodeJson Sessions where
decodeJson json = do
ss <- decodeSessions' json
pure (Sessions (Seq.fromFoldable ss))
where
decodeSessions' :: Json -> Either String (Array Session)
decodeSessions' json = decodeJson json >>= traverse decodeJson
instance encodeJsonSessions :: EncodeJson Sessions where
encodeJson (Sessions ss) = "sessions" := (encodeSessions ss)
~> jsonEmptyObject
where
encodeSessions :: Seq Session -> Json
encodeSessions ss = fromArray $ encodeJson <$> (Seq.toUnfoldable ss)
unSessions :: Sessions -> Array Session
unSessions (Sessions s) = A.fromFoldable s
......@@ -125,13 +168,14 @@ null (Sessions seq) = Seq.null seq
-- | Will attempt to load saved sessions from localstorage. should log if decoding fails
loadSessions :: Effect Sessions
loadSessions = pure empty
-- loadSessions = window >>= localStorage >>= getItem "auths" >>= traverse decode
-- where
-- decode :: String -> Effect (Maybe Sessions)
-- decode = ret <<< runExcept <<< decodeJSON
-- ret (Right v) = pure $ Just v
-- ret (Left e) = log2 "Error reading serialised sessions:" e *> pure (Malformed e)
{-
loadSessions = window >>= localStorage >>= getItem "auths" >>= traverse decode
where
decode :: String -> Effect (Maybe Sessions)
decode = ret <<< runExcept <<< decodeJson
ret (Right v) = pure $ Just v
ret (Left e) = log2 "Error reading serialised sessions:" e *> pure (Malformed e)
-}
saveSessions :: Sessions -> Effect Sessions
saveSessions sessions = effect *> pure sessions
where
......
......@@ -282,17 +282,31 @@ instance showOrderBy :: Show OrderBy where
show = genericShow
------------------------------------------------------------
data ApiVersion = V10 | V11
-- V0 is the dummy case (impossible)
data ApiVersion = V0 | V10 | V11
instance showApiVersion :: Show ApiVersion where
show V0 = "v0"
show V10 = "v1.0"
show V11 = "v1.1"
------------------------------------------------------------
instance eqApiVersion :: Eq ApiVersion where
eq V10 V10 = true
eq V11 V11 = true
eq _ _ = false
instance encodeJsonApiVersion :: EncodeJson ApiVersion where
encodeJson v = encodeJson (show v)
instance decodeJsonApiVersion :: DecodeJson ApiVersion where
decodeJson json = do
v <- decodeJson json
case v of
"v1.0" -> pure V10
"v1.1" -> pure V11
_ -> pure V0
------------------------------------------------------------
data CTabNgramType = CTabTerms | CTabSources | CTabAuthors | CTabInstitutes
derive instance eqCTabNgramType :: Eq CTabNgramType
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment