Break up parts 5 and 6 a bit.

This commit is contained in:
Richard Feldman
2016-04-02 16:14:04 -07:00
parent fa7dc4ca91
commit 08abf49174

View File

@@ -1,10 +1,10 @@
module Main (..) where module Main (..) where
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (..) import Html.Attributes exposing (class, target, href, property)
import Html.Events exposing (..) import Html.Events exposing (..)
import Auth import Auth
import StartApp import StartApp.Simple as StartApp
import Http import Http
import Task exposing (Task) import Task exposing (Task)
import Effects exposing (Effects) import Effects exposing (Effects)
@@ -14,38 +14,56 @@ import Json.Encode
import Signal exposing (Address) import Signal exposing (Address)
main : Signal Html
main = main =
app.html
app : StartApp.App Model
app =
StartApp.start StartApp.start
{ view = view { view = view
, update = update , update = update
, init = ( initialModel, Effects.task (searchFeed initialModel.query) ) , model = initialModel
, inputs = []
} }
port tasks : Signal (Task Effects.Never ()) sampleJson : String
port tasks = sampleJson =
app.tasks """
{
"total_count": 40,
searchFeed : String -> Task x Action "incomplete_results": false,
searchFeed query = "items": [
let {
-- See https://developer.github.com/v3/search/#example for how to customize! "id": 3081286,
url = "name": "Tetris",
"https://api.github.com/search/repositories?access_token=" "full_name": "dtrupenn/Tetris",
++ Auth.token "owner": {
++ "&q=" "login": "dtrupenn",
++ query "id": 872147,
++ "+language:elm&sort=stars&order=desc" "avatar_url": "https://secure.gravatar.com/avatar/e7956084e75f239de85d3a31bc172ace?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png",
in "gravatar_id": "",
performAction SetResults (\_ -> SetResults []) (Http.get responseDecoder url) "url": "https://api.github.com/users/dtrupenn",
"received_events_url": "https://api.github.com/users/dtrupenn/received_events",
"type": "User"
},
"private": false,
"html_url": "https://github.com/dtrupenn/Tetris",
"description": "A C implementation of Tetris using Pennsim through LC4",
"fork": false,
"url": "https://api.github.com/repos/dtrupenn/Tetris",
"created_at": "2012-01-01T00:31:50Z",
"updated_at": "2013-01-05T17:58:47Z",
"pushed_at": "2012-01-01T00:37:02Z",
"homepage": "",
"size": 524,
"stargazers_count": 1,
"watchers_count": 1,
"language": "Assembly",
"forks_count": 0,
"open_issues_count": 0,
"master_branch": "master",
"default_branch": "master",
"score": 10.309712
}
]
}
"""
responseDecoder : Decoder (List SearchResult) responseDecoder : Decoder (List SearchResult)
@@ -63,15 +81,6 @@ searchResultDecoder =
|> hardcoded 0 |> hardcoded 0
performAction : (a -> b) -> (y -> b) -> Task y a -> Task x b
performAction successToAction errorToAction task =
let
successTask =
Task.map successToAction task
in
Task.onError successTask (\err -> Task.succeed (errorToAction err))
type alias Model = type alias Model =
{ query : String { query : String
, results : List SearchResult , results : List SearchResult
@@ -92,10 +101,20 @@ type alias ResultId =
initialModel : Model initialModel : Model
initialModel = initialModel =
{ query = "tutorial" { query = "tutorial"
, results = [] , results = decodeResults sampleJson
} }
decodeResults : String -> List SearchResult
decodeResults json =
case Json.Decode.decodeString responseDecoder json of
Ok results ->
results
Err err ->
[]
view : Address Action -> Model -> Html view : Address Action -> Model -> Html
view address model = view address model =
div div
@@ -106,7 +125,7 @@ view address model =
, span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ] , span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ]
] ]
, input [ class "search-query", onInput address SetQuery, defaultValue model.query ] [] , input [ class "search-query", onInput address SetQuery, defaultValue model.query ] []
, button [ class "search-button" {- TODO on click, run a search -} ] [ text "Search" ] , button [ class "search-button" ] [ text "Search" ]
, ul , ul
[ class "results" ] [ class "results" ]
(List.map (viewSearchResult address) model.results) (List.map (viewSearchResult address) model.results)
@@ -136,34 +155,27 @@ viewSearchResult address result =
type Action type Action
= Search = SetQuery String
| SetQuery String
| DeleteById ResultId | DeleteById ResultId
| SetResults (List SearchResult) | SetResults (List SearchResult)
update : Action -> Model -> ( Model, Effects Action ) update : Action -> Model -> Model
update action model = update action model =
case action of case action of
Search ->
( model, Effects.none {- TODO use searchFeed to run a search -} )
SetQuery query -> SetQuery query ->
( { model | query = query }, Effects.none ) { model | query = query }
SetResults results -> SetResults results ->
let let
newModel = newModel =
{ model | results = results } { model | results = results }
in in
( newModel, Effects.none ) newModel
DeleteById idToHide -> DeleteById idToHide ->
let let
newResults = newResults =
List.filter (\{ id } -> id /= idToHide) model.results List.filter (\{ id } -> id /= idToHide) model.results
newModel =
{ model | results = newResults }
in in
( newModel, Effects.none ) { model | results = newResults }