Last active
October 17, 2023 08:56
-
-
Save joshhornby/50fef6aa18d8d908ddab5574f133f27e to your computer and use it in GitHub Desktop.
JSON decoding over ports in Elm.
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
port module App exposing (..) | |
import Html exposing (..) | |
import Json.Decode.Pipeline exposing (..) | |
import Json.Decode exposing (..) | |
port workerUpdated : (Json.Decode.Value -> msg) -> Sub msg | |
type alias Model = | |
{ items : List Worker } | |
type alias Worker = | |
{ workerActivityName : String | |
, sid : String | |
, workerName : String | |
, workerActivityName : String | |
, department : List String | |
} | |
decodeWorkers : Json.Decode.Decoder Model | |
decodeWorkers = | |
Json.Decode.Pipeline.decode Model | |
|> Json.Decode.Pipeline.required "items" (Json.Decode.list decodeWorker) | |
decodeWorker : Json.Decode.Decoder Worker | |
decodeWorker = | |
Json.Decode.Pipeline.decode Worker | |
|> Json.Decode.Pipeline.requiredAt [ "value", "WorkerActivityName" ] (Json.Decode.string) | |
|> Json.Decode.Pipeline.requiredAt [ "value", "Sid" ] (Json.Decode.string) | |
|> Json.Decode.Pipeline.requiredAt [ "value", "WorkerName" ] (Json.Decode.string) | |
|> Json.Decode.Pipeline.requiredAt [ "value", "WorkerActivityName" ] (Json.Decode.string) | |
|> Json.Decode.Pipeline.custom | |
(Json.Decode.at [ "value", "WorkerAttributes", "department" ] (Json.Decode.list Json.Decode.string)) | |
init : ( Model, Cmd Msg ) | |
init = | |
( { items = [] }, Cmd.none ) | |
type Msg | |
= NoOp | |
| SetModel Model | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
case msg of | |
SetModel newModel -> | |
( newModel | |
, Cmd.none | |
) | |
NoOp -> | |
( model | |
, Cmd.none | |
) | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ viewWorkers model.items | |
] | |
viewWorkers : List Worker -> Html Msg | |
viewWorkers items = | |
items | |
|> List.sortBy .department | |
|> List.map viewItem | |
|> ul [] | |
viewItem : Worker -> Html Msg | |
viewItem item = | |
li [] [ text (item.workerName ++ " - " ++ item.workerActivityName) ] | |
mapWorkerUpdated : Json.Decode.Value -> Msg | |
mapWorkerUpdated modelJson = | |
case (decodeModel modelJson) of | |
Ok model -> | |
SetModel model | |
Err errorMessage -> | |
let | |
_ = | |
Debug.log "Error in mapWorkerUpdated:" errorMessage | |
in | |
NoOp | |
decodeModel : Json.Decode.Value -> Result String Model | |
decodeModel modelJson = | |
Json.Decode.decodeValue decodeWorkers modelJson | |
subscriptions : Model -> Sub Msg | |
subscriptions model = | |
workerUpdated mapWorkerUpdated |
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
{ | |
"items":[ | |
{ | |
"key":"uiory7943hiufnljddf", | |
"value":{ | |
"WorkerActivityName":"Foo", | |
"EventType":"worker.activity.update", | |
"ResourceType":"worker", | |
"WorkerPreviousActivitySid":"djkhf98384UJKDF", | |
"WorkerTimeInPreviousActivity":"922", | |
"Timestamp":"1480346909", | |
"WorkerActivitySid":"WJKDUDF89D7FYDFYG", | |
"AccountSid":"T8403UJKDFNKDBDFNMDFMN", | |
"WorkerName":"Elm Noob", | |
"Sid":"DFBJDF8334343", | |
"WorkerSid":"DFJIODFND38YUH3IR", | |
"WorkspaceSid":"TUIPJDFNNBMFNMFJHK", | |
"WorkspaceName":"Josh Hornby", | |
"EventDescription":"Trying Elm!", | |
"ResourceSid":"9034ndfdfdfdf", | |
"WorkerAttributes":{ | |
"department":[ | |
"foo" | |
] | |
} | |
} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment