View code refactor while using ADTs

I wrote this code but I think it is not an idiomatic one. Could any one help me refactor it to a nicer one?
thanks.

view_currentEvent : Event -> Html Msg
view_currentEvent event =
    case event of
        Aggregate agg ->
            fieldset []
                [ input [ checked True, id "aggregation", name "event", type_ "radio" ] []
                , label [ for "aggregation" ] [ text "Aggregation" ]
                , input [ id "transection", name "event", type_ "radio" ] []
                , label [ for "transection" ] [ text "Transection" ]
                ]

        Transection tran ->
            fieldset []
                [ input [ id "aggregation", name "event", type_ "radio" ] []
                , label [ for "aggregation" ] [ text "Aggregation" ]
                , input [ checked True, id "transection", name "event", type_ "radio" ] []
                , label [ for "transection" ] [ text "Transection" ]
                ]

Maybe something like this:

toEventField : Event -> EventField
toEventField event =
    case event of
        Aggregate _ ->
            AggregateField

        Transection _ ->
            TransectionField


viewCurrentEvent : Event -> Html Msg
viewCurrentEvent event =
    let
        eventField =
            toEventField event
    in
    fieldset []
        [ radio "Aggregation" (SwitchTo AggregateField) (eventField == AggregateField)
        , radio "Transection" (SwitchTo TransectionField) (eventField == TransectionField)
        ]


radio : String -> msg -> Bool -> Html msg
radio value msg isChecked =
    label []
        [ input [ type_ "radio", name "event", onClick msg, checked isChecked ] []
        , text value
        ]

It is puzzling that you have payload on a custom type that you use to switch only based on the tag. This is indicative that you might have to rethink your custom types.

If you somehow refactor that out, then it simply becomes:


viewCurrentEvent : EventField -> Html Msg
viewCurrentEvent event =
    fieldset []
        [ radio "Aggregation" (SwitchTo AggregateField) (eventField == AggregateField)
        , radio "Transection" (SwitchTo TransectionField) (eventField == TransectionField)
        ]


radio : String -> msg -> Bool -> Html msg
radio value msg isChecked =
    label []
        [ input [ type_ "radio", name "event", onClick msg, checked isChecked ] []
        , text value
        ]

Also, as a side note, idiomatic Elm does not mixes camelCase with snake_case.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.