Add redirectTo helper.

This commit is contained in:
Dillon Kearns 2022-04-06 16:29:38 -07:00
parent 911270bb40
commit 52c8a73485
8 changed files with 26 additions and 19 deletions

View File

@ -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
)

View File

@ -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))
]

View File

@ -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
)
)
)

View File

@ -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
)
)
)

View File

@ -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
)

View File

@ -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 = []

View File

@ -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
)
)
)

View File

@ -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
`,
};
}