mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-11-24 06:54:03 +03:00
Add redirectTo helper.
This commit is contained in:
parent
911270bb40
commit
52c8a73485
@ -9,6 +9,7 @@ import Html.Styled.Attributes as Attr
|
||||
import MySession
|
||||
import Pages.PageUrl exposing (PageUrl)
|
||||
import Pages.Url
|
||||
import Route
|
||||
import RouteBuilder exposing (StatefulRoute, StatelessRoute, StaticPayload)
|
||||
import Server.Request as Request
|
||||
import Server.Response as Response exposing (Response)
|
||||
@ -55,8 +56,7 @@ data routeParams =
|
||||
|> Maybe.withDefault Session.empty
|
||||
|> Session.insert "name" name
|
||||
|> Session.withFlash "message" ("Welcome " ++ name ++ "!")
|
||||
, "/greet"
|
||||
|> Response.temporaryRedirect
|
||||
, Route.redirectTo Route.Greet
|
||||
)
|
||||
|> DataSource.succeed
|
||||
)
|
||||
|
@ -9,6 +9,7 @@ import Head.Seo as Seo
|
||||
import Pages.PageUrl exposing (PageUrl)
|
||||
import Pages.Url
|
||||
import Path exposing (Path)
|
||||
import Route
|
||||
import RouteBuilder exposing (StatefulRoute, StatelessRoute, StaticPayload)
|
||||
import Server.Request as Request
|
||||
import Server.Response as Response exposing (Response)
|
||||
@ -87,7 +88,7 @@ data : RouteParams -> Request.Parser (DataSource (Response Data ErrorPage))
|
||||
data routeParams =
|
||||
Request.oneOf
|
||||
[ Request.acceptMethod ( Request.Post, [] )
|
||||
(Request.succeed (DataSource.succeed (Response.temporaryRedirect "/hello")))
|
||||
(Request.succeed (DataSource.succeed (Route.redirectTo Route.Hello)))
|
||||
, Request.succeed (DataSource.succeed (Response.render Data))
|
||||
]
|
||||
|
||||
|
@ -3,6 +3,7 @@ module MySession exposing (..)
|
||||
import Codec
|
||||
import DataSource exposing (DataSource)
|
||||
import DataSource.Env as Env
|
||||
import Route
|
||||
import Server.Request exposing (Parser)
|
||||
import Server.Response as Response exposing (Response)
|
||||
import Server.Session as Session
|
||||
@ -37,7 +38,7 @@ withSessionOrRedirect handler toRequest =
|
||||
|> Result.withDefault
|
||||
(DataSource.succeed
|
||||
( Session.empty
|
||||
, Response.temporaryRedirect "/login"
|
||||
, Route.redirectTo Route.Login
|
||||
)
|
||||
)
|
||||
)
|
||||
@ -61,7 +62,7 @@ expectSessionOrRedirect toRequest handler =
|
||||
|> Maybe.withDefault
|
||||
(DataSource.succeed
|
||||
( Session.empty
|
||||
, Response.temporaryRedirect "/login"
|
||||
, Route.redirectTo Route.Login
|
||||
)
|
||||
)
|
||||
)
|
||||
|
@ -97,7 +97,7 @@ redirectRoute =
|
||||
ApiRoute.succeed
|
||||
(Server.Request.succeed
|
||||
(DataSource.succeed
|
||||
(Server.Response.temporaryRedirect "/")
|
||||
(Route.redirectTo Route.Index)
|
||||
)
|
||||
)
|
||||
|> ApiRoute.literal "api"
|
||||
@ -163,7 +163,7 @@ logout =
|
||||
(\() sessionResult ->
|
||||
DataSource.succeed
|
||||
( Session.empty
|
||||
, Server.Response.temporaryRedirect "/login"
|
||||
, Route.redirectTo Route.Login
|
||||
)
|
||||
)
|
||||
)
|
||||
|
@ -10,6 +10,7 @@ import Html.Attributes as Attr
|
||||
import MySession
|
||||
import Pages.PageUrl exposing (PageUrl)
|
||||
import Pages.Url
|
||||
import Route
|
||||
import RouteBuilder exposing (StatefulRoute, StatelessRoute, StaticPayload)
|
||||
import Server.Request as Request
|
||||
import Server.Response exposing (Response)
|
||||
@ -56,8 +57,7 @@ data routeParams =
|
||||
|> Maybe.withDefault Session.empty
|
||||
|> Session.insert "name" name
|
||||
|> Session.withFlash "message" ("Welcome " ++ name ++ "!")
|
||||
, "/greet"
|
||||
|> Server.Response.temporaryRedirect
|
||||
, Route.redirectTo Route.Greet
|
||||
)
|
||||
|> DataSource.succeed
|
||||
)
|
||||
|
@ -22,6 +22,7 @@ import Pages.PageUrl exposing (PageUrl)
|
||||
import Pages.Url
|
||||
import Path exposing (Path)
|
||||
import Request.Fauna
|
||||
import Route
|
||||
import RouteBuilder exposing (StatefulRoute, StatelessRoute, StaticPayload)
|
||||
import Server.Request as Request exposing (Parser)
|
||||
import Server.Response as Response exposing (Response)
|
||||
@ -162,9 +163,7 @@ data routeParams =
|
||||
Ok id ->
|
||||
Request.Fauna.mutationDataSource "" (deleteTodo id)
|
||||
|> DataSource.map
|
||||
(\_ ->
|
||||
Response.temporaryRedirect "/todos"
|
||||
)
|
||||
(\_ -> Route.redirectTo Route.Todos)
|
||||
|
||||
Err error ->
|
||||
{ todos = [] }
|
||||
@ -177,9 +176,7 @@ data routeParams =
|
||||
Ok okItem ->
|
||||
Request.Fauna.mutationDataSource "" (createTodo okItem.description)
|
||||
|> DataSource.map
|
||||
(\_ ->
|
||||
Response.temporaryRedirect "/todos"
|
||||
)
|
||||
(\_ -> Route.redirectTo Route.Todos)
|
||||
|
||||
Err error ->
|
||||
{ todos = []
|
||||
|
@ -3,6 +3,7 @@ module MySession exposing (..)
|
||||
import Codec
|
||||
import DataSource exposing (DataSource)
|
||||
import DataSource.Env as Env
|
||||
import Route
|
||||
import Server.Request exposing (Parser)
|
||||
import Server.Response as Response exposing (Response)
|
||||
import Server.Session as Session
|
||||
@ -37,7 +38,7 @@ withSessionOrRedirect handler toRequest =
|
||||
|> Result.withDefault
|
||||
(DataSource.succeed
|
||||
( Session.empty
|
||||
, Response.temporaryRedirect "/login"
|
||||
, Route.redirectTo Route.Login
|
||||
)
|
||||
)
|
||||
)
|
||||
@ -61,7 +62,7 @@ expectSessionOrRedirect toRequest handler =
|
||||
|> Maybe.withDefault
|
||||
(DataSource.succeed
|
||||
( Session.empty
|
||||
, Response.temporaryRedirect "/login"
|
||||
, Route.redirectTo Route.Login
|
||||
)
|
||||
)
|
||||
)
|
||||
|
@ -888,11 +888,11 @@ decodeBytes bytesDecoder items =
|
||||
-- Lamdera.Wire3.bytesDecodeStrict bytesDecoder items
|
||||
|> Result.fromMaybe "Decoding error"
|
||||
`,
|
||||
routesModule: `module Route exposing (baseUrlAsPath, Route(..), link, matchers, routeToPath, toLink, urlToRoute, toPath)
|
||||
routesModule: `module Route exposing (baseUrlAsPath, Route(..), link, matchers, routeToPath, toLink, urlToRoute, toPath, redirectTo)
|
||||
|
||||
{-|
|
||||
|
||||
@docs Route, link, matchers, routeToPath, toLink, urlToRoute, toPath
|
||||
@docs Route, link, matchers, routeToPath, toLink, urlToRoute, toPath, redirectTo
|
||||
|
||||
-}
|
||||
|
||||
@ -1017,6 +1017,13 @@ link attributes children route =
|
||||
children
|
||||
)
|
||||
route
|
||||
|
||||
|
||||
redirectTo : Route -> Server.Response.Response data error
|
||||
redirectTo route =
|
||||
route
|
||||
|> toString
|
||||
|> Server.Response.temporaryRedirect
|
||||
`,
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user