Update 12 and 13
This commit is contained in:
@@ -12,7 +12,7 @@ import Json.Decode exposing (Decoder, (:=))
|
||||
import Json.Encode
|
||||
import Signal exposing (Address)
|
||||
import Dict exposing (Dict)
|
||||
import SearchResult
|
||||
import SearchResult exposing (ResultId)
|
||||
|
||||
|
||||
searchFeed : String -> Task x Action
|
||||
@@ -68,26 +68,19 @@ view address model =
|
||||
]
|
||||
|
||||
|
||||
viewSearchResults : Address Action -> Dict SearchResult.ResultId SearchResult.Model -> List Html
|
||||
viewSearchResults : Address Action -> Dict ResultId SearchResult.Model -> List Html
|
||||
viewSearchResults address results =
|
||||
results
|
||||
|> Dict.values
|
||||
|> List.sortBy (.stars >> negate)
|
||||
|> filterResults
|
||||
|> List.map (lazy3 SearchResult.view address DeleteById)
|
||||
|> List.map (viewSearchResult address)
|
||||
|
||||
|
||||
filterResults : List SearchResult.Model -> List SearchResult.Model
|
||||
filterResults results =
|
||||
case results of
|
||||
[] ->
|
||||
[]
|
||||
|
||||
result :: rest ->
|
||||
if result.stars > 0 then
|
||||
result :: (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 =
|
||||
@@ -101,8 +94,8 @@ defaultValue str =
|
||||
type Action
|
||||
= Search
|
||||
| SetQuery String
|
||||
| DeleteById SearchResult.ResultId
|
||||
| SetResults (List SearchResult.Model)
|
||||
| UpdateSearchResult ResultId SearchResult.Action
|
||||
|
||||
|
||||
update : Action -> Model -> ( Model, Effects Action )
|
||||
@@ -124,9 +117,23 @@ update action model =
|
||||
in
|
||||
( { model | results = resultsById }, Effects.none )
|
||||
|
||||
DeleteById id ->
|
||||
UpdateSearchResult id childAction ->
|
||||
let
|
||||
newModel =
|
||||
{ model | results = Dict.remove id model.results }
|
||||
updated =
|
||||
model.results
|
||||
|> Dict.get id
|
||||
|> Maybe.map (SearchResult.update childAction)
|
||||
in
|
||||
( newModel, Effects.none )
|
||||
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 )
|
||||
|
||||
@@ -1,44 +1,65 @@
|
||||
module SearchResult (..) where
|
||||
|
||||
import Html exposing (..)
|
||||
import Html.Attributes exposing (..)
|
||||
import Html.Attributes exposing (class, target, href, property)
|
||||
import Html.Events exposing (..)
|
||||
import Json.Decode exposing (Decoder, (:=))
|
||||
import Signal exposing (Address)
|
||||
import Dict exposing (Dict)
|
||||
import Effects exposing (Effects)
|
||||
import Json.Decode exposing (Decoder, (:=))
|
||||
import Json.Decode.Pipeline exposing (..)
|
||||
|
||||
|
||||
type alias Model =
|
||||
{ id : Int
|
||||
, name : String
|
||||
, stars : Int
|
||||
, expanded : Bool
|
||||
}
|
||||
|
||||
|
||||
type alias ResultId =
|
||||
Int
|
||||
|
||||
|
||||
type alias Model =
|
||||
{ id : ResultId
|
||||
, name : String
|
||||
, stars : Int
|
||||
}
|
||||
type Action
|
||||
= Expand
|
||||
| Collapse
|
||||
|
||||
|
||||
decoder : Decoder Model
|
||||
decoder =
|
||||
Json.Decode.object3
|
||||
Model
|
||||
("id" := Json.Decode.int)
|
||||
("full_name" := Json.Decode.string)
|
||||
("stargazers_count" := Json.Decode.int)
|
||||
decode Model
|
||||
|> required "id" Json.Decode.int
|
||||
|> required "full_name" Json.Decode.string
|
||||
|> required "stargazers_count" Json.Decode.int
|
||||
|> hardcoded True
|
||||
|
||||
|
||||
view : Address a -> (Int -> a) -> Model -> Html
|
||||
view address delete result =
|
||||
update : Action -> Model -> ( Model, Effects Action )
|
||||
update action model =
|
||||
case action of
|
||||
Expand ->
|
||||
( { model | expanded = True }, Effects.none )
|
||||
|
||||
Collapse ->
|
||||
( { model | expanded = False }, Effects.none )
|
||||
|
||||
|
||||
view : Address Action -> Model -> Html
|
||||
view address model =
|
||||
li
|
||||
[]
|
||||
[ span [ class "star-count" ] [ text (toString result.stars) ]
|
||||
, a
|
||||
[ href ("https://github.com/" ++ result.name)
|
||||
, target "_blank"
|
||||
<| 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
|
||||
[ class "hide-result", onClick address Collapse ]
|
||||
[ text "X" ]
|
||||
]
|
||||
else
|
||||
[ button
|
||||
[ class "expand-result", onClick address Expand ]
|
||||
[ text "Show" ]
|
||||
]
|
||||
[ text result.name ]
|
||||
, button
|
||||
[ class "hide-result", onClick address (delete result.id) ]
|
||||
[ text "X" ]
|
||||
]
|
||||
|
||||
103
part13/style.css
103
part13/style.css
@@ -1,6 +1,101 @@
|
||||
|
||||
.content {
|
||||
width: 960px;
|
||||
margin: 0 auto;
|
||||
padding: 30px;
|
||||
font-family: Helvetica, Arial, serif;
|
||||
width: 960px;
|
||||
margin: 0 auto;
|
||||
padding: 30px;
|
||||
font-family: Helvetica, Arial, serif;
|
||||
}
|
||||
|
||||
header {
|
||||
position: relative;
|
||||
padding: 6px 12px;
|
||||
height: 36px;
|
||||
background-color: rgb(96, 181, 204);
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: white;
|
||||
font-weight: normal;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.tagline {
|
||||
color: #eee;
|
||||
position: absolute;
|
||||
right: 16px;
|
||||
top: 12px;
|
||||
font-size: 24px;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.results {
|
||||
list-style-image: url('http://img-cache.cdn.gaiaonline.com/76bd5c99d8f2236e9d3672510e933fdf/http://i278.photobucket.com/albums/kk81/d3m3nt3dpr3p/Tiny-Star-Icon.png');
|
||||
list-style-position: inside;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.results li {
|
||||
font-size: 18px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.star-count {
|
||||
font-weight: bold;
|
||||
margin-right: 16px;
|
||||
}
|
||||
|
||||
a {
|
||||
color: rgb(96, 181, 204);
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.search-query {
|
||||
padding: 8px;
|
||||
font-size: 24px;
|
||||
margin-bottom: 18px;
|
||||
margin-top: 36px;
|
||||
}
|
||||
|
||||
.search-button {
|
||||
padding: 8px 16px;
|
||||
font-size: 24px;
|
||||
color: white;
|
||||
border: 1px solid #ccc;
|
||||
background-color: rgb(96, 181, 204);
|
||||
margin-left: 12px
|
||||
}
|
||||
|
||||
.search-button:hover {
|
||||
color: rgb(96, 181, 204);
|
||||
background-color: white;
|
||||
}
|
||||
|
||||
.hide-result {
|
||||
background-color: transparent;
|
||||
border: 0;
|
||||
font-weight: bold;
|
||||
font-size: 18px;
|
||||
margin-left: 18px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.hide-result:hover {
|
||||
color: rgb(96, 181, 204);
|
||||
}
|
||||
|
||||
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