aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/Model/User.elm
blob: 02f2cea1393b111d026c81d313fc402e96ed24cc (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
module Model.User exposing
  ( Users
  , usersDecoder
  , User
  , userDecoder
  , UserId
  , userIdDecoder
  , getUserName
  )

import Json.Decode as Json exposing ((:=))
import Dict exposing (Dict)

type alias Users = Dict UserId User

type alias UserId = Int

type alias User =
  { name : String
  , email : String
  }

usersDecoder : Json.Decoder Users
usersDecoder = Json.map Dict.fromList (Json.list userWithIdDecoder)

userWithIdDecoder : Json.Decoder (UserId, User)
userWithIdDecoder =
  Json.object2 (,)
    ("id" := userIdDecoder)
    userDecoder

userIdDecoder : Json.Decoder UserId
userIdDecoder = Json.int

userDecoder : Json.Decoder User
userDecoder =
  Json.object2 User
    ("name" := Json.string)
    ("email" := Json.string)

getUserName : Users -> UserId -> Maybe String
getUserName users userId =
  Dict.get userId users
    |> Maybe.map .name