swap 12 and 13
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
module ElmHub (..) where
|
||||
module Component.ElmHub (..) where
|
||||
|
||||
import Html exposing (..)
|
||||
import Html.Attributes exposing (..)
|
||||
@@ -11,8 +11,7 @@ import Effects exposing (Effects)
|
||||
import Json.Decode exposing (Decoder, (:=))
|
||||
import Json.Encode
|
||||
import Signal exposing (Address)
|
||||
import Dict exposing (Dict)
|
||||
import SearchResult
|
||||
import Component.SearchResult exposing (ResultId)
|
||||
|
||||
|
||||
searchFeed : String -> Task x Action
|
||||
@@ -33,21 +32,31 @@ searchFeed query =
|
||||
Task.onError task (\_ -> Task.succeed (SetResults []))
|
||||
|
||||
|
||||
responseDecoder : Decoder (List SearchResult.Model)
|
||||
responseDecoder : Decoder (List Component.SearchResult.Model)
|
||||
responseDecoder =
|
||||
"items" := Json.Decode.list SearchResult.decoder
|
||||
"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)
|
||||
|
||||
|
||||
type alias Model =
|
||||
{ query : String
|
||||
, results : Dict SearchResult.ResultId SearchResult.Model
|
||||
, results : List Component.SearchResult.Model
|
||||
}
|
||||
|
||||
|
||||
initialModel : Model
|
||||
initialModel =
|
||||
{ query = "tutorial"
|
||||
, results = Dict.empty
|
||||
, results = []
|
||||
}
|
||||
|
||||
|
||||
@@ -68,24 +77,22 @@ view address model =
|
||||
]
|
||||
|
||||
|
||||
viewSearchResults : Address Action -> Dict SearchResult.ResultId SearchResult.Model -> List Html
|
||||
viewSearchResults : Address Action -> List Component.SearchResult.Model -> List Html
|
||||
viewSearchResults address results =
|
||||
results
|
||||
|> Dict.values
|
||||
|> List.sortBy (.stars >> negate)
|
||||
|> filterResults
|
||||
|> List.map (lazy3 SearchResult.view address DeleteById)
|
||||
|> List.map (lazy2 viewSearchResult address)
|
||||
|
||||
|
||||
filterResults : List SearchResult.Model -> List SearchResult.Model
|
||||
filterResults : List Component.SearchResult.Model -> List Component.SearchResult.Model
|
||||
filterResults results =
|
||||
case results of
|
||||
[] ->
|
||||
[]
|
||||
|
||||
result :: rest ->
|
||||
if result.stars > 0 then
|
||||
result :: (filterResults rest)
|
||||
first :: rest ->
|
||||
if first.stars > 0 then
|
||||
first :: (filterResults rest)
|
||||
else
|
||||
filterResults rest
|
||||
|
||||
@@ -98,11 +105,18 @@ 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
|
||||
| DeleteById SearchResult.ResultId
|
||||
| SetResults (List SearchResult.Model)
|
||||
| SetResults (List Component.SearchResult.Model)
|
||||
| UpdateSearchResult ResultId Component.SearchResult.Action
|
||||
|
||||
|
||||
update : Action -> Model -> ( Model, Effects Action )
|
||||
@@ -115,18 +129,32 @@ update action model =
|
||||
( { model | query = query }, Effects.none )
|
||||
|
||||
SetResults results ->
|
||||
let
|
||||
resultsById : Dict SearchResult.ResultId SearchResult.Model
|
||||
resultsById =
|
||||
results
|
||||
|> List.map (\result -> ( result.id, result ))
|
||||
|> Dict.fromList
|
||||
in
|
||||
( { model | results = resultsById }, Effects.none )
|
||||
|
||||
DeleteById id ->
|
||||
let
|
||||
newModel =
|
||||
{ model | results = Dict.remove id model.results }
|
||||
{ model | results = results }
|
||||
in
|
||||
( newModel, 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 ) =
|
||||
model.results
|
||||
|> List.map updateResult
|
||||
|> List.unzip
|
||||
|
||||
newModel =
|
||||
{ model | results = newResults }
|
||||
in
|
||||
( newModel, Effects.batch effects )
|
||||
52
part12/Component/SearchResult.elm
Normal file
52
part12/Component/SearchResult.elm
Normal file
@@ -0,0 +1,52 @@
|
||||
module Component.SearchResult (..) where
|
||||
|
||||
import Html exposing (..)
|
||||
import Html.Attributes exposing (..)
|
||||
import Html.Events exposing (..)
|
||||
import Signal exposing (Address)
|
||||
import Effects exposing (Effects)
|
||||
|
||||
|
||||
type alias Model =
|
||||
{ id : Int
|
||||
, name : String
|
||||
, stars : Int
|
||||
, expanded : Bool
|
||||
}
|
||||
|
||||
|
||||
type alias ResultId =
|
||||
Int
|
||||
|
||||
|
||||
type Action
|
||||
= Expand
|
||||
| Collapse
|
||||
|
||||
|
||||
update : Action -> Model -> ( Model, Effects Action )
|
||||
update action model =
|
||||
-- TODO make expand and collapse work
|
||||
( model, Effects.none )
|
||||
|
||||
|
||||
view : Address Action -> Model -> Html
|
||||
view address model =
|
||||
li
|
||||
[]
|
||||
<| if model.expanded then
|
||||
[ span [ class "star-count" ] [ text (toString model.stars) ]
|
||||
, a
|
||||
[ href ("https://github.com/" ++ model.name), target "_blank" ]
|
||||
[ text model.name ]
|
||||
, button
|
||||
-- TODO when the user clicks, send a Collapse action
|
||||
[ class "hide-result" ]
|
||||
[ text "X" ]
|
||||
]
|
||||
else
|
||||
[ button
|
||||
-- TODO when the user clicks, send an Expand action
|
||||
[ class "expand-result" ]
|
||||
[ text "Show" ]
|
||||
]
|
||||
@@ -1,14 +0,0 @@
|
||||
module ElmHub.Css (..) where
|
||||
|
||||
import Css exposing (..)
|
||||
|
||||
|
||||
css =
|
||||
stylesheet
|
||||
[ ((.) "content")
|
||||
[ width (px 960)
|
||||
, margin2 zero auto
|
||||
, padding (px 30)
|
||||
, fontFamilies [ "Helvetica", "Arial", "serif" ]
|
||||
]
|
||||
]
|
||||
@@ -1,7 +1,7 @@
|
||||
module Main (..) where
|
||||
|
||||
import StartApp
|
||||
import ElmHub exposing (..)
|
||||
import Component.ElmHub exposing (..)
|
||||
import Effects exposing (Effects)
|
||||
import Task exposing (Task)
|
||||
import Html exposing (Html)
|
||||
|
||||
@@ -15,9 +15,3 @@ to fail; in that case, just run `elm package install` again.)
|
||||
```bash
|
||||
elm live Main.elm --open -- --output=elm.js
|
||||
```
|
||||
|
||||
## Compiling CSS
|
||||
|
||||
```bash
|
||||
elm css Stylesheets.elm
|
||||
```
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
module SearchResult (..) where
|
||||
|
||||
import Html exposing (..)
|
||||
import Html.Attributes exposing (..)
|
||||
import Html.Events exposing (..)
|
||||
import Json.Decode exposing (Decoder, (:=))
|
||||
import Signal exposing (Address)
|
||||
import Dict exposing (Dict)
|
||||
|
||||
|
||||
type alias ResultId =
|
||||
Int
|
||||
|
||||
|
||||
type alias Model =
|
||||
{ id : ResultId
|
||||
, name : String
|
||||
, stars : Int
|
||||
}
|
||||
|
||||
|
||||
decoder : Decoder Model
|
||||
decoder =
|
||||
Json.Decode.object3
|
||||
Model
|
||||
("id" := Json.Decode.int)
|
||||
("full_name" := Json.Decode.string)
|
||||
("stargazers_count" := Json.Decode.int)
|
||||
|
||||
|
||||
view : Address a -> (Int -> a) -> Model -> Html
|
||||
view address delete result =
|
||||
li
|
||||
[]
|
||||
[ span [ class "star-count" ] [ text (toString result.stars) ]
|
||||
, a
|
||||
[ href ("https://github.com/" ++ result.name)
|
||||
, target "_blank"
|
||||
]
|
||||
[ text result.name ]
|
||||
, button
|
||||
[ class "hide-result", onClick address (delete result.id) ]
|
||||
[ text "X" ]
|
||||
]
|
||||
@@ -1,10 +0,0 @@
|
||||
module Stylesheets (..) where
|
||||
|
||||
import Css.File exposing (..)
|
||||
import ElmHub.Css
|
||||
|
||||
|
||||
port files : CssFileStructure
|
||||
port files =
|
||||
toFileStructure
|
||||
[ ( "style.css", compile ElmHub.Css.css ) ]
|
||||
@@ -4,17 +4,14 @@
|
||||
"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",
|
||||
"evancz/elm-http": "3.0.0 <= v < 4.0.0",
|
||||
"rtfeldman/elm-css": "1.0.0 <= v < 2.0.0",
|
||||
"evancz/start-app": "2.0.0 <= v < 3.0.0"
|
||||
},
|
||||
"elm-version": "0.16.0 <= v < 0.17.0"
|
||||
|
||||
@@ -90,12 +90,3 @@ a:hover {
|
||||
button:focus, input:focus {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.error {
|
||||
background-color: #FF9632;
|
||||
padding: 20px;
|
||||
box-sizing: border-box;
|
||||
overflow-x: auto;
|
||||
font-family: monospace;
|
||||
font-size: 18px;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user