PageRouter.purs 2.6 KB
Newer Older
1 2 3
module PageRouter where

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
24
  | UserPage
25
  | AnnotationDocumentView Int
26
  | Tabview
27
  | CorpusAnalysis
28
  | Dashboard
29

30 31

instance showRoutes :: Show Routes where
32 33 34 35
  show Home       = "Home"
  show Login      = "Login"
  show AddCorpus  = "AddCorpus"
  show DocView    = "DocView"
Abinaya Sudhir's avatar
Abinaya Sudhir committed
36
  show SearchView = "SearchView"
37
  show UserPage   = "UserPage"
38
  show (AnnotationDocumentView i)  = "DocumentView"
39
  show Tabview   = "Tabview"
40 41
  show CorpusAnalysis   = "Corpus"
  show Dashboard = "Dashboard"
42 43 44 45

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

46

47 48
routing :: Match Routes
routing =
49
   loginRoute
50
  <|> tabview
51
  <|> documentView
52 53
  <|> userPageRoute
  <|> searchRoute
54
  <|> docviewRoute
Abinaya Sudhir's avatar
Abinaya Sudhir committed
55
  <|> addcorpusRoute
56
  <|> corpusAnalysis
57
  <|> dashboard
58
  <|> home
59
  where
60
    tabview  = Tabview   <$ route "tabview"
61
    documentView   = AnnotationDocumentView <$> (route "documentView" *> int)
62 63 64 65 66
    userPageRoute  = UserPage   <$ route "userPage"
    searchRoute    = SearchView <$ route "search"
    docviewRoute   = DocView    <$ route "docView"
    addcorpusRoute = AddCorpus  <$ route "addCorpus"
    loginRoute     = Login      <$ route "login"
67
    corpusAnalysis  = CorpusAnalysis <$ route "corpus"
68
    dashboard = Dashboard <$ route "dashboard"
69
    home           = Home       <$ lit ""
70
    route str      = lit "" *> lit str
71

72 73 74 75 76 77 78 79 80 81 82

routeHandler :: forall e. ( Maybe Routes -> Routes -> Eff
                            ( dom     :: DOM
                            , console :: CONSOLE
                            | e
                            ) Unit
                          ) -> Maybe Routes -> Routes -> Eff
                            ( dom     :: DOM
                            , console :: CONSOLE
                            | e
                            ) Unit
83 84
routeHandler dispatchAction old new = do
  liftEff $ log $ "change route : " <> show new
85 86 87
  w      <- window
  ls     <- localStorage w
  token  <- getItem "accessToken" ls
88 89 90 91
  let tkn = token
  liftEff $ log $ "JWToken : " <> show tkn
  case tkn of
    Nothing -> do
Abinaya Sudhir's avatar
Abinaya Sudhir committed
92
      dispatchAction old new
93 94 95 96
      liftEff $ log $ "called SignIn Route :"
    Just t -> do
      dispatchAction old new
      liftEff $ log $ "called Route : " <> show new