Actions.purs 2.75 KB
Newer Older
1 2
-- | Module Description

3
module Gargantext.Pages.Layout.Actions where
4

5
import Data.Either                                     (Either(..))
6
import Data.Maybe                                      (Maybe(..))
7 8
import Data.Lens                                       (Prism', prism)
import Effect.Class                                    (liftEffect)
9
import Thermite                                        (PerformAction, modifyState, modifyState_)
10
import Routing.Hash                                    (setHash)
11

12
import Gargantext.Components.GraphExplorer.Types as GET
13 14
import Gargantext.Components.Login                  as LN
import Gargantext.Components.Modals.Modal              (modalShow)
15
import Gargantext.Pages.Annuaire             as Annuaire
16
import Gargantext.Pages.Layout.States                  (AppState)
17
import Gargantext.Prelude
18
import Gargantext.Router                               (Routes)
19

20
------------------------------------------------------------------------
21 22

data Action
23
  = LoginA     LN.Action
24
  | SetRoute   Routes
25
  | GraphExplorerA     GET.Action
26
  | AnnuaireAction     Annuaire.Action
27
  | ShowLogin
28
  | Logout
29
  | ShowAddCorpus
Sudhir Kumar's avatar
Sudhir Kumar committed
30
  | ToggleTree
31 32


33
performAction :: PerformAction AppState {} Action
34 35 36
performAction (SetRoute route)  _ _ = void do
  modifyState $ _ {currentRoute = pure route}

Sudhir Kumar's avatar
Sudhir Kumar committed
37
performAction (ToggleTree)  _ (state) = void do -- TODO
Sudhir Kumar's avatar
Sudhir Kumar committed
38 39
  modifyState $ _ {showTree = not (state.showTree)}

40
performAction ShowLogin  _ _ = void do
Sudhir Kumar's avatar
Sudhir Kumar committed
41
  liftEffect $ modalShow "loginModal"
42 43
  modifyState $ _ {showLogin = true}

44 45 46 47 48 49 50
performAction Logout _ _ = do
  loginState <- liftEffect do
    LN.setAuthData Nothing
    setHash "/"
    LN.initialState
  modifyState_ $ _ {currentRoute = Nothing, loginState = loginState}

51 52
---------------------------------------------------------
-- TODO chose one of them
53
performAction ShowAddCorpus  _ _ = void do
Sudhir Kumar's avatar
Sudhir Kumar committed
54
  liftEffect $ modalShow "addCorpus"
55 56
  modifyState $ _ {showCorpus = true}

57
---------------------------------------------------------
58

59 60 61 62 63
performAction (LoginA          _) _ _ = pure unit
performAction (GraphExplorerA  _) _ _ = pure unit
performAction (AnnuaireAction  _) _ _ = pure unit
  -- liftEffect $ modalShow "addCorpus"
  -- modifyState $ _ {showCorpus = true}
64 65 66 67 68 69 70 71 72

----------------------------------------------------------

_loginAction :: Prism' Action LN.Action
_loginAction = prism LoginA \action ->
  case action of
    LoginA caction -> Right caction
    _-> Left action

73 74 75 76 77 78
_annuaireAction :: Prism' Action Annuaire.Action
_annuaireAction = prism AnnuaireAction \action ->
  case action of
       AnnuaireAction a -> Right a
       _                -> Left  action

79 80 81 82 83
_graphExplorerAction :: Prism' Action GET.Action
_graphExplorerAction = prism GraphExplorerA \action ->
  case action of
    GraphExplorerA caction -> Right caction
    _-> Left action