init work on search
This commit is contained in:
parent
3643fbe322
commit
ea09e01c89
181
src/Main.elm
181
src/Main.elm
|
@ -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,57 +113,43 @@ 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 )
|
|
||||||
|
|
||||||
Inc ->
|
|
||||||
case model.page of
|
|
||||||
Counter x ->
|
|
||||||
let
|
|
||||||
xx =
|
|
||||||
x + 1
|
|
||||||
in
|
|
||||||
( { model | page = Counter xx }
|
|
||||||
, Nav.pushUrl model.key <| "/counter/" ++ String.fromInt xx
|
|
||||||
)
|
|
||||||
|
|
||||||
_ ->
|
|
||||||
( model, Cmd.none )
|
|
||||||
|
|
||||||
TestServer ->
|
|
||||||
let
|
let
|
||||||
expect =
|
newModel =
|
||||||
Http.expectJson OnServerResponse (Decode.field "result" Decode.string)
|
{ model | page = UrlParser.parse urlParser url |> Maybe.withDefault model.page }
|
||||||
in
|
in
|
||||||
( model
|
( { newModel
|
||||||
, Http.get { url = "/test", expect = expect }
|
| page =
|
||||||
|
case newModel.page of
|
||||||
|
Search searchModel ->
|
||||||
|
Search
|
||||||
|
{ searchModel
|
||||||
|
| results =
|
||||||
|
if searchModel.query == "" then
|
||||||
|
[]
|
||||||
|
|
||||||
|
else
|
||||||
|
[ "result1" ]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
, initPage model.page
|
||||||
)
|
)
|
||||||
|
|
||||||
OnServerResponse res ->
|
SearchPageInput query ->
|
||||||
case res of
|
( { model
|
||||||
Ok serverMessage ->
|
| page =
|
||||||
( { model | page = Server serverMessage }, Cmd.none )
|
case model.page of
|
||||||
|
Search searchModel ->
|
||||||
|
Search { searchModel | query = query }
|
||||||
|
}
|
||||||
|
, Cmd.none
|
||||||
|
)
|
||||||
|
|
||||||
Err err ->
|
SearchQuerySubmit ->
|
||||||
( { model | page = Server <| "Error: " ++ httpErrorToString err }, Cmd.none )
|
case model.page of
|
||||||
|
Search searchModel ->
|
||||||
|
( model
|
||||||
httpErrorToString : Http.Error -> String
|
, Nav.pushUrl model.key <| "/search?query=" ++ searchModel.query
|
||||||
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
|
||||||
|
]
|
||||||
|
[]
|
||||||
|
, button [ onClick SearchQuerySubmit ] [ text "Search" ]
|
||||||
]
|
]
|
||||||
[ text "+ 1" ]
|
, ul [] (List.map searchPageResult model.results)
|
||||||
, text <| String.fromInt counter
|
|
||||||
, p [] [ text "Then make a change to the source code and see how the state is retained after you recompile." ]
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
|
|
Loading…
Reference in a new issue