Created
February 5, 2019 01:19
-
-
Save AntonRich/847033e2de357c17149037fe57b56af5 to your computer and use it in GitHub Desktop.
The list of players doesn't fetch.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- MODEL | |
type alias Model = | |
{ gamesList : List Game | |
, playersList : List Player | |
} | |
type alias Player = | |
{ displayName : Maybe String | |
, id : Int | |
, score : Int | |
, username : String | |
} | |
initialModel : Model | |
initialModel = | |
{ gamesList = [] | |
, playersList = [] | |
} | |
initialCommand : Cmd Msg | |
initialCommand = | |
Cmd.batch | |
[ fetchGamesList | |
, fetchPlayersList | |
] | |
-- UPDATE | |
type Msg | |
= FetchGamesList (Result Http.Error (List Game)) | |
| FetchPlayersList (Result Http.Error (List Player)) | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
case msg of | |
FetchGamesList result -> | |
case result of | |
Ok games -> | |
( { model | gamesList = games }, Cmd.none ) | |
Err _ -> | |
Debug.log "Error fetching games from API." | |
( model, Cmd.none ) | |
FetchPlayersList result -> | |
case result of | |
Ok players -> | |
( { model | playersList = players }, Cmd.none ) | |
Err _ -> | |
Debug.log "Error fetching players from API" | |
( model, Cmd.none ) | |
-- API | |
fetchPlayersList : Cmd Msg | |
fetchPlayersList = | |
Http.get | |
{ url = "/api/players" | |
, expect = Http.expectJson FetchPlayersList decodePlayersList | |
} | |
decodePlayersList : Decode.Decoder (List Player) | |
decodePlayersList = | |
decodePlayer | |
|> Decode.list | |
|> Decode.at [ "data" ] | |
decodePlayer : Decode.Decoder Player | |
decodePlayer = | |
Decode.map4 Player | |
(Decode.maybe (Decode.field "display_name" Decode.string)) | |
(Decode.field "id" Decode.int) | |
(Decode.field "score" Decode.int) | |
(Decode.field "username" Decode.string) | |
-- VIEW | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ gamesIndex model | |
, playersIndex model | |
] | |
playersIndex : Model -> Html msg | |
playersIndex model = | |
if List.isEmpty model.playersList then | |
div [] [] | |
else | |
div [ class "players-index" ] | |
[ h2 [] [ text "Players" ] | |
, model.playersList | |
|> List.sortBy .score | |
|> List.reverse | |
|> playersList | |
] | |
playersList : List Player -> Html msg | |
playersList players = | |
ul [ class "players-list" ] (List.map playersListItem players) | |
playersListItem : Player -> Html msg | |
playersListItem player = | |
li [ class "player-item" ] | |
[ case player.displayName of | |
Just displayName -> | |
strong [] [ text displayName ] | |
Nothing -> | |
strong [] [ text player.username ] | |
, p [] [ text (String.fromInt player.score) ] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment