From 0edb08fb3093409ac9b0a80c864d6edd668aec70 Mon Sep 17 00:00:00 2001 From: stuart nelson Date: Sun, 11 Dec 2016 19:57:58 +0100 Subject: [PATCH] Remove Maybe on Decode for blocks --- src/Api.elm | 18 +++++++++++++++++- src/Types.elm | 2 +- src/Views.elm | 20 ++++---------------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/Api.elm b/src/Api.elm index 674e9f4d..bc0c62b6 100644 --- a/src/Api.elm +++ b/src/Api.elm @@ -61,10 +61,26 @@ alertGroupsDecoder = alertGroupDecoder : Json.Decoder AlertGroup alertGroupDecoder = Json.map2 AlertGroup - (Json.at [ "blocks" ] <| Json.maybe <| Json.list blockDecoder) + (decodeBlocks) (Json.at [ "labels" ] (Json.keyValuePairs Json.string)) +decodeBlocks : Json.Decoder (List Block) +decodeBlocks = + Json.maybe (Json.at [ "blocks" ] (Json.list blockDecoder)) + |> andThen (unwrapWithDefault []) + + +unwrapWithDefault : a -> Maybe a -> Json.Decoder a +unwrapWithDefault default val = + case val of + Just a -> + Json.succeed a + + Nothing -> + Json.succeed default + + blockDecoder : Json.Decoder Block blockDecoder = Json.map Block diff --git a/src/Types.elm b/src/Types.elm index 8050a18a..a90d764c 100644 --- a/src/Types.elm +++ b/src/Types.elm @@ -34,7 +34,7 @@ type alias Silence = type alias AlertGroup = - { blocks : Maybe (List Block) + { blocks : List Block , labels : List ( String, String ) } diff --git a/src/Views.elm b/src/Views.elm index a616a230..903c19ca 100644 --- a/src/Views.elm +++ b/src/Views.elm @@ -63,22 +63,10 @@ silenceFormView kind silence = alertGroupsView : AlertGroup -> Html Msg alertGroupsView alertGroup = - let - blocks = - case alertGroup.blocks of - Just blocks -> - blocks - - Nothing -> - [] - - pairs = - List.map2 (,) alertGroup.labels blocks - in - li [ class "pa3 pa4-ns bb b--black-10" ] - [ div [] (List.map labelHeader alertGroup.labels) - , div [] (List.map blockView blocks) - ] + li [ class "pa3 pa4-ns bb b--black-10" ] + [ div [] (List.map labelHeader alertGroup.labels) + , div [] (List.map blockView alertGroup.blocks) + ] blockView : Block -> Html msg