Router.purs 2.49 KB
Newer Older
1
module Gargantext.Router where
2 3

import Prelude
Abinaya Sudhir's avatar
Abinaya Sudhir committed
4

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
import Control.Alt ((<|>))
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Class (liftEff)
import Control.Monad.Eff.Console (CONSOLE, log)
import DOM (DOM)
import DOM.HTML (window)
import DOM.HTML.Window (localStorage)
import DOM.WebStorage.Storage (getItem)
import Data.Int (floor)
import Data.Maybe (Maybe(..))
import Routing.Match (Match)
import Routing.Match.Class (lit, num)

data Routes
  = Home
  | Login
21
  | AddCorpus
22
  | DocView
Abinaya Sudhir's avatar
Abinaya Sudhir committed
23
  | SearchView
Mael NICOLAS's avatar
Mael NICOLAS committed
24
  | UserPage Int
25
  | DocAnnotation Int
26
  | Tabview
27
  | CorpusAnalysis
Sudhir Kumar's avatar
Sudhir Kumar committed
28
  | PGraphExplorer
29
  | NGramsTable
30
  | Dashboard
31 32


33
instance showRoutes :: Show Routes where
Mael NICOLAS's avatar
Mael NICOLAS committed
34 35 36
  show Login = "Login"
  show AddCorpus = "AddCorpus"
  show DocView = "DocView"
Abinaya Sudhir's avatar
Abinaya Sudhir committed
37
  show SearchView = "SearchView"
Mael NICOLAS's avatar
Mael NICOLAS committed
38
  show (UserPage i) = "UserPage"
39
  show (DocAnnotation i)= "DocumentView"
Mael NICOLAS's avatar
Mael NICOLAS committed
40 41 42 43 44 45
  show Tabview = "Tabview"
  show CorpusAnalysis = "corpus"
  show PGraphExplorer = "graphExplorer"
  show NGramsTable = "NGramsTable"
  show Dashboard = "Dashboard"
  show Home = "Home"
46 47 48 49 50 51

int :: Match Int
int = floor <$> num

routing :: Match Routes
routing =
Mael NICOLAS's avatar
Mael NICOLAS committed
52 53
      Login          <$  route "login"
  <|> Tabview        <$  route "tabview"
54
  <|> DocAnnotation  <$> (route "documentView" *> int)
Mael NICOLAS's avatar
Mael NICOLAS committed
55 56 57 58 59 60 61 62 63
  <|> UserPage       <$> (route "user" *> int)
  <|> SearchView     <$ route "search"
  <|> DocView        <$ route "docView"
  <|> AddCorpus      <$ route "addCorpus"
  <|> CorpusAnalysis <$ route "corpus"
  <|> PGraphExplorer <$ route "graphExplorer"
  <|> NGramsTable    <$ route "ngrams"
  <|> Dashboard      <$ route "dashboard"
  <|> Home           <$ lit ""
64
  where
65
    route str      = lit "" *> lit str
66

67 68 69 70 71 72 73 74 75 76
routeHandler :: forall e. ( Maybe Routes -> Routes -> Eff
                            ( dom     :: DOM
                            , console :: CONSOLE
                            | e
                            ) Unit
                          ) -> Maybe Routes -> Routes -> Eff
                            ( dom     :: DOM
                            , console :: CONSOLE
                            | e
                            ) Unit
77 78
routeHandler dispatchAction old new = do
  liftEff $ log $ "change route : " <> show new
79 80 81
  w      <- window
  ls     <- localStorage w
  token  <- getItem "accessToken" ls
82 83 84 85
  let tkn = token
  liftEff $ log $ "JWToken : " <> show tkn
  case tkn of
    Nothing -> do
Abinaya Sudhir's avatar
Abinaya Sudhir committed
86
      dispatchAction old new
87 88 89 90
      liftEff $ log $ "called SignIn Route :"
    Just t -> do
      dispatchAction old new
      liftEff $ log $ "called Route : " <> show new