From 7f2c967d3d7114972069f9efd63749859c3c19bf Mon Sep 17 00:00:00 2001 From: aqquq <jannik.wurche@student.uni-halle.de> Date: Fri, 5 Jul 2024 18:21:54 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9F=A2=20update=20to=20a=20working=20webs?= =?UTF-8?q?ite?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/QuizEditor/Editor.elm | 68 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/src/QuizEditor/Editor.elm b/src/QuizEditor/Editor.elm index b9f9dbd..4d4718a 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) ] -- GitLab