Extract helper function.

This commit is contained in:
Dillon Kearns 2022-08-16 15:49:15 -07:00
parent 564e410817
commit 2d912ffcb3

View File

@ -152,42 +152,11 @@ action routeParams =
in
case formResult of
Ok (Add newItemDescription) ->
okSessionThing
|> Session.get "sessionId"
|> Maybe.map Data.Session.get
|> Maybe.map Request.Hasura.dataSource
|> Maybe.map
(DataSource.andThen
(\maybeUserSession ->
let
bar : Maybe Uuid
bar =
maybeUserSession
|> Maybe.map .id
in
case bar of
Nothing ->
DataSource.succeed
( okSessionThing
, Response.render {}
)
Just userId ->
withUserSession session
(\userId ->
Data.Todo.create userId newItemDescription
|> Request.Hasura.mutationDataSource
|> DataSource.map
(\newItemId ->
( okSessionThing
, Response.render {}
)
)
)
)
|> Maybe.withDefault
(DataSource.succeed
( okSessionThing
, Response.render {}
)
|> DataSource.map (\_ -> Response.render {})
)
Ok (CheckAll toggleTo) ->
@ -404,6 +373,51 @@ action routeParams =
)
withUserSession :
Result x (Maybe Session)
-> (Uuid -> DataSource (Response ActionData ErrorPage))
-> DataSource ( Session, Response ActionData ErrorPage )
withUserSession cookieSession continue =
let
okSessionThing : Session
okSessionThing =
cookieSession
|> Result.withDefault Nothing
|> Maybe.withDefault Session.empty
in
okSessionThing
|> Session.get "sessionId"
|> Maybe.map Data.Session.get
|> Maybe.map Request.Hasura.dataSource
|> Maybe.map
(DataSource.andThen
(\maybeUserSession ->
let
maybeUserId : Maybe Uuid
maybeUserId =
maybeUserSession
|> Maybe.map .id
in
case maybeUserId of
Nothing ->
DataSource.succeed
( okSessionThing
, Response.render {}
)
Just userId ->
continue userId
|> DataSource.map (Tuple.pair okSessionThing)
)
)
|> Maybe.withDefault
(DataSource.succeed
( okSessionThing
, Response.render {}
)
)
subscriptions : Maybe PageUrl -> RouteParams -> Path -> Shared.Model -> Model -> Sub Msg
subscriptions maybePageUrl routeParams path sharedModel model =
Sub.none