Update 12 and 13

This commit is contained in:
Richard Feldman
2016-04-03 10:07:53 -07:00
parent c004be3f72
commit bb92151d61
9 changed files with 235 additions and 116 deletions

View File

@@ -1,4 +1,4 @@
module Component.ElmHub (..) where
module ElmHub (..) where
import Html exposing (..)
import Html.Attributes exposing (..)
@@ -11,7 +11,8 @@ import Effects exposing (Effects)
import Json.Decode exposing (Decoder, (:=))
import Json.Encode
import Signal exposing (Address)
import Component.SearchResult exposing (ResultId)
import Dict exposing (Dict)
import SearchResult exposing (ResultId)
searchFeed : String -> Task x Action
@@ -32,31 +33,21 @@ searchFeed query =
Task.onError task (\_ -> Task.succeed (SetResults []))
responseDecoder : Decoder (List Component.SearchResult.Model)
responseDecoder : Decoder (List SearchResult.Model)
responseDecoder =
"items" := Json.Decode.list searchResultDecoder
searchResultDecoder : Decoder Component.SearchResult.Model
searchResultDecoder =
Json.Decode.object4
Component.SearchResult.Model
("id" := Json.Decode.int)
("full_name" := Json.Decode.string)
("stargazers_count" := Json.Decode.int)
(Json.Decode.succeed True)
"items" := Json.Decode.list SearchResult.decoder
type alias Model =
{ query : String
, results : List Component.SearchResult.Model
, results : Dict SearchResult.ResultId SearchResult.Model
}
initialModel : Model
initialModel =
{ query = "tutorial"
, results = []
, results = Dict.empty
}
@@ -77,24 +68,19 @@ view address model =
]
viewSearchResults : Address Action -> List Component.SearchResult.Model -> List Html
viewSearchResults : Address Action -> Dict ResultId SearchResult.Model -> List Html
viewSearchResults address results =
results
|> filterResults
|> List.map (lazy2 viewSearchResult address)
|> Dict.values
|> List.sortBy (.stars >> negate)
|> List.map (viewSearchResult address)
filterResults : List Component.SearchResult.Model -> List Component.SearchResult.Model
filterResults results =
case results of
[] ->
[]
first :: rest ->
if first.stars > 0 then
first :: (filterResults rest)
else
filterResults rest
viewSearchResult : Address Action -> SearchResult.Model -> Html
viewSearchResult address result =
SearchResult.view
(Signal.forwardTo address (UpdateSearchResult result.id))
result
onInput address wrap =
@@ -105,18 +91,11 @@ defaultValue str =
property "defaultValue" (Json.Encode.string str)
viewSearchResult : Address Action -> Component.SearchResult.Model -> Html
viewSearchResult address result =
Component.SearchResult.view
(Signal.forwardTo address (UpdateSearchResult result.id))
(Debug.log "rendering result..." result)
type Action
= Search
| SetQuery String
| SetResults (List Component.SearchResult.Model)
| UpdateSearchResult ResultId Component.SearchResult.Action
| SetResults (List SearchResult.Model)
| UpdateSearchResult ResultId SearchResult.Action
update : Action -> Model -> ( Model, Effects Action )
@@ -130,31 +109,31 @@ update action model =
SetResults results ->
let
newModel =
{ model | results = results }
resultsById : Dict SearchResult.ResultId SearchResult.Model
resultsById =
results
|> List.map (\result -> ( result.id, result ))
|> Dict.fromList
in
( newModel, Effects.none )
( { model | results = resultsById }, Effects.none )
UpdateSearchResult id childAction ->
let
updateResult childModel =
if childModel.id == id then
let
( newChildModel, childEffects ) =
Component.SearchResult.update childAction childModel
in
( newChildModel
, Effects.map (UpdateSearchResult id) childEffects
)
else
( childModel, Effects.none )
( newResults, effects ) =
updated =
model.results
|> List.map updateResult
|> List.unzip
newModel =
{ model | results = newResults }
|> Dict.get id
|> Maybe.map (SearchResult.update childAction)
in
( newModel, Effects.batch effects )
case updated of
Nothing ->
( model, Effects.none )
Just ( newChildModel, childEffects ) ->
let
effects =
Effects.map (UpdateSearchResult id) childEffects
newResults =
Dict.insert id newChildModel model.results
in
( { model | results = newResults }, effects )

View File

@@ -1,7 +1,7 @@
module Main (..) where
import StartApp
import Component.ElmHub exposing (..)
import ElmHub exposing (..)
import Effects exposing (Effects)
import Task exposing (Task)
import Html exposing (Html)

View File

@@ -15,3 +15,7 @@ to fail; in that case, just run `elm package install` again.)
```bash
elm live Main.elm --open -- --output=elm.js
```
## References
* [Elm Architecture Tutorial](https://github.com/evancz/elm-architecture-tutorial)

View File

@@ -1,10 +1,12 @@
module Component.SearchResult (..) where
module SearchResult (..) where
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Attributes exposing (class, target, href, property)
import Html.Events exposing (..)
import Signal exposing (Address)
import Effects exposing (Effects)
import Json.Decode exposing (Decoder, (:=))
import Json.Decode.Pipeline exposing (..)
type alias Model =
@@ -24,9 +26,18 @@ type Action
| Collapse
decoder : Decoder Model
decoder =
decode Model
|> required "id" Json.Decode.int
|> required "full_name" Json.Decode.string
|> required "stargazers_count" Json.Decode.int
|> hardcoded True
update : Action -> Model -> ( Model, Effects Action )
update action model =
-- TODO make expand and collapse work
-- TODO implement Expand and Collapse logic
( model, Effects.none )

View File

@@ -4,10 +4,12 @@
"repository": "https://github.com/rtfeldman/elm-workshop.git",
"license": "BSD-3-Clause",
"source-directories": [
".", ".."
".",
".."
],
"exposed-modules": [],
"dependencies": {
"NoRedInk/elm-decode-pipeline": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "3.0.0 <= v < 4.0.0",
"evancz/elm-effects": "2.0.0 <= v < 3.0.0",
"evancz/elm-html": "4.0.0 <= v < 5.0.0",
@@ -15,4 +17,4 @@
"evancz/start-app": "2.0.0 <= v < 3.0.0"
},
"elm-version": "0.16.0 <= v < 0.17.0"
}
}