aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/Model.elm
blob: a1d2bff47843efedbb75597fb20ef38786ab06b0 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
module Model exposing
  ( Model
  , init
  )

import Time exposing (Time)
import Json.Decode as Json

import Html.App as Html

import Page exposing (Page)
import Init as Init exposing (Init)
import Msg exposing (Msg)

import Model.View exposing (..)
import Model.Translations exposing (..)
import Model.Conf exposing (..)
import Model.InitResult exposing (..)
import LoggedIn.Model as LoggedInModel
import SignIn.Model as SignInModel

import Dialog
import Dialog.Model as DialogModel
import Dialog.Msg as DialogMsg

import Tooltip

import Utils.Maybe exposing (isJust)

type alias Model =
  { view : View
  , currentTime : Time
  , translations : Translations
  , conf : Conf
  , page : Page
  , errors : List String
  , dialog : Dialog.Model DialogModel.Model DialogMsg.Msg Msg
  , tooltip : Tooltip.Model
  }

init : Json.Value -> Result String Page -> (Model, Cmd Msg)
init payload result =
  let page =
        case result of
          Err _ -> Page.Home
          Ok page -> page
      model =
        case Json.decodeValue Init.decoder payload of
          Ok { time, translations, conf, result, windowSize } ->
            { view =
                case result of
                  InitEmpty ->
                    SignInView (SignInModel.init Nothing)
                  InitSuccess init ->
                    LoggedInView (LoggedInModel.init init)
                  InitError error ->
                    SignInView (SignInModel.init (Just error))
            , currentTime = time
            , translations = translations
            , conf = conf
            , page = page
            , errors = []
            , dialog = Dialog.init DialogModel.init Msg.Dialog
            , tooltip = Tooltip.init windowSize.width windowSize.height
            }
          Err error ->
            { view = SignInView (SignInModel.init (Just error))
            , currentTime = 0
            , translations = []
            , conf = { currency = "" }
            , page = page
            , errors = [ error ]
            , dialog = Dialog.init DialogModel.init Msg.Dialog
            , tooltip = Tooltip.init 0 0
            }
  in  (model, Cmd.none)