diff --git a/src/Main.elm b/src/Main.elm index 55b21a1672..7f2263edc6 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. @@ -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 @@ -120,6 +121,7 @@ type Msg | GotArticleMsg Article.Msg | GotEditorMsg Editor.Msg | GotSession Session + | GotNotFoundMsg NotFound.Msg toSession : Model -> Session @@ -128,8 +130,8 @@ toSession page = Redirect session -> session - NotFound session -> - session + NotFound notFound -> + NotFound.toSession notFound Home home -> Home.toSession home @@ -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 ) @@ -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 @@ -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)) 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 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) 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 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")) ]