init work on search

This commit is contained in:
Rok Garbas 2020-03-31 02:59:06 +02:00
parent 3643fbe322
commit ea09e01c89
Failed to generate hash of commit

View file

@ -4,11 +4,12 @@ import Browser exposing (UrlRequest(..))
import Browser.Navigation as Nav exposing (Key) import Browser.Navigation as Nav exposing (Key)
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (..) import Html.Attributes exposing (..)
import Html.Events exposing (onClick) import Html.Events exposing (onClick, onInput)
import Http exposing (Error(..)) import Http exposing (Error(..))
import Json.Decode as Decode import Json.Decode as Decode
import Url exposing (Url) import Url exposing (Url)
import Url.Parser as UrlParser exposing ((</>), Parser) import Url.Parser as UrlParser exposing ((</>), (<?>), Parser)
import Url.Parser.Query as UrlParserQuery
@ -32,14 +33,27 @@ type alias Model =
} }
init : Int -> Url -> Key -> ( Model, Cmd Msg ) type alias SearchModel =
init flags url key = { query : String
( { key = key, page = UrlParser.parse urlParser url |> Maybe.withDefault (Counter 0) }, Cmd.none ) , results : List String
}
type Page type Page
= Counter Int = Search SearchModel
| Server String
emptySearch =
Search { query = "", results = [] }
init : Int -> Url -> Key -> ( Model, Cmd Msg )
init flags url key =
( { key = key
, page = UrlParser.parse urlParser url |> Maybe.withDefault emptySearch
}
, Cmd.none
)
@ -61,9 +75,14 @@ handleUrlRequest key urlRequest =
urlParser : Parser (Page -> msg) msg urlParser : Parser (Page -> msg) msg
urlParser = urlParser =
UrlParser.oneOf UrlParser.oneOf
[ UrlParser.map Counter <| UrlParser.s "counter" </> UrlParser.int [ UrlParser.map
, UrlParser.map Server <| UrlParser.s "server" </> UrlParser.string (\q ->
, UrlParser.map (Server "") <| UrlParser.s "server" Search
{ query = q |> Maybe.withDefault ""
, results = []
}
)
(UrlParser.s "search" <?> UrlParserQuery.string "query")
] ]
@ -76,9 +95,15 @@ urlParser =
type Msg type Msg
= OnUrlRequest UrlRequest = OnUrlRequest UrlRequest
| OnUrlChange Url | OnUrlChange Url
| Inc | SearchPageInput String
| TestServer | SearchQuerySubmit
| OnServerResponse (Result Http.Error String)
initPage : Page -> Cmd Msg
initPage page =
case page of
Search model ->
Cmd.none
update : Msg -> Model -> ( Model, Cmd Msg ) update : Msg -> Model -> ( Model, Cmd Msg )
@ -88,58 +113,44 @@ update message model =
( model, handleUrlRequest model.key urlRequest ) ( model, handleUrlRequest model.key urlRequest )
OnUrlChange url -> OnUrlChange url ->
( { model | page = UrlParser.parse urlParser url |> Maybe.withDefault model.page }, Cmd.none ) let
newModel =
{ model | page = UrlParser.parse urlParser url |> Maybe.withDefault model.page }
in
( { newModel
| page =
case newModel.page of
Search searchModel ->
Search
{ searchModel
| results =
if searchModel.query == "" then
[]
Inc -> else
[ "result1" ]
}
}
, initPage model.page
)
SearchPageInput query ->
( { model
| page =
case model.page of case model.page of
Counter x -> Search searchModel ->
let Search { searchModel | query = query }
xx = }
x + 1 , Cmd.none
in
( { model | page = Counter xx }
, Nav.pushUrl model.key <| "/counter/" ++ String.fromInt xx
) )
_ -> SearchQuerySubmit ->
( model, Cmd.none ) case model.page of
Search searchModel ->
TestServer ->
let
expect =
Http.expectJson OnServerResponse (Decode.field "result" Decode.string)
in
( model ( model
, Http.get { url = "/test", expect = expect } , Nav.pushUrl model.key <| "/search?query=" ++ searchModel.query
) )
OnServerResponse res ->
case res of
Ok serverMessage ->
( { model | page = Server serverMessage }, Cmd.none )
Err err ->
( { model | page = Server <| "Error: " ++ httpErrorToString err }, Cmd.none )
httpErrorToString : Http.Error -> String
httpErrorToString err =
case err of
BadUrl _ ->
"BadUrl"
Timeout ->
"Timeout"
NetworkError ->
"NetworkError"
BadStatus _ ->
"BadStatus"
BadBody s ->
"BadBody: " ++ s
-- --------------------------- -- ---------------------------
@ -151,47 +162,33 @@ view : Model -> Html Msg
view model = view model =
div [ class "container" ] div [ class "container" ]
[ header [] [ header []
[ img [ src "/images/logo.png" ] [] [ h1 [] [ text "NixOS Search" ]
, h1 [] [ text "Elm 0.19 Webpack Starter, with hot-reloading" ]
] ]
, case model.page of , case model.page of
Counter counter -> Search searchModel ->
counterPage counter searchPage searchModel
Server serverMessage ->
serverPage serverMessage
, p []
[ text "And now don't forget to add a star to the Github repo "
, a [ href "https://github.com/simonh1000/elm-webpack-starter" ] [ text "elm-webpack-starter" ]
]
] ]
counterPage counter = searchPage : SearchModel -> Html Msg
div [ class "pure-u-1-3" ] searchPage model =
[ a [ href "/server/" ] [ text "Switch to server" ] div []
, p [] [ text "Click on the button below to increment the state." ] [ div []
, button [ input
[ class "pure-button pure-button-primary" [ type_ "text"
, onClick Inc , onInput SearchPageInput
, value model.query
] ]
[ text "+ 1" ] []
, text <| String.fromInt counter , button [ onClick SearchQuerySubmit ] [ text "Search" ]
, p [] [ text "Then make a change to the source code and see how the state is retained after you recompile." ] ]
, ul [] (List.map searchPageResult model.results)
] ]
serverPage serverMessage = searchPageResult : String -> Html Msg
div [ class "pure-u-1-3" ] searchPageResult item =
[ a [ href "/counter/1" ] [ text "Switch to counter" ] li [] [ text item ]
, p [] [ text "Test the dev server" ]
, button
[ class "pure-button pure-button-primary"
, onClick TestServer
]
[ text "ping dev server" ]
, text serverMessage
]
@ -207,7 +204,7 @@ main =
, update = update , update = update
, view = , view =
\m -> \m ->
{ title = "Elm 0.19 starter" { title = "NixOS Search"
, body = [ view m ] , body = [ view m ]
} }
, subscriptions = \_ -> Sub.none , subscriptions = \_ -> Sub.none