Remove cleanUrl and fragment hack (#216)

This commit is contained in:
Marek Fajkus 2020-10-29 00:38:46 +01:00 committed by GitHub
parent e412085ea8
commit 2de4ef3bda
Failed to generate hash of commit
3 changed files with 90 additions and 111 deletions

View file

@ -49,7 +49,7 @@ type alias Flags =
type alias Model = type alias Model =
{ navKey : Browser.Navigation.Key { navKey : Browser.Navigation.Key
, url : Url.Url , route : Route.Route
, elasticsearch : Search.Options , elasticsearch : Search.Options
, page : Page , page : Page
} }
@ -71,7 +71,6 @@ init flags url navKey =
let let
model = model =
{ navKey = navKey { navKey = navKey
, url = url
, elasticsearch = , elasticsearch =
Search.Options Search.Options
flags.elasticsearchMappingSchemaVersion flags.elasticsearchMappingSchemaVersion
@ -79,6 +78,7 @@ init flags url navKey =
flags.elasticsearchUsername flags.elasticsearchUsername
flags.elasticsearchPassword flags.elasticsearchPassword
, page = NotFound , page = NotFound
, route = Route.Home
} }
in in
changeRouteTo model url changeRouteTo model url
@ -158,80 +158,78 @@ changeRouteTo :
Model Model
-> Url.Url -> Url.Url
-> ( Model, Cmd Msg ) -> ( Model, Cmd Msg )
changeRouteTo model url = changeRouteTo currentModel url =
let let
cleanUrl = attempteQuery (( newModel, cmd ) as pair) =
if url.fragment == Just "disabled" then case ( currentModel.route, newModel.route ) of
{ url | fragment = Nothing } ( Route.Packages channel1 query1 _ from1 size1 sort1, Route.Packages channel2 query2 _ from2 size2 sort2 ) ->
if channel1 /= channel2 || query1 /= query2 || from1 /= from2 || size1 /= size2 || sort1 /= sort2 then
submitQuery newModel ( newModel, cmd )
else else
url pair
newModel = ( Route.Options channel1 query1 _ from1 size1 sort1, Route.Options channel2 query2 _ from2 size2 sort2 ) ->
{ model | url = cleanUrl } if channel1 /= channel2 || query1 /= query2 || from1 /= from2 || size1 /= size2 || sort1 /= sort2 then
submitQuery newModel ( newModel, cmd )
maybeRoute = else
Route.fromUrl url pair
( a, b ) ->
if a /= b then
submitQuery newModel ( newModel, cmd )
else
pair
in in
case maybeRoute of case Route.fromUrl url of
Nothing -> Nothing ->
( { newModel ( { currentModel | page = NotFound }
| page = NotFound
}
, Cmd.none , Cmd.none
) )
Just Route.NotFound -> Just route ->
( { newModel
| page = NotFound
}
, Cmd.none
)
Just Route.Home ->
-- Always redirect to /packages until we have something to show
-- on the home page
( newModel, Browser.Navigation.pushUrl newModel.navKey "/packages" )
Just (Route.Packages channel query show from size sort) ->
let let
modelPage = model =
case newModel.page of { currentModel | route = route }
Packages x ->
Just x
_ ->
Nothing
in in
Page.Packages.init channel query show from size sort modelPage case route of
|> updateWith Packages PackagesMsg newModel Route.NotFound ->
|> (\x -> ( { model | page = NotFound }, Cmd.none )
if url.fragment == Just "disabled" then
( Tuple.first x, Cmd.none )
else Route.Home ->
submitQuery newModel x -- Always redirect to /packages until we have something to show
) -- on the home page
( model, Browser.Navigation.pushUrl model.navKey "/packages" )
Just (Route.Options channel query show from size sort) -> Route.Packages channel query show from size sort ->
let let
modelPage = modelPage =
case newModel.page of case model.page of
Options x -> Packages x ->
Just x Just x
_ -> _ ->
Nothing Nothing
in in
Page.Options.init channel query show from size sort modelPage Page.Packages.init channel query show from size sort modelPage
|> updateWith Options OptionsMsg newModel |> updateWith Packages PackagesMsg model
|> (\x -> |> attempteQuery
if url.fragment == Just "disabled" then
( Tuple.first x, Cmd.none )
else Route.Options channel query show from size sort ->
submitQuery newModel x let
) modelPage =
case model.page of
Options x ->
Just x
_ ->
Nothing
in
Page.Options.init channel query show from size sort modelPage
|> updateWith Options OptionsMsg model
|> attempteQuery
update : Msg -> Model -> ( Model, Cmd Msg ) update : Msg -> Model -> ( Model, Cmd Msg )
@ -241,11 +239,7 @@ update msg model =
case urlRequest of case urlRequest of
Browser.Internal url -> Browser.Internal url ->
( model ( model
, if url.fragment == Just "disabled" then , Browser.Navigation.pushUrl model.navKey <| Url.toString url
Cmd.none
else
Browser.Navigation.pushUrl model.navKey <| Url.toString url
) )
Browser.External href -> Browser.External href ->
@ -318,7 +312,7 @@ view model =
] ]
, div [ class "nav-collapse collapse" ] , div [ class "nav-collapse collapse" ]
[ ul [ class "nav pull-left" ] [ ul [ class "nav pull-left" ]
(viewNavigation model.page model.url) (viewNavigation model.route)
] ]
] ]
] ]
@ -349,52 +343,37 @@ view model =
} }
viewNavigation : Page -> Url.Url -> List (Html Msg) viewNavigation : Route.Route -> List (Html Msg)
viewNavigation page url = viewNavigation route =
let let
preserveSearchOptions = toRoute f =
case page of case route of
Packages model -> -- Preserve arguments
model.query Route.Packages channel query show from size sort ->
|> Maybe.map (\q -> [ Url.Builder.string "query" q ]) f channel query show from size sort
|> Maybe.withDefault []
|> List.append [ Url.Builder.string "channel" model.channel ]
Options model -> Route.Options channel query show from size sort ->
model.query f channel query show from size sort
|> Maybe.map (\q -> [ Url.Builder.string "query" q ])
|> Maybe.withDefault []
|> List.append [ Url.Builder.string "channel" model.channel ]
_ -> _ ->
[] f Nothing Nothing Nothing Nothing Nothing Nothing
createUrl path =
[]
|> List.append preserveSearchOptions
|> Url.Builder.absolute [ path ]
in in
List.map li [] [ a [ href "https://nixos.org" ] [ text "Back to nixos.org" ] ]
(viewNavigationItem url) :: List.map
[ ( "https://nixos.org", "Back to nixos.org" ) (viewNavigationItem route)
, ( createUrl "packages", "Packages" ) [ ( toRoute Route.Packages, "Packages" )
, ( createUrl "options", "Options" ) , ( toRoute Route.Options, "Options" )
] ]
viewNavigationItem : viewNavigationItem :
Url.Url Route.Route
-> ( String, String ) -> ( Route.Route, String )
-> Html Msg -> Html Msg
viewNavigationItem url ( path, title ) = viewNavigationItem currentRoute ( route, title ) =
li li
[ classList [ classList [ ( "active", currentRoute == route ) ] ]
[ ( "active" [ a [ Route.href route ] [ text title ] ]
, String.startsWith url.path path
)
]
]
[ a [ href path ] [ text title ] ]
viewPage : Model -> Html Msg viewPage : Model -> Html Msg

View file

@ -17,6 +17,7 @@ import Url.Parser.Query
type Route type Route
= NotFound = NotFound
| Home | Home
-- route | channel | (search) query | show | from | size | sort
| Packages (Maybe String) (Maybe String) (Maybe String) (Maybe Int) (Maybe Int) (Maybe String) | Packages (Maybe String) (Maybe String) (Maybe String) (Maybe Int) (Maybe Int) (Maybe String)
| Options (Maybe String) (Maybe String) (Maybe String) (Maybe Int) (Maybe Int) (Maybe String) | Options (Maybe String) (Maybe String) (Maybe String) (Maybe Int) (Maybe Int) (Maybe String)

View file

@ -282,7 +282,6 @@ update toRoute navKey msg model =
model.from model.from
model.size model.size
model.sort model.sort
|> (\x -> x ++ "#disabled")
|> Browser.Navigation.pushUrl navKey |> Browser.Navigation.pushUrl navKey
) )
@ -635,11 +634,11 @@ viewPager _ model result toRoute =
] ]
] ]
[ a [ a
[ if model.from == 0 then [ href <|
href "#disabled" if model.from == 0 then
""
else else
href <|
createUrl createUrl
toRoute toRoute
model.channel model.channel
@ -659,7 +658,7 @@ viewPager _ model result toRoute =
[ a [ a
[ href <| [ href <|
if model.from - model.size < 0 then if model.from - model.size < 0 then
"#disabled" ""
else else
createUrl createUrl
@ -681,7 +680,7 @@ viewPager _ model result toRoute =
[ a [ a
[ href <| [ href <|
if model.from + model.size >= result.hits.total.value then if model.from + model.size >= result.hits.total.value then
"#disabled" ""
else else
createUrl createUrl
@ -703,7 +702,7 @@ viewPager _ model result toRoute =
[ a [ a
[ href <| [ href <|
if model.from + model.size >= result.hits.total.value then if model.from + model.size >= result.hits.total.value then
"#disabled" ""
else else
let let