Skip to content

Commit

Permalink
temp result without fruits list, but string from http
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfarseer committed Feb 16, 2020
1 parent 291e5bf commit 6347dde
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
5 changes: 4 additions & 1 deletion elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
"direct": {
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0"
"elm/html": "1.0.0",
"elm/http": "2.0.0"
},
"indirect": {
"elm/bytes": "1.0.8",
"elm/file": "1.0.5",
"elm/json": "1.1.3",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
Expand Down
42 changes: 31 additions & 11 deletions src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Main exposing (main)

import Browser
import Html exposing (Html, div, h1, li, text, ul)
import Http


type alias Fruit =
Expand All @@ -10,30 +11,48 @@ type alias Fruit =
}


type alias Flags =
List Fruit
type FruitsRequest
= Loading
| Failure
| Success


type alias Model =
{ fruits : List Fruit }
{ fruits : FruitsRequest }


init : Flags -> ( Model, Cmd msg )
init flags =
( { fruits = flags }, Cmd.none )
init : () -> ( Model, Cmd Msg )
init _ =
( { fruits = Loading }
, Http.get
{ url = "http://my-json-server.typicode.com/maxfarseer/elm-webinar-2/fruits"
, expect = Http.expectString GotFruits
}
)


type Msg
= GotFruits (Result Http.Error String)


update : msg -> Model -> ( Model, Cmd msg )
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
( model, Cmd.none )
case msg of
GotFruits response ->
case response of
Ok fruits ->
( { model | fruits = Success }, Cmd.none )

Err _ ->
( { model | fruits = Failure }, Cmd.none )


renderItem : Fruit -> Html msg
renderItem fruit =
li [] [ text (fruit.emoji ++ " " ++ fruit.name) ]


renderFruits : List Fruit -> Html msg
renderFruits : List Fruit -> Html Msg
renderFruits data =
let
list =
Expand All @@ -47,11 +66,12 @@ view model =
div []
[ h1 []
[ text "Сезон фруктов!" ]
, renderFruits model.fruits

--, renderFruits model.fruits
]


main : Program Flags Model msg
main : Program () Model Msg
main =
Browser.element
{ init = init
Expand Down
9 changes: 0 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { Elm } from './Main.elm'

const fruits = [
{ "name": "Яблоко", "emoji": "🍏" },
{ "name": "Груша", "emoji": "🍐" },
{ "name": "Банан", "emoji": "🍌" },
{ "name": "Апельсин", "emoji": "🍊" },
{ "name": "Лимон", "emoji": "🍋" }
]

Elm.Main.init({
flags: fruits,
node: document.querySelector('main')
})

0 comments on commit 6347dde

Please sign in to comment.