module View.Form exposing ( textInput , radioInputs ) import Html exposing (..) import Html.Attributes exposing (..) import Form exposing (Form, FieldState) import Form.Input as Input import Form.Error as FormError exposing (Error(..)) import Msg exposing (Msg) import LoggedData exposing (LoggedData) import Model.Translations as Translations exposing (Translations) import Utils.Maybe exposing (isJust) textInput : Translations -> Form String a -> (Html Form.Msg -> Html msg) -> String -> Html msg textInput translations form htmlMap fieldName = let field = Form.getFieldAsString fieldName form in div [ classList [ ("textInput", True) , ("error", isJust field.liveError) ] ] [ htmlMap <| Input.textInput field [ id fieldName , classList [ ("filled", isJust field.value) ] ] , label [ for fieldName ] [ text (Translations.getMessage fieldName translations) ] , case field.liveError of Just error -> formError translations error Nothing -> text "" ] radioInputs : Translations -> Form String a -> (Html Form.Msg -> Html msg) -> String -> List String -> Html msg radioInputs translations form htmlMap radioName fieldNames = let field = Form.getFieldAsString radioName form in div [ classList [ ("radioGroup", True) , ("error", isJust field.liveError) ] ] [ div [ class "title" ] [ text (Translations.getMessage radioName translations) ] , div [ class "radioElems" ] (List.map (radioInput translations field htmlMap) fieldNames) , case field.liveError of Just error -> formError translations error Nothing -> text "" ] radioInput : Translations -> FieldState String String -> (Html Form.Msg -> Html msg) -> String -> Html msg radioInput translations field htmlMap fieldName = label [ for fieldName ] [ htmlMap <| Input.radioInput field.path field [ id fieldName , value fieldName , checked (field.value == Just fieldName) ] , text (Translations.getMessage fieldName translations) ] formError : Translations -> FormError.Error String -> Html msg formError translations error = let errorElement error params = div [ class "errorMessage" ] [ text (Translations.getParamMessage params error translations) ] in case error of CustomError key -> errorElement key [] SmallerIntThan n -> errorElement "SmallerIntThan" [toString n] GreaterIntThan n -> errorElement "GreaterIntThan" [toString n] error -> errorElement (toString error) []