aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/Update.elm
blob: d3e82de48c89ae409b6580f773300c245c38f0c5 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
module Update exposing
  ( update
  , urlUpdate
  )

import Task
import Platform.Cmd exposing (Cmd)
import Navigation

import Page exposing (Page)

import Server

import Msg exposing (..)

import Model exposing (Model)
import Model.Translations exposing (getMessage)
import Model.View as V

import LoggedIn.Model as LoggedInModel
import LoggedIn.Msg as LoggedInMsg
import LoggedIn.Update as LoggedInUpdate

import SignIn.Model as SignInModel
import SignIn.Msg as SignInMsg
import SignIn.Update as SignInUpdate

import Dialog

import Utils.Http exposing (errorKey)

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of

    NoOp ->
      (model, Cmd.none)

    SignIn email ->
      ( applySignIn model (SignInMsg.WaitingServer)
      , Server.signIn email
          |> Task.perform
               (\error -> UpdateSignIn (SignInMsg.ErrorLogin (errorKey error)))
               (\() -> UpdateSignIn SignInMsg.ValidLogin)
      )

    GoLoggedInView init ->
      ( { model | view = V.LoggedInView (LoggedInModel.init init) }
      , Cmd.none
      )

    UpdateTime time ->
      ({ model | currentTime = time }, Cmd.none)

    GoSignInView ->
      ({ model | view = V.SignInView (SignInModel.init Nothing) }, Cmd.none)

    UpdateSignIn signInMsg ->
      (applySignIn model signInMsg, Cmd.none)

    UpdateLoggedIn loggedInMsg ->
      applyLoggedIn model loggedInMsg

    SignOut ->
      ( model
      , Server.signOut
          |> Task.perform (always NoOp) (always GoSignInView)
      )

    Dialog dialogMsg ->
      let (newDialog, command) = Dialog.update dialogMsg model.view model.dialog
      in  ( { model | dialog = newDialog }
          , command
          )

applySignIn : Model -> SignInMsg.Msg -> Model
applySignIn model signInMsg =
  case model.view of
    V.SignInView signInView ->
      { model | view = V.SignInView (SignInUpdate.update model.translations signInMsg signInView) }
    _ ->
      model

applyLoggedIn : Model -> LoggedInMsg.Msg -> (Model, Cmd Msg)
applyLoggedIn model loggedInMsg =
  case model.view of
    V.LoggedInView loggedInView ->
      let (loggedInView, cmd) = LoggedInUpdate.update model loggedInMsg loggedInView
      in  ( { model | view = V.LoggedInView loggedInView }
          , Cmd.map UpdateLoggedIn cmd
          )
    _ ->
      (model, Cmd.none)

urlUpdate : Result String Page -> Model -> (Model, Cmd Msg)
urlUpdate result model =
  case Debug.log "urlUpdate" result of
    Err _ ->
      (model, Navigation.modifyUrl (Page.toHash model.page))
    Ok page ->
      ({ model | page = page }, Cmd.none)