Skip to content

Commit

Permalink
It is often helpful to grab information from elsewhere on the internet.
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfBarkow committed May 24, 2023
1 parent ea1a22c commit 937e8e1
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions src/Main.elm
@@ -0,0 +1,88 @@
-- Make a GET request to load a book called "Public Opinion"
--
-- Read how it works:
-- https://guide.elm-lang.org/effects/http.html
--

import Browser
import Html exposing (Html, text, pre)
import Http



-- MAIN


main =
Browser.element
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}



-- MODEL


type Model
= Failure
| Loading
| Success String


init : () -> (Model, Cmd Msg)
init _ =
( Loading
, Http.get
{ url = "https://elm-lang.org/assets/public-opinion.txt"
, expect = Http.expectString GotText
}
)



-- UPDATE


type Msg
= GotText (Result Http.Error String)


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
GotText result ->
case result of
Ok fullText ->
(Success fullText, Cmd.none)

Err _ ->
(Failure, Cmd.none)



-- SUBSCRIPTIONS


subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none



-- VIEW


view : Model -> Html Msg
view model =
case model of
Failure ->
text "I was unable to load your book."

Loading ->
text "Loading..."

Success fullText ->
pre [] [ text fullText ]

0 comments on commit 937e8e1

Please sign in to comment.