From c85bb78f6c9190f4aca6d24aba0f13cf6fa95cb6 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sun, 15 Nov 2020 16:08:41 -0500 Subject: [PATCH] make HTTP requests --- backend/templates/app.html | 2 +- sina/src/Main.elm | 52 +++++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/backend/templates/app.html b/backend/templates/app.html index 04cf57a..45b25f4 100644 --- a/backend/templates/app.html +++ b/backend/templates/app.html @@ -7,7 +7,6 @@ -
@@ -18,6 +17,7 @@ node: document.getElementById("app") }); +
diff --git a/sina/src/Main.elm b/sina/src/Main.elm index 7edeb90..eb17c73 100644 --- a/sina/src/Main.elm +++ b/sina/src/Main.elm @@ -1,40 +1,68 @@ module Main exposing (..) import Browser -import Html exposing (Html, div, h1, img, text) +import Html exposing (Html, div, h1, img, p, pre, text) import Html.Attributes exposing (src) +import Http +import Mi -type alias Model = - {} +type Model + = Failure String + | Loading + | Success String -init : ( Model, Cmd Msg ) -init = - ( {}, Cmd.none ) +init : () -> ( Model, Cmd Msg ) +init _ = + ( Loading + , Http.get + { url = "/.within/botinfo" + , expect = Http.expectString GotText + } + ) type Msg - = NoOp + = GotText (Result Http.Error String) update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = - ( model, Cmd.none ) + case msg of + GotText result -> + case result of + Ok fullText -> + ( Success fullText, Cmd.none ) + + Err why -> + ( Failure (Mi.errorToString why), Cmd.none ) view : Model -> Html Msg view model = - div [] - [ h1 [] [ text "Your Elm App is working!" ] - ] + case model of + Failure why -> + div [] + [ h1 [] [ text "Error" ] + , p [] [ text why ] + ] + + Loading -> + div [] [ h1 [] [ text "Loading" ] ] + + Success msg -> + div [] + [ h1 [] [ text "Mi" ] + , pre [] [ text msg ] + ] main : Program () Model Msg main = Browser.element { view = view - , init = \_ -> init + , init = init , update = update , subscriptions = always Sub.none }