Skip to content

Commit

Permalink
Merge pull request #38 from elm-land/feature/page-hooks
Browse files Browse the repository at this point in the history
✨ Feature – URL query parameters work with pages
  • Loading branch information
ryan-haskell committed Sep 9, 2022
2 parents 9d4df42 + 7699a4a commit b3004fc
Show file tree
Hide file tree
Showing 26 changed files with 855 additions and 75 deletions.
2 changes: 1 addition & 1 deletion cli/README.md
Expand Up @@ -16,7 +16,7 @@ The `elm-land` CLI tool has everything you need to create your next Elm applicat
```
$ elm-land
🌈 Welcome to Elm Land! (v0.16.3)
🌈 Welcome to Elm Land! (v0.17.0)
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
Here are the available commands:
Expand Down
4 changes: 2 additions & 2 deletions cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/package.json
@@ -1,6 +1,6 @@
{
"name": "elm-land",
"version": "0.16.3",
"version": "0.17.0",
"description": "Reliable web apps for everyone",
"main": "index.js",
"bin": {
Expand Down
61 changes: 35 additions & 26 deletions cli/src/codegen/src/Commands/Generate.elm
Expand Up @@ -259,34 +259,43 @@ mainElmModule data =
, { name = "UrlChanged"
, arguments = [ CodeGen.Argument.new "url" ]
, expression =
CodeGen.Expression.letIn
{ let_ =
[ { argument = CodeGen.Argument.new "( pageModel, pageCmd )"
, annotation = Nothing
, expression =
CodeGen.Expression.function
{ name = "initPage"
, arguments =
[ CodeGen.Expression.record
[ ( "key", CodeGen.Expression.value "model.key" )
, ( "shared", CodeGen.Expression.value "model.shared" )
, ( "url", CodeGen.Expression.value "url" )
]
]
}
}
]
, in_ =
CodeGen.Expression.ifElse
{ condition = CodeGen.Expression.value "url.path == model.url.path"
, ifBranch =
CodeGen.Expression.multilineTuple
[ CodeGen.Expression.recordUpdate
{ value = "model"
, fields =
[ ( "url", CodeGen.Expression.value "url" )
, ( "page", CodeGen.Expression.value "pageModel" )
]
}
, CodeGen.Expression.value "pageCmd"
[ CodeGen.Expression.value "{ model | url = url }"
, CodeGen.Expression.value "Cmd.none"
]
, elseBranch =
CodeGen.Expression.letIn
{ let_ =
[ { argument = CodeGen.Argument.new "( pageModel, pageCmd )"
, annotation = Nothing
, expression =
CodeGen.Expression.function
{ name = "initPage"
, arguments =
[ CodeGen.Expression.record
[ ( "key", CodeGen.Expression.value "model.key" )
, ( "shared", CodeGen.Expression.value "model.shared" )
, ( "url", CodeGen.Expression.value "url" )
]
]
}
}
]
, in_ =
CodeGen.Expression.multilineTuple
[ CodeGen.Expression.recordUpdate
{ value = "model"
, fields =
[ ( "url", CodeGen.Expression.value "url" )
, ( "page", CodeGen.Expression.value "pageModel" )
]
}
, CodeGen.Expression.value "pageCmd"
]
}
}
}
, { name = "PageSent"
Expand Down
5 changes: 3 additions & 2 deletions cli/src/templates/_elm-land/customizable/Auth.elm
@@ -1,6 +1,7 @@
module Auth exposing (User, onPageLoad)

import Auth.Action
import Dict
import Route exposing (Route)
import Route.Path
import Shared
Expand All @@ -15,6 +16,6 @@ onPageLoad : Shared.Model -> Route () -> Auth.Action.Action User
onPageLoad shared route =
Auth.Action.pushRoute
{ path = Route.Path.NotFound_
, query = []
, query = Dict.empty
, hash = Nothing
}
}
7 changes: 4 additions & 3 deletions cli/src/templates/_elm-land/customizable/Effect.elm
Expand Up @@ -50,7 +50,7 @@ fromCmd =

pushRoute :
{ path : Route.Path.Path
, query : List ( String, Maybe String )
, query : Dict String String
, hash : Maybe String
}
-> Effect msg
Expand All @@ -60,7 +60,7 @@ pushRoute route =

replaceRoute :
{ path : Route.Path.Path
, query : List ( String, Maybe String )
, query : Dict String String
, hash : Maybe String
}
-> Effect msg
Expand Down Expand Up @@ -99,7 +99,8 @@ map fn effect =
LoadExternalUrl url


{-| ( Used by Elm Land internally ) -}
{-| ( Used by Elm Land internally )
-}
toCmd :
{ key : Browser.Navigation.Key
, fromSharedMsg : sharedMsg -> mainMsg
Expand Down
11 changes: 6 additions & 5 deletions cli/src/templates/_elm-land/src/Auth/Action.elm
Expand Up @@ -14,6 +14,7 @@ module Auth.Action exposing
-}

import Browser.Navigation
import Dict exposing (Dict)
import Route.Path
import Shared
import Url exposing (Url)
Expand All @@ -24,12 +25,12 @@ type Action user
= LoadPageWithUser user
| ReplaceRoute
{ path : Route.Path.Path
, query : List ( String, Maybe String )
, query : Dict String String
, hash : Maybe String
}
| PushRoute
{ path : Route.Path.Path
, query : List ( String, Maybe String )
, query : Dict String String
, hash : Maybe String
}
| ShowLoadingPage (View Never)
Expand All @@ -42,7 +43,7 @@ loadPageWithUser =

replaceRoute :
{ path : Route.Path.Path
, query : List ( String, Maybe String )
, query : Dict String String
, hash : Maybe String
}
-> Action user
Expand All @@ -52,7 +53,7 @@ replaceRoute =

pushRoute :
{ path : Route.Path.Path
, query : List ( String, Maybe String )
, query : Dict String String
, hash : Maybe String
}
-> Action user
Expand Down Expand Up @@ -94,4 +95,4 @@ subscriptions toView authAction =
Sub.none

ShowLoadingPage _ ->
Sub.none
Sub.none
8 changes: 4 additions & 4 deletions cli/src/templates/_elm-land/src/Route.elm
Expand Up @@ -22,7 +22,7 @@ import Url.Parser exposing ((</>), query)
type alias Route params =
{ path : Route.Path.Path
, params : params
, query : Dict String (Maybe String)
, query : Dict String String
, hash : Maybe String
, url : Url
}
Expand All @@ -40,7 +40,7 @@ fromUrl params url =

href :
{ path : Route.Path.Path
, query : List ( String, Maybe String )
, query : Dict String String
, hash : Maybe String
}
-> Html.Attribute msg
Expand All @@ -51,13 +51,13 @@ href route =
toString :
{ route
| path : Route.Path.Path
, query : List ( String, Maybe String )
, query : Dict String String
, hash : Maybe String
}
-> String
toString route =
String.join ""
[ Route.Path.toString route.path
, Route.Query.toStringFromList route.query |> Maybe.withDefault ""
, route.query |> Route.Query.toString |> Maybe.withDefault ""
, route.hash |> Maybe.map (String.append "#") |> Maybe.withDefault ""
]
32 changes: 16 additions & 16 deletions cli/src/templates/_elm-land/src/Route/Query.elm
@@ -1,11 +1,11 @@
module Route.Query exposing (fromUrl, toStringFromList)
module Route.Query exposing (fromUrl, toString)

import Dict exposing (Dict)
import Url exposing (Url)
import Url.Parser exposing (query)


fromUrl : Url -> Dict String (Maybe String)
fromUrl : Url -> Dict String String
fromUrl url =
case url.query of
Nothing ->
Expand All @@ -31,33 +31,33 @@ fromUrl url =
Nothing

key :: [] ->
Just ( decode key, Nothing )
Just ( decode key, "" )

key :: value :: _ ->
Just ( decode key, Just (decode value) )
Just ( decode key, decode value )
)
)
|> Dict.fromList


toStringFromList : List ( String, Maybe String ) -> Maybe String
toStringFromList queryParameterList =
if List.isEmpty queryParameterList then
toString : Dict String String -> Maybe String
toString queryParameterList =
if Dict.isEmpty queryParameterList then
Nothing

else
queryParameterList
|> Dict.toList
|> List.map
(\( key, maybeValue ) ->
case maybeValue of
Nothing ->
Url.percentEncode key
(\( key, value ) ->
if String.isEmpty value then
Url.percentEncode key

Just value ->
String.join "="
[ Url.percentEncode key
, Url.percentEncode value
]
else
String.join "="
[ Url.percentEncode key
, Url.percentEncode value
]
)
|> String.join "&"
|> String.append "?"
Expand Down
11 changes: 11 additions & 0 deletions cli/tests/08-examples.bats
Expand Up @@ -51,6 +51,17 @@ load helpers

expectOutputContains "successfully built"

rm -r .elm-land elm-stuff dist
cd ../../cli
}

@test "'elm-land build' works with the '06-query-parameters' example" {
cd ../examples/06-query-parameters
run elm-land build
expectToPass

expectOutputContains "successfully built"

rm -r .elm-land elm-stuff dist
cd ../../cli
}
6 changes: 3 additions & 3 deletions cli/tests/09-elm-binary.bats
Expand Up @@ -41,7 +41,7 @@ load helpers

cp -r ../examples/01-hello-world ../examples/01-local-hello
cd ../examples/01-local-hello
echo '{ "dependencies": { "elm-land": "file:../../cli/elm-land-0.16.3.tgz" } }' > package.json
echo '{ "dependencies": { "elm-land": "file:../../cli/elm-land-0.17.0.tgz" } }' > package.json
npm install

run npx elm-land build
Expand All @@ -59,7 +59,7 @@ load helpers

cp -r ../examples/01-hello-world ../examples/01-local-hello
cd ../examples/01-local-hello
echo '{ "dependencies": { "elm-land": "file:../../cli/elm-land-0.16.3.tgz" } }' > package.json
echo '{ "dependencies": { "elm-land": "file:../../cli/elm-land-0.17.0.tgz" } }' > package.json
npm install -g yarn
yarn

Expand All @@ -78,7 +78,7 @@ load helpers

cp -r ../examples/01-hello-world ../examples/01-local-hello
cd ../examples/01-local-hello
echo '{ "dependencies": { "elm-land": "file:../../cli/elm-land-0.16.3.tgz" } }' > package.json
echo '{ "dependencies": { "elm-land": "file:../../cli/elm-land-0.17.0.tgz" } }' > package.json
npm install -g pnpm
pnpm install

Expand Down

0 comments on commit b3004fc

Please sign in to comment.