aboutsummaryrefslogtreecommitdiff
path: root/src/client/Model/User.elm
blob: f6e8147f9f090adf9a35bfafd556464f0256a420 (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 Decode exposing (Decoder)
import Dict exposing (Dict)

type alias Users = Dict UserId User

type alias UserId = Int

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

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

userWithIdDecoder : Decode.Decoder (UserId, User)
userWithIdDecoder =
  Decode.map2 (,)
    (Decode.field "id" userIdDecoder)
    userDecoder

userIdDecoder : Decoder UserId
userIdDecoder = Decode.int

userDecoder : Decoder User
userDecoder =
  Decode.map2 User
    (Decode.field "name" Decode.string)
    (Decode.field "email" Decode.string)

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