Skip to content

Commit

Permalink
Fix/join with invitation (#596)
Browse files Browse the repository at this point in the history
* Prevent redirecting to selector if user has invite

* Redirect user to dashboard after signing up with invite

* Fix according to elm-review
  • Loading branch information
henriquecbuss authored Jul 15, 2021
1 parent 80a622e commit 0b1e7b7
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 55 deletions.
141 changes: 89 additions & 52 deletions src/elm/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -641,15 +641,24 @@ updateGuestUResult toStatus toMsg model uResult =

( session, cmd ) =
LoggedIn.initLogin shared (Just privateKey) userWithCommunity token

redirectRoute =
case guest.afterLoginRedirect of
Just (Route.Invite _) ->
Route.Dashboard

Just route ->
route

Nothing ->
Route.Dashboard
in
( { m
| session =
Page.LoggedIn session
}
, Cmd.map (Page.GotLoggedInMsg >> GotPageMsg) cmd
:: (Maybe.withDefault Route.Dashboard guest.afterLoginRedirect
|> Route.pushUrl guest.shared.navKey
)
:: Route.pushUrl guest.shared.navKey redirectRoute
:: cmds_
)

Expand Down Expand Up @@ -892,58 +901,56 @@ changeRouteTo maybeRoute model =
noCmd model_ =
( model_, Cmd.none )

afterLoginRedirect maybeRedirect =
let
addRedirect redirect =
case model.session of
Page.LoggedIn _ ->
model
addAfterLoginRedirect : Maybe Route -> Model -> Model
addAfterLoginRedirect maybeRedirect model_ =
case model_.session of
Page.LoggedIn _ ->
model_

Page.Guest guest ->
{ model
Page.Guest guest ->
case maybeRedirect of
Nothing ->
model_

Just redirect ->
{ model_
| session =
Guest.addAfterLoginRedirect redirect guest
|> Page.Guest
, status = Redirect
}
in
Maybe.map addRedirect maybeRedirect
|> Maybe.withDefault model

addMaybeInvitation : Maybe String -> Model -> Model
addMaybeInvitation maybeInvitation model_ =
case model_.session of
Page.LoggedIn _ ->
model_

Page.Guest guest ->
{ model_ | session = { guest | maybeInvitation = maybeInvitation } |> Page.Guest }

withGuest init_ update_ maybeInvitation maybeRedirect =
let
model_ =
afterLoginRedirect maybeRedirect
|> addMaybeInvitation maybeInvitation
{ model_
| session =
{ guest | maybeInvitation = maybeInvitation }
|> Page.Guest
}

fn =
init_
>> update_ model_
in
withGuest : Maybe String -> Maybe Route -> (Guest.Model -> ( Model, Cmd Msg )) -> ( Model, Cmd Msg )
withGuest maybeInvitation maybeRedirect fn =
case session of
Page.Guest guest ->
fn guest

Page.LoggedIn _ ->
let
redirect =
case maybeRedirect of
Nothing ->
Route.Dashboard
( model
, maybeRedirect
|> Maybe.withDefault Route.Dashboard
|> Route.replaceUrl shared.navKey
)

Just route_ ->
route_
Page.Guest guest ->
let
( newModel, newCmd ) =
fn guest
in
( model_
, Route.replaceUrl shared.navKey redirect
( newModel
|> addAfterLoginRedirect maybeRedirect
|> addMaybeInvitation maybeInvitation
, newCmd
)

addRouteToHistory status loggedIn =
Expand All @@ -963,6 +970,7 @@ changeRouteTo maybeRoute model =
routeHistory ->
{ loggedIn | routeHistory = newRoute :: routeHistory }

withLoggedIn : Route -> (LoggedIn.Model -> ( Model, Cmd Msg )) -> ( Model, Cmd Msg )
withLoggedIn route fn =
case session of
Page.LoggedIn loggedIn ->
Expand Down Expand Up @@ -1006,6 +1014,37 @@ changeRouteTo maybeRoute model =
else
Route.replaceUrl shared.navKey (Route.Join (Just route))
)

withSession : Route -> (Session -> ( Model, Cmd Msg )) -> ( Model, Cmd Msg )
withSession route fn =
let
( newModel, newCmd ) =
fn session
in
case newModel.session of
Page.LoggedIn loggedIn ->
let
newSession =
addRouteToHistory newModel.status loggedIn
|> Page.LoggedIn
in
( { newModel | session = newSession }
, Cmd.batch
[ newCmd
, Task.perform
(LoggedIn.GotTimeInternal
>> Page.GotLoggedInMsg
>> GotPageMsg
)
Time.now
]
)

Page.Guest _ ->
( newModel
|> addAfterLoginRedirect (Just route)
, newCmd
)
in
case maybeRoute of
Nothing ->
Expand All @@ -1024,18 +1063,14 @@ changeRouteTo maybeRoute model =
|> noCmd

Just (Route.Register invitation maybeRedirect) ->
withGuest
(Register.init invitation)
(updateStatusWith (Register invitation maybeRedirect) GotRegisterMsg)
invitation
maybeRedirect
Register.init invitation
>> updateStatusWith (Register invitation maybeRedirect) GotRegisterMsg model
|> withGuest invitation maybeRedirect

Just (Route.Login maybeInvitation maybeRedirect) ->
withGuest
Login.init
(updateStatusWith (Login maybeRedirect) GotLoginMsg)
maybeInvitation
maybeRedirect
Login.init
>> updateStatusWith (Login maybeRedirect) GotLoginMsg model
|> withGuest maybeInvitation maybeRedirect

Just (Route.PaymentHistory accountName) ->
PaymentHistory.init
Expand Down Expand Up @@ -1178,12 +1213,14 @@ changeRouteTo maybeRoute model =
|> withLoggedIn (Route.ViewTransfer transferId)

Just (Route.Invite invitationId) ->
Invite.init session invitationId
|> updateStatusWith Invite GotInviteMsg model
(\s -> Invite.init s invitationId)
>> updateStatusWith Invite GotInviteMsg model
|> withSession (Route.Invite invitationId)

Just (Route.Join maybeRedirect) ->
Join.init session maybeRedirect
|> updateStatusWith Join GotJoinMsg model
(\s -> Join.init s maybeRedirect)
>> updateStatusWith Join GotJoinMsg model
|> withSession (Route.Join maybeRedirect)

Just (Route.Transfer maybeTo) ->
(\l -> Transfer.init l maybeTo)
Expand Down
14 changes: 14 additions & 0 deletions src/elm/Page/Community/Invite.elm
Original file line number Diff line number Diff line change
Expand Up @@ -522,11 +522,25 @@ update session msg model =
, hasActions = community.hasObjectives
, hasKyc = community.hasKyc
}

navKey =
toShared session
|> .navKey

redirectRoute =
case session of
LoggedIn _ ->
Route.Dashboard

Guest guest ->
guest.afterLoginRedirect
|> Maybe.withDefault Route.Dashboard
in
model
|> UR.init
|> UR.addExt ({ loggedIn | authToken = token } |> LoggedIn.UpdatedLoggedIn)
|> UR.addExt (LoggedIn.AddedCommunity communityInfo)
|> UR.addCmd (Route.pushUrl navKey redirectRoute)

Nothing ->
model
Expand Down
29 changes: 26 additions & 3 deletions src/elm/Session/LoggedIn.elm
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,22 @@ loadCommunity ({ shared } as model) maybeSymbol =
)


getInvitation : Model -> Maybe String
getInvitation model =
case List.head model.routeHistory of
Just (Route.Invite invitation) ->
Just invitation

Just (Route.Login maybeInvitation _) ->
maybeInvitation

Just (Route.Register maybeInvitation _) ->
maybeInvitation

_ ->
Nothing


{-| Given a `Community.Model`, check if the user is part of it (or if it has
auto invites), and set it as default or redirect the user
-}
Expand Down Expand Up @@ -1324,9 +1340,16 @@ setCommunity community ({ shared } as model) =
)

else
( { model | selectedCommunity = RemoteData.Success community, shared = sharedWithCommunity }
, Cmd.batch [ Route.pushUrl shared.navKey (Route.CommunitySelector (List.head model.routeHistory)), storeCommunityCmd ]
)
case getInvitation model of
Just invitation ->
( { model | selectedCommunity = RemoteData.Success community, shared = sharedWithCommunity }
, Route.pushUrl shared.navKey (Route.Invite invitation)
)

Nothing ->
( { model | selectedCommunity = RemoteData.Success community, shared = sharedWithCommunity }
, Cmd.batch [ Route.pushUrl shared.navKey (Route.CommunitySelector (List.head model.routeHistory)), storeCommunityCmd ]
)


signUpForCommunity : Model -> Profile.CommunityInfo -> ( Model, Cmd Msg )
Expand Down

0 comments on commit 0b1e7b7

Please sign in to comment.