diff --git a/src/QuizEditor/Editor.elm b/src/QuizEditor/Editor.elm index b9f9dbd734583b3deca3f51c2a7533a0c77153ae..4d4718a2885332c6c8c941ad9e494c44de0b505a 100644 --- a/src/QuizEditor/Editor.elm +++ b/src/QuizEditor/Editor.elm @@ -1 +1,67 @@ -module QuizEditor.Editor exposing (..) \ No newline at end of file +module QuizEditor.Editor exposing (main) + +import Browser +import Html exposing (Html, div, text) +import Json.Decode as Decode exposing (Decoder, field) + + +type alias ExampleType = + { str : String + , int : Int + } + + +exampleTypeDecoder : Decoder ExampleType +exampleTypeDecoder = + Decode.map2 ExampleType + ( field "str" Decode.string ) + ( field "int" Decode.int ) + + +main = + Browser.element + { init = \flags -> init flags + , update = update + , subscriptions = \_ -> Sub.none + , view = view + } + + +type alias Model = + { result : Result Decode.Error ExampleType } + + +init : Decode.Value -> ( Model, Cmd Msg ) +init flags = + let + result = + Decode.decodeValue exampleTypeDecoder flags + in + ( Model result, Cmd.none ) + + +type Msg + = NoOp + + +update : Msg -> Model -> ( Model, Cmd Msg ) +update msg model = + ( model, Cmd.none ) + + +view : Model -> Html Msg +view model = + div [] + [ viewResult model.result ] + + +viewResult : Result Decode.Error ExampleType -> Html Msg +viewResult result = + case result of + Ok example -> + div [] + [ text ("Valid JSON! str: " ++ example.str ++ ", int: " ++ String.fromInt example.int) ] + + Err error -> + div [] + [ text ("Invalid JSON: " ++ Decode.errorToString error) ]