aux-search/src/Main.elm

214 lines
4.6 KiB
Elm
Raw Normal View History

2020-03-28 04:09:01 +00:00
port module Main exposing (main)
import Browser exposing (UrlRequest(..))
import Browser.Navigation as Nav exposing (Key)
import Html exposing (..)
import Html.Attributes exposing (..)
2020-03-31 00:59:06 +00:00
import Html.Events exposing (onClick, onInput)
2020-03-28 04:09:01 +00:00
import Http exposing (Error(..))
import Json.Decode as Decode
import Url exposing (Url)
2020-03-31 00:59:06 +00:00
import Url.Parser as UrlParser exposing ((</>), (<?>), Parser)
import Url.Parser.Query as UrlParserQuery
2020-03-28 04:09:01 +00:00
-- ---------------------------
-- PORTS
-- ---------------------------
port toJs : String -> Cmd msg
-- ---------------------------
-- MODEL
-- ---------------------------
type alias Model =
{ key : Key
, page : Page
}
2020-03-31 00:59:06 +00:00
type alias SearchModel =
{ query : String
, results : List String
}
2020-03-28 04:09:01 +00:00
type Page
2020-03-31 00:59:06 +00:00
= Search SearchModel
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
)
2020-03-28 04:09:01 +00:00
-- ---------------------------
-- URL Parsing and Routing
-- ---------------------------
handleUrlRequest : Key -> UrlRequest -> Cmd msg
handleUrlRequest key urlRequest =
case urlRequest of
Internal url ->
Nav.pushUrl key (Url.toString url)
External url ->
Nav.load url
urlParser : Parser (Page -> msg) msg
urlParser =
UrlParser.oneOf
2020-03-31 00:59:06 +00:00
[ UrlParser.map
(\q ->
Search
{ query = q |> Maybe.withDefault ""
, results = []
}
)
(UrlParser.s "search" <?> UrlParserQuery.string "query")
2020-03-28 04:09:01 +00:00
]
-- ---------------------------
-- UPDATE
-- ---------------------------
type Msg
= OnUrlRequest UrlRequest
| OnUrlChange Url
2020-03-31 00:59:06 +00:00
| SearchPageInput String
| SearchQuerySubmit
initPage : Page -> Cmd Msg
initPage page =
case page of
Search model ->
Cmd.none
2020-03-28 04:09:01 +00:00
update : Msg -> Model -> ( Model, Cmd Msg )
update message model =
case message of
OnUrlRequest urlRequest ->
( model, handleUrlRequest model.key urlRequest )
OnUrlChange url ->
let
2020-03-31 00:59:06 +00:00
newModel =
{ model | page = UrlParser.parse urlParser url |> Maybe.withDefault model.page }
2020-03-28 04:09:01 +00:00
in
2020-03-31 00:59:06 +00:00
( { newModel
| page =
case newModel.page of
Search searchModel ->
Search
{ searchModel
| results =
if searchModel.query == "" then
[]
else
[ "result1" ]
}
}
, initPage model.page
2020-03-28 04:09:01 +00:00
)
2020-03-31 00:59:06 +00:00
SearchPageInput query ->
( { model
| page =
case model.page of
Search searchModel ->
Search { searchModel | query = query }
}
, Cmd.none
)
2020-03-28 04:09:01 +00:00
2020-03-31 00:59:06 +00:00
SearchQuerySubmit ->
case model.page of
Search searchModel ->
( model
, Nav.pushUrl model.key <| "/search?query=" ++ searchModel.query
)
2020-03-28 04:09:01 +00:00
-- ---------------------------
-- VIEW
-- ---------------------------
view : Model -> Html Msg
view model =
div [ class "container" ]
[ header []
2020-03-31 00:59:06 +00:00
[ h1 [] [ text "NixOS Search" ]
2020-03-28 04:09:01 +00:00
]
, case model.page of
2020-03-31 00:59:06 +00:00
Search searchModel ->
searchPage searchModel
2020-03-28 04:09:01 +00:00
]
2020-03-31 00:59:06 +00:00
searchPage : SearchModel -> Html Msg
searchPage model =
div []
[ div []
[ input
[ type_ "text"
, onInput SearchPageInput
, value model.query
]
[]
, button [ onClick SearchQuerySubmit ] [ text "Search" ]
2020-03-28 04:09:01 +00:00
]
2020-03-31 00:59:06 +00:00
, ul [] (List.map searchPageResult model.results)
2020-03-28 04:09:01 +00:00
]
2020-03-31 00:59:06 +00:00
searchPageResult : String -> Html Msg
searchPageResult item =
li [] [ text item ]
2020-03-28 04:09:01 +00:00
-- ---------------------------
-- MAIN
-- ---------------------------
main : Program Int Model Msg
main =
Browser.application
{ init = init
, update = update
, view =
\m ->
2020-03-31 00:59:06 +00:00
{ title = "NixOS Search"
2020-03-28 04:09:01 +00:00
, body = [ view m ]
}
, subscriptions = \_ -> Sub.none
, onUrlRequest = OnUrlRequest
, onUrlChange = OnUrlChange
}