Extract function.

This commit is contained in:
Dillon Kearns 2022-07-25 15:57:40 +02:00
parent 058d4a3819
commit e191b1c870

View File

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