aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/Page.elm
blob: 39232e012ea1a31fb27de4d3326cc88ddc46024f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module Page exposing
  ( Page(..)
  , toHash
  , fromLocation
  )

import Navigation exposing (Location)
import UrlParser exposing (Parser, (</>), s)
import String

type Page =
  Home
  | Income
  | Categories
  | Statistics
  | NotFound

toHash : Page -> String
toHash page =
  case page of
    Home -> "#"
    Income -> "#income"
    Categories -> "#categories"
    Statistics -> "#statistics"
    NotFound -> "#notFound"

fromLocation : Location -> Page
fromLocation location =
  if location.hash == ""
    then
      Home
    else
      case UrlParser.parseHash pageParser location of
        Just page -> page
        Nothing -> NotFound

pageParser : Parser (Page -> a) a
pageParser =
  UrlParser.oneOf
    [ UrlParser.map Income (s "income")
    , UrlParser.map Categories (s "categories")
    , UrlParser.map Statistics (s "statistics")
    ]