API.purs 996 Bytes
Newer Older
Sudhir Kumar's avatar
Sudhir Kumar committed
1
module Gargantext.Pages.Corpus.User.Users.API where
2

3
import Prelude
4 5 6 7 8

import Control.Monad.Trans.Class (lift)
import Data.Either (Either(..))
import Data.Lens (set)
import Data.Maybe (Maybe(..))
Sudhir Kumar's avatar
Sudhir Kumar committed
9 10 11
import Effect.Aff (Aff)
import Effect.Class (liftEffect)
import Effect.Console (log)
12
import Gargantext.Config.REST (get)
Sudhir Kumar's avatar
Sudhir Kumar committed
13
import Gargantext.Pages.Corpus.User.Users.Types (Action(..), State, User, _user)
14
import Thermite (PerformAction, modifyState)
15

16
getUser :: Int -> Aff (Either String User)
17
getUser id = get $ "http://localhost:8008/node/" <> show id
18 19


20
performAction :: PerformAction State {} Action
21
performAction NoOp _ _ = void do
Sudhir Kumar's avatar
Sudhir Kumar committed
22
  modifyState identity
23 24 25 26
performAction (FetchUser userId) _ _ = void do
  value <- lift $ getUser userId
  _ <- case value of
    (Right user) -> modifyState \state -> set _user (Just user) state
27
    (Left err) -> do
Sudhir Kumar's avatar
Sudhir Kumar committed
28 29 30
      _ <- liftEffect $ log err
      modifyState identity
  liftEffect <<< log $ "Fetching user..."
31
performAction _ _ _ = void do
Sudhir Kumar's avatar
Sudhir Kumar committed
32
  modifyState identity