Created
April 13, 2017 23:28
-
-
Save busypeoples/1e6e93dd1cdb995629e5b6670c27125a to your computer and use it in GitHub Desktop.
Elm Example - Side-Effects Handling
This file contains 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
const jsonServer = require('json-server') | |
const server = jsonServer.create() | |
const router = jsonServer.router('db.json') | |
const middlewares = jsonServer.defaults() | |
server.use(middlewares) | |
server.use(router) | |
server.listen(3000, () => { | |
console.log('JSON Server is running. Listening at 3000.') | |
}) |
This file contains 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
{ | |
"users": [ | |
{ "id": 1, "username": "Foo", "role": "Admin" }, | |
{ "id": 2, "username": "Bar", "role": "Guest" } | |
] | |
} |
This file contains 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
module LoadUsers exposing (..) | |
import Html exposing (Html, div, h1, text) | |
import Http | |
import Json.Decode as Decode | |
main = | |
Html.program | |
{ init = init | |
, update = update | |
, view = view | |
, subscriptions = \_ -> Sub.none | |
} | |
-- MODEL | |
type alias User = | |
{ id : Int | |
, username : String | |
, role : String | |
} | |
type alias Model = List User | |
-- INIT | |
init : (Model, Cmd Msg) | |
init = | |
([], loadUsers) | |
-- UPDATE | |
type Msg | |
= LoadUsers | |
| LoadedUsers (Result Http.Error (List User)) | |
update : Msg -> Model -> (Model, Cmd Msg) | |
update msg model = | |
case msg of | |
LoadUsers -> | |
(model, loadUsers) | |
LoadedUsers (Ok users) -> | |
(users, Cmd.none) | |
LoadedUsers (Err _) -> | |
(model, Cmd.none) | |
-- View | |
view : Model -> Html Msg | |
view model = | |
let | |
result = | |
if ((List.isEmpty model)) then | |
div [][ text "Currently no Users." ] | |
else | |
div [] (List.map userRow model) | |
in | |
div [] | |
[ h1 [] [ text "Users" ] | |
, result | |
] | |
userRow : User -> Html Msg | |
userRow user = | |
div [] [ text (user.username ++ " " ++ user.role) ] | |
decodeUser : Decode.Decoder User | |
decodeUser = | |
Decode.map3 User | |
(Decode.field "id" Decode.int) | |
(Decode.field "username" Decode.string) | |
(Decode.field "role" Decode.string) | |
decodeUserList : Decode.Decoder (List User) | |
decodeUserList = | |
Decode.list decodeUser | |
loadUsers : Cmd Msg | |
loadUsers = | |
Http.send LoadedUsers (Http.get "http://localhost:3000/users" decodeUserList) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment