Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect to Home when navigating to Login page and user is logged in #70

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Username exposing (Username)
import Viewer exposing (Viewer)



-- NOTE: Based on discussions around how asset management features
-- like code splitting and lazy loading have been shaping up, it's possible
-- that most of this file may become unnecessary in a future release of Elm.
Expand All @@ -35,7 +36,7 @@ import Viewer exposing (Viewer)

type Model
= Redirect Session
| NotFound Session
| NotFound NotFound.Model
| Home Home.Model
| Settings Settings.Model
| Login Login.Model
Expand Down Expand Up @@ -120,6 +121,7 @@ type Msg
| GotArticleMsg Article.Msg
| GotEditorMsg Editor.Msg
| GotSession Session
| GotNotFoundMsg NotFound.Msg


toSession : Model -> Session
Expand All @@ -128,8 +130,8 @@ toSession page =
Redirect session ->
session

NotFound session ->
session
NotFound notFound ->
NotFound.toSession notFound

Home home ->
Home.toSession home
Expand Down Expand Up @@ -161,7 +163,8 @@ changeRouteTo maybeRoute model =
in
case maybeRoute of
Nothing ->
( NotFound session, Cmd.none )
NotFound.init session
|> updateWith NotFound GotNotFoundMsg model

Just Route.Root ->
( model, Route.replaceUrl (Session.navKey session) Route.Home )
Expand Down Expand Up @@ -247,6 +250,10 @@ update msg model =
Login.update subMsg login
|> updateWith Login GotLoginMsg model

( GotNotFoundMsg subMsg, NotFound notFound ) ->
NotFound.update subMsg notFound
|> updateWith NotFound GotNotFoundMsg model

( GotRegisterMsg subMsg, Register register ) ->
Register.update subMsg register
|> updateWith Register GotRegisterMsg model
Expand Down Expand Up @@ -291,8 +298,8 @@ updateWith toModel toMsg model ( subModel, subCmd ) =
subscriptions : Model -> Sub Msg
subscriptions model =
case model of
NotFound _ ->
Sub.none
NotFound notFound ->
Sub.map GotNotFoundMsg (NotFound.subscriptions notFound)

Redirect _ ->
Session.changes GotSession (Session.navKey (toSession model))
Expand Down
98 changes: 75 additions & 23 deletions src/Page/Login.elm
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import Http
import Json.Decode as Decode exposing (Decoder, decodeString, field, string)
import Json.Decode.Pipeline exposing (optional)
import Json.Encode as Encode
import Process
import Route exposing (Route)
import Session exposing (Session)
import Session exposing (Session, isLoggedIn)
import Task
import Username exposing (toString)
import Viewer exposing (Viewer)


Expand Down Expand Up @@ -63,17 +66,30 @@ type alias Form =
}


init : Session -> ( Model, Cmd msg )
initialModel : Session -> Model
initialModel session =
{ session = session
, problems = []
, form =
{ email = ""
, password = ""
}
}


init : Session -> ( Model, Cmd Msg )
init session =
( { session = session
, problems = []
, form =
{ email = ""
, password = ""
}
}
, Cmd.none
)
if Session.isLoggedIn session then
( initialModel session
, Process.sleep 3000
|> Task.andThen (always <| Task.succeed (GotSession session))
|> Task.perform identity
)

else
( initialModel session
, Cmd.none
)



Expand All @@ -84,23 +100,59 @@ view : Model -> { title : String, content : Html Msg }
view model =
{ title = "Login"
, content =
div [ class "cred-page" ]
[ div [ class "container page" ]
[ div [ class "row" ]
[ div [ class "col-md-6 offset-md-3 col-xs-12" ]
[ h1 [ class "text-xs-center" ] [ text "Sign in" ]
, p [ class "text-xs-center" ]
[ a [ Route.href Route.Register ]
[ text "Need an account?" ]
if Session.isLoggedIn model.session then
viewLoggedIn model.session

else
viewLoggedOut model
}


viewLoggedOut : Model -> Html Msg
viewLoggedOut model =
div [ class "cred-page" ]
[ div [ class "container page" ]
[ div [ class "row" ]
[ div [ class "col-md-6 offset-md-3 col-xs-12" ]
[ h1 [ class "text-xs-center" ] [ text "Sign in" ]
, p [ class "text-xs-center" ]
[ a [ Route.href Route.Register ]
[ text "Need an account?" ]
]
, ul [ class "error-messages" ]
(List.map viewProblem model.problems)
, viewForm model.form
]
]
]
]


viewLoggedIn : Session -> Html Msg
viewLoggedIn session =
div [ class "cred-page" ]
[ div [ class "container page" ]
[ div [ class "row" ]
[ div [ class "col-md-6 offset-md-3 col-xs-12" ]
[ h2 [ class "text-xs-center" ]
[ text <| "Hi " ++ getUserName session
, h3 [ class "text-xs-center" ]
[ text "Redirecting you to the Home Page"
]
, ul [ class "error-messages" ]
(List.map viewProblem model.problems)
, viewForm model.form
]
]
]
]
}
]


getUserName : Session -> String
getUserName session =
session
|> Session.viewer
|> Maybe.map Viewer.username
|> Maybe.map Username.toString
|> Maybe.withDefault "Donald Trump"


viewProblem : Problem -> Html msg
Expand Down
37 changes: 36 additions & 1 deletion src/Page/NotFound.elm
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
module Page.NotFound exposing (view)
module Page.NotFound exposing (Model, Msg, init, subscriptions, toSession, update, view)

import Asset
import Html exposing (Html, div, h1, img, main_, text)
import Html.Attributes exposing (alt, class, id, src, tabindex)
import Session exposing (Session)


type alias Model =
{ session : Session
}


type Msg
= GotSession Session


init : Session -> ( Model, Cmd Msg )
init session =
( { session = session
}
, Cmd.none
)


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
GotSession session ->
( { model | session = session }, Cmd.none )


toSession : Model -> Session
toSession model =
model.session



Expand All @@ -19,3 +49,8 @@ view =
[ img [ Asset.src Asset.error ] [] ]
]
}


subscriptions : Model -> Sub Msg
subscriptions model =
Session.changes GotSession (Session.navKey model.session)
12 changes: 11 additions & 1 deletion src/Session.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Session exposing (Session, changes, cred, fromViewer, navKey, viewer)
module Session exposing (Session, changes, cred, fromViewer, isLoggedIn, navKey, viewer)

import Api exposing (Cred)
import Avatar exposing (Avatar)
Expand Down Expand Up @@ -54,6 +54,16 @@ navKey session =
key


isLoggedIn : Session -> Bool
isLoggedIn session =
case session of
LoggedIn key _ ->
True

Guest key ->
False



-- CHANGES

Expand Down
21 changes: 11 additions & 10 deletions tests/RoutingTests.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module RoutingTests exposing (..)
module RoutingTests exposing (fragment, fromUrl, slugFromStr, testUrl, usernameFromStr)

import Article
import Article.Slug as Slug exposing (Slug)
Expand All @@ -10,21 +10,22 @@ import Url exposing (Url)
import Username exposing (Username)



-- TODO need to add lots more tests!


fromUrl : Test
fromUrl =
describe "Route.fromUrl"
[ testUrl "" Root
, testUrl "#login" Login
, testUrl "#logout" Logout
, testUrl "#settings" Settings
, testUrl "#profile/foo" (Profile (usernameFromStr "foo"))
, testUrl "#register" Register
, testUrl "#article/foo" (Article (slugFromStr "foo"))
, testUrl "#editor" NewArticle
, testUrl "#editor/foo" (EditArticle (slugFromStr "foo"))
[ testUrl "" Home
, testUrl "login" Login
, testUrl "logout" Logout
, testUrl "settings" Settings
, testUrl "profile/foo" (Profile (usernameFromStr "foo"))
, testUrl "register" Register
, testUrl "article/foo" (Article (slugFromStr "foo"))
, testUrl "editor" NewArticle
, testUrl "editor/foo" (EditArticle (slugFromStr "foo"))
]


Expand Down