From e41c13713021a7f1ccef1ade321a51ca6d81f9e1 Mon Sep 17 00:00:00 2001 From: Martin Bryant Date: Wed, 13 Feb 2019 18:31:06 +0000 Subject: [PATCH 1/4] added isLoggedIn function --- src/Session.elm | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Session.elm b/src/Session.elm index 8b5436e504..200ae994b3 100644 --- a/src/Session.elm +++ b/src/Session.elm @@ -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) @@ -54,6 +54,16 @@ navKey session = key +isLoggedIn : Session -> Bool +isLoggedIn session = + case session of + LoggedIn key _ -> + True + + Guest key -> + False + + -- CHANGES From 3e14bf2379cf22fd6b17fff0e4649a99d2ad27d3 Mon Sep 17 00:00:00 2001 From: Martin Bryant Date: Wed, 13 Feb 2019 18:32:44 +0000 Subject: [PATCH 2/4] added checks on init and view for isLoggedIn check when on Login page --- src/Page/Login.elm | 98 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 75 insertions(+), 23 deletions(-) diff --git a/src/Page/Login.elm b/src/Page/Login.elm index 31bab51450..8f9aa4413c 100644 --- a/src/Page/Login.elm +++ b/src/Page/Login.elm @@ -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) @@ -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 + ) @@ -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 From 844bd6c3e4db70de522ca15d666c5e23cfa0ff31 Mon Sep 17 00:00:00 2001 From: Martin Bryant Date: Wed, 13 Feb 2019 19:23:59 +0000 Subject: [PATCH 3/4] all tests passing and ran elm-format on both files --- src/Main.elm | 1 + tests/RoutingTests.elm | 21 +++++++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Main.elm b/src/Main.elm index 55b21a1672..893018794a 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -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. diff --git a/tests/RoutingTests.elm b/tests/RoutingTests.elm index b096a19993..04d55244ab 100644 --- a/tests/RoutingTests.elm +++ b/tests/RoutingTests.elm @@ -1,4 +1,4 @@ -module RoutingTests exposing (..) +module RoutingTests exposing (fragment, fromUrl, slugFromStr, testUrl, usernameFromStr) import Article import Article.Slug as Slug exposing (Slug) @@ -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")) ] From 6f94d15575fdbb12ba1590e2f6291b51116789df Mon Sep 17 00:00:00 2001 From: Martin Bryant Date: Thu, 14 Feb 2019 15:51:28 +0000 Subject: [PATCH 4/4] added NotFound route subscription to track session changes --- src/Main.elm | 18 ++++++++++++------ src/Page/NotFound.elm | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/Main.elm b/src/Main.elm index 893018794a..7f2263edc6 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -36,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 @@ -121,6 +121,7 @@ type Msg | GotArticleMsg Article.Msg | GotEditorMsg Editor.Msg | GotSession Session + | GotNotFoundMsg NotFound.Msg toSession : Model -> Session @@ -129,8 +130,8 @@ toSession page = Redirect session -> session - NotFound session -> - session + NotFound notFound -> + NotFound.toSession notFound Home home -> Home.toSession home @@ -162,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 ) @@ -248,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 @@ -292,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)) diff --git a/src/Page/NotFound.elm b/src/Page/NotFound.elm index e0c534b732..55ed2bdb52 100644 --- a/src/Page/NotFound.elm +++ b/src/Page/NotFound.elm @@ -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 @@ -19,3 +49,8 @@ view = [ img [ Asset.src Asset.error ] [] ] ] } + + +subscriptions : Model -> Sub Msg +subscriptions model = + Session.changes GotSession (Session.navKey model.session)