From e191b1c8705983b4807cfa6bd40e802ad0a5c09f Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Mon, 25 Jul 2022 15:57:40 +0200 Subject: [PATCH] Extract function. --- examples/smoothies/app/Route/Login.elm | 77 +++++++++++++------------- 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/examples/smoothies/app/Route/Login.elm b/examples/smoothies/app/Route/Login.elm index 53ba4632..33630c78 100644 --- a/examples/smoothies/app/Route/Login.elm +++ b/examples/smoothies/app/Route/Login.elm @@ -48,42 +48,7 @@ route = RouteBuilder.serverRender { head = head , data = data - , action = - \_ -> - MySession.withSession - (Request.formDataWithoutServerValidation [ form ]) - (\usernameResult session -> - case usernameResult of - Err _ -> - ( session - |> Result.withDefault Nothing - |> Maybe.withDefault Session.empty - |> Session.withFlash "message" "Invalid form submission - no userId provided" - , Route.redirectTo Route.Login - ) - |> DataSource.succeed - - Ok username -> - case userIdMap |> Dict.get username of - Just userId -> - ( session - |> Result.withDefault Nothing - |> Maybe.withDefault Session.empty - |> Session.insert "userId" userId - |> Session.withFlash "message" ("Welcome " ++ username ++ "!") - , Route.redirectTo Route.Index - ) - |> DataSource.succeed - - Nothing -> - ( session - |> Result.withDefault Nothing - |> Maybe.withDefault Session.empty - |> Session.withFlash "message" ("Couldn't find username " ++ username) - , Route.redirectTo Route.Login - ) - |> DataSource.succeed - ) + , action = action } |> RouteBuilder.buildNoState { view = view } @@ -110,7 +75,7 @@ form = ] } ) - |> Form.field "username" (Field.text |> Field.required "Required") + |> Form.field "username" (Field.text |> Field.email |> Field.required "Required") fieldView : @@ -171,6 +136,44 @@ data routeParams = ) +action : RouteParams -> Request.Parser (DataSource (Response ActionData ErrorPage)) +action routeParams = + MySession.withSession + (Request.formDataWithoutServerValidation [ form ]) + (\usernameResult session -> + case usernameResult of + Err _ -> + ( session + |> Result.withDefault Nothing + |> Maybe.withDefault Session.empty + |> Session.withFlash "message" "Invalid form submission - no userId provided" + , Route.redirectTo Route.Login + ) + |> DataSource.succeed + + Ok username -> + case userIdMap |> Dict.get username of + Just userId -> + ( session + |> Result.withDefault Nothing + |> Maybe.withDefault Session.empty + |> Session.insert "userId" userId + |> Session.withFlash "message" ("Welcome " ++ username ++ "!") + , Route.redirectTo Route.Index + ) + |> DataSource.succeed + + Nothing -> + ( session + |> Result.withDefault Nothing + |> Maybe.withDefault Session.empty + |> Session.withFlash "message" ("Couldn't find username " ++ username) + , Route.redirectTo Route.Login + ) + |> DataSource.succeed + ) + + head : StaticPayload Data ActionData RouteParams -> List Head.Tag