Add sortable table to part11
This commit is contained in:
@@ -7,6 +7,7 @@ import Auth
|
|||||||
import Json.Decode exposing (Decoder)
|
import Json.Decode exposing (Decoder)
|
||||||
import Json.Decode.Pipeline exposing (decode, required)
|
import Json.Decode.Pipeline exposing (decode, required)
|
||||||
import Navigation
|
import Navigation
|
||||||
|
import Table
|
||||||
|
|
||||||
|
|
||||||
type alias SearchResult =
|
type alias SearchResult =
|
||||||
@@ -43,6 +44,7 @@ type alias Model =
|
|||||||
{ query : String
|
{ query : String
|
||||||
, results : List SearchResult
|
, results : List SearchResult
|
||||||
, errorMessage : Maybe String
|
, errorMessage : Maybe String
|
||||||
|
, tableState : Table.State
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -56,6 +58,7 @@ init =
|
|||||||
( { query = initialQuery
|
( { query = initialQuery
|
||||||
, results = []
|
, results = []
|
||||||
, errorMessage = Nothing
|
, errorMessage = Nothing
|
||||||
|
, tableState = Table.initialSort "Stars"
|
||||||
}
|
}
|
||||||
, githubSearch (getQueryString initialQuery)
|
, githubSearch (getQueryString initialQuery)
|
||||||
)
|
)
|
||||||
@@ -63,14 +66,41 @@ init =
|
|||||||
|
|
||||||
view : Model -> Html Msg
|
view : Model -> Html Msg
|
||||||
view model =
|
view model =
|
||||||
div []
|
div [ class "home-container" ]
|
||||||
[ input [ class "search-query", onInput SetQuery, defaultValue model.query ] []
|
[ input [ class "search-query", onInput SetQuery, defaultValue model.query ] []
|
||||||
, button [ class "search-button", onClick Search ] [ text "Search" ]
|
, button [ class "search-button", onClick Search ] [ text "Search" ]
|
||||||
, viewErrorMessage model.errorMessage
|
, viewErrorMessage model.errorMessage
|
||||||
, ul [ class "results" ] (List.map viewSearchResult model.results)
|
, Table.view tableConfig model.tableState model.results
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
tableConfig : Table.Config SearchResult Msg
|
||||||
|
tableConfig =
|
||||||
|
Table.config
|
||||||
|
{ toId = .id >> toString
|
||||||
|
, toMsg = SetTableState
|
||||||
|
, columns = [ starsColumn, nameColumn ]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
starsColumn : Table.Column SearchResult Msg
|
||||||
|
starsColumn =
|
||||||
|
Table.veryCustomColumn
|
||||||
|
{ name = "Stars"
|
||||||
|
, viewData = viewStars
|
||||||
|
, sorter = Table.increasingOrDecreasingBy (negate << .stars)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
nameColumn : Table.Column SearchResult Msg
|
||||||
|
nameColumn =
|
||||||
|
Table.veryCustomColumn
|
||||||
|
{ name = "Name"
|
||||||
|
, viewData = viewSearchResult
|
||||||
|
, sorter = Table.increasingOrDecreasingBy .name
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
viewErrorMessage : Maybe String -> Html a
|
viewErrorMessage : Maybe String -> Html a
|
||||||
viewErrorMessage errorMessage =
|
viewErrorMessage errorMessage =
|
||||||
case errorMessage of
|
case errorMessage of
|
||||||
@@ -81,12 +111,16 @@ viewErrorMessage errorMessage =
|
|||||||
text ""
|
text ""
|
||||||
|
|
||||||
|
|
||||||
viewSearchResult : SearchResult -> Html Msg
|
viewStars : SearchResult -> Table.HtmlDetails Msg
|
||||||
|
viewStars result =
|
||||||
|
Table.HtmlDetails []
|
||||||
|
[ span [ class "star-count" ] [ text (toString result.stars) ] ]
|
||||||
|
|
||||||
|
|
||||||
|
viewSearchResult : SearchResult -> Table.HtmlDetails Msg
|
||||||
viewSearchResult result =
|
viewSearchResult result =
|
||||||
li []
|
Table.HtmlDetails []
|
||||||
[ span [ class "star-count" ] [ text (toString result.stars) ]
|
[ a [ onClick (Visit ("/repositories/" ++ result.name)) ] [ text result.name ]
|
||||||
, a [ onClick (Visit ("/repositories/" ++ result.name)) ]
|
|
||||||
[ text result.name ]
|
|
||||||
, button [ class "hide-result", onClick (DeleteById result.id) ]
|
, button [ class "hide-result", onClick (DeleteById result.id) ]
|
||||||
[ text "X" ]
|
[ text "X" ]
|
||||||
]
|
]
|
||||||
@@ -99,6 +133,7 @@ type Msg
|
|||||||
| DeleteById Int
|
| DeleteById Int
|
||||||
| HandleSearchResponse (List SearchResult)
|
| HandleSearchResponse (List SearchResult)
|
||||||
| HandleSearchError (Maybe String)
|
| HandleSearchError (Maybe String)
|
||||||
|
| SetTableState Table.State
|
||||||
| DoNothing
|
| DoNothing
|
||||||
|
|
||||||
|
|
||||||
@@ -131,6 +166,9 @@ update msg model =
|
|||||||
in
|
in
|
||||||
( newModel, Cmd.none )
|
( newModel, Cmd.none )
|
||||||
|
|
||||||
|
SetTableState newState ->
|
||||||
|
( { model | tableState = newState }, Cmd.none )
|
||||||
|
|
||||||
DoNothing ->
|
DoNothing ->
|
||||||
( model, Cmd.none )
|
( model, Cmd.none )
|
||||||
|
|
||||||
|
|||||||
@@ -117,6 +117,14 @@ th {
|
|||||||
text-align: left;
|
text-align: left;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.home-container th {
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.home-container th:hover {
|
||||||
|
color: rgb(96, 181, 204);
|
||||||
|
}
|
||||||
|
|
||||||
th, td {
|
th, td {
|
||||||
font-size: 18px;
|
font-size: 18px;
|
||||||
padding-right: 20px;
|
padding-right: 20px;
|
||||||
|
|||||||
Reference in New Issue
Block a user