Add Starter.elm
This commit is contained in:
116
part6/Main.elm
116
part6/Main.elm
@@ -3,34 +3,10 @@ module Main exposing (..)
|
||||
import Html exposing (..)
|
||||
import Html.Attributes exposing (class, defaultValue, href, property, target)
|
||||
import Html.Events exposing (..)
|
||||
import Json.Decode exposing (..)
|
||||
import Json.Decode.Pipeline exposing (..)
|
||||
import Json.Starter as Json exposing (Json, Outcome(..), required)
|
||||
import SampleResponse
|
||||
|
||||
|
||||
main : Program Never Model Msg
|
||||
main =
|
||||
Html.beginnerProgram
|
||||
{ view = view
|
||||
, update = update
|
||||
, model = initialModel
|
||||
}
|
||||
|
||||
|
||||
searchResultDecoder : Decoder SearchResult
|
||||
searchResultDecoder =
|
||||
-- See https://developer.github.com/v3/search/#example
|
||||
-- and http://package.elm-lang.org/packages/NoRedInk/elm-decode-pipeline/latest
|
||||
--
|
||||
-- Look in SampleResponse.elm to see the exact JSON we'll be decoding!
|
||||
--
|
||||
-- TODO replace these calls to `hardcoded` with calls to `required`
|
||||
decode SearchResult
|
||||
|> hardcoded 0
|
||||
|> hardcoded ""
|
||||
|> hardcoded 0
|
||||
|
||||
|
||||
type alias Model =
|
||||
{ query : String
|
||||
, results : List SearchResult
|
||||
@@ -38,12 +14,65 @@ type alias Model =
|
||||
|
||||
|
||||
type alias SearchResult =
|
||||
{ id : Int
|
||||
, name : String
|
||||
{ name : String
|
||||
, id : Int
|
||||
, stars : Int
|
||||
}
|
||||
|
||||
|
||||
decodeSearchResult : Json -> Result String SearchResult
|
||||
decodeSearchResult json =
|
||||
-- See https://developer.github.com/v3/search/#example
|
||||
--
|
||||
-- Look in SampleResponse.elm to see the exact JSON we'll be decoding!
|
||||
case
|
||||
Json.decodeObject json
|
||||
-- TODO insert the appropriate strings to decode the id and stars
|
||||
[ required [ "name" ]
|
||||
, required []
|
||||
, required []
|
||||
]
|
||||
of
|
||||
[ String name {- TODO match the types with the other 2 `required` fields -} ] ->
|
||||
Ok
|
||||
{ name = ""
|
||||
, id = 0
|
||||
, stars = 0
|
||||
}
|
||||
|
||||
errors ->
|
||||
Err (Json.errorsToString errors)
|
||||
|
||||
|
||||
decodeSearchResults : Json -> Result String (List SearchResult)
|
||||
decodeSearchResults json =
|
||||
case
|
||||
Json.decodeObject json
|
||||
-- TODO specify the required field for the search result items
|
||||
-- based on https://developer.github.com/v3/search/#example
|
||||
--
|
||||
-- HINT: It's a field on the outermost object, and it holds an array.
|
||||
[ required [] ]
|
||||
of
|
||||
[ List searchResultsJson ] ->
|
||||
Json.decodeAll searchResultsJson decodeSearchResult
|
||||
|
||||
errors ->
|
||||
Err (Json.errorsToString errors)
|
||||
|
||||
|
||||
decodeResults : String -> List SearchResult
|
||||
decodeResults rawJson =
|
||||
case decodeSearchResults (Json.fromString rawJson) of
|
||||
Ok searchResults ->
|
||||
searchResults
|
||||
|
||||
_ ->
|
||||
-- If it failed, we'll return no search results for now.
|
||||
-- We could improve this by displaying an error to the user!
|
||||
[]
|
||||
|
||||
|
||||
initialModel : Model
|
||||
initialModel =
|
||||
{ query = "tutorial"
|
||||
@@ -51,30 +80,6 @@ initialModel =
|
||||
}
|
||||
|
||||
|
||||
responseDecoder : Decoder (List SearchResult)
|
||||
responseDecoder =
|
||||
decode identity
|
||||
|> required "items" (list searchResultDecoder)
|
||||
|
||||
|
||||
decodeResults : String -> List SearchResult
|
||||
decodeResults json =
|
||||
case decodeString responseDecoder json of
|
||||
-- TODO add branches to this case-expression which return:
|
||||
--
|
||||
-- * the search results, if decoding succeeded
|
||||
-- * an empty list if decoding failed
|
||||
--
|
||||
-- see http://package.elm-lang.org/packages/elm-lang/core/4.0.0/Json-Decode#decodeString
|
||||
--
|
||||
-- HINT: decodeString returns a Result which is one of the following:
|
||||
--
|
||||
-- Ok (List SearchResult)
|
||||
-- Err String
|
||||
_ ->
|
||||
[]
|
||||
|
||||
|
||||
view : Model -> Html Msg
|
||||
view model =
|
||||
div [ class "content" ]
|
||||
@@ -117,3 +122,12 @@ update msg model =
|
||||
List.filter (\{ id } -> id /= idToHide) model.results
|
||||
in
|
||||
{ model | results = newResults }
|
||||
|
||||
|
||||
main : Program Never Model Msg
|
||||
main =
|
||||
Html.beginnerProgram
|
||||
{ view = view
|
||||
, update = update
|
||||
, model = initialModel
|
||||
}
|
||||
|
||||
@@ -5,11 +5,11 @@
|
||||
"license": "BSD-3-Clause",
|
||||
"source-directories": [
|
||||
".",
|
||||
".."
|
||||
"..",
|
||||
"../vendor"
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
"NoRedInk/elm-decode-pipeline": "3.0.0 <= v < 4.0.0",
|
||||
"elm-lang/core": "5.0.0 <= v < 6.0.0",
|
||||
"elm-lang/html": "2.0.0 <= v < 3.0.0",
|
||||
"elm-lang/http": "1.0.0 <= v < 2.0.0"
|
||||
|
||||
Reference in New Issue
Block a user