diff --git a/src/Backend.elm b/src/Backend.elm index 55de21b..1f8a0b2 100644 --- a/src/Backend.elm +++ b/src/Backend.elm @@ -19,7 +19,6 @@ import Pages.Login import Pages.Profile.Username_ import Pages.Register import Pages.Settings -import Stubs exposing (..) import Task import Time import Time.Extra as Time @@ -41,22 +40,10 @@ app = init : ( Model, Cmd BackendMsg ) init = - let - articles = - [ ( stubArticle.slug, stubArticle ), ( stubArticle2.slug, stubArticle2 ) ] - |> Dict.fromList - in ( { sessions = Dict.empty - , users = stubUsersFull |> List.map (\u -> ( u.email, u )) |> Dict.fromList - , articles = articles - , comments = - articles - |> Dict.map - (\k a -> - stubComments - |> List.map (\c -> ( Time.posixToMillis c.createdAt, c )) - |> Dict.fromList - ) + , users = Dict.empty + , articles = Dict.empty + , comments = Dict.empty } , Cmd.none ) @@ -370,15 +357,15 @@ updateFromFrontend sessionId clientId msg model = model (\r -> send_ (PageMsg (Gen.Msg.Article__Slug_ (Pages.Article.Slug_.GotAuthor r)))) - UserAuthentication_Login { user } -> + UserAuthentication_Login { params } -> let ( response, cmd ) = model.users - |> Dict.get user.email + |> Dict.get params.email |> Maybe.map (\u -> - if u.password == user.password then - ( Success (Api.User.toUser u), renewSession user.email sessionId clientId ) + if u.password == params.password then + ( Success (Api.User.toUser u), renewSession params.email sessionId clientId ) else ( Failure [ "email or password is invalid" ], Cmd.none ) @@ -387,11 +374,53 @@ updateFromFrontend sessionId clientId msg model = in ( model, Cmd.batch [ send_ (PageMsg (Gen.Msg.Login (Pages.Login.GotUser response))), cmd ] ) - UserRegistration_Register { user } -> - send (PageMsg (Gen.Msg.Register (Pages.Register.GotUser (Success stubUser)))) + UserRegistration_Register { params } -> + let + ( model_, cmd, res ) = + if model.users |> Dict.member params.email then + ( model, Cmd.none, Failure [ "email address already taken" ] ) + + else + let + user_ = + { email = params.email + , username = params.username + , bio = Nothing + , image = "https://static.productionready.io/images/smiley-cyrus.jpg" + , password = params.password + , favorites = [] + , following = [] + } + in + ( { model | users = model.users |> Dict.insert user_.email user_ } + , renewSession params.email sessionId clientId + , Success (Api.User.toUser user_) + ) + in + ( model_, Cmd.batch [ cmd, send_ (PageMsg (Gen.Msg.Register (Pages.Register.GotUser res))) ] ) + + UserUpdate_Settings { params } -> + let + ( model_, res ) = + case model |> getSessionUser sessionId of + Just user -> + let + user_ = + { user + | username = params.username + + -- , email = params.email + , password = params.password |> Maybe.withDefault user.password + , image = params.image + , bio = Just params.bio + } + in + ( model |> updateUser user_, Success (Api.User.toUser user_) ) - UserUpdate_Settings { user } -> - send (PageMsg (Gen.Msg.Settings (Pages.Settings.GotUser (Success stubUser)))) + Nothing -> + ( model, Failure [ "you do not have permission for this user" ] ) + in + ( model_, send_ (PageMsg (Gen.Msg.Settings (Pages.Settings.GotUser res))) ) NoOpToBackend -> ( model, Cmd.none ) diff --git a/src/Bridge.elm b/src/Bridge.elm index 098ffd4..f2bf903 100644 --- a/src/Bridge.elm +++ b/src/Bridge.elm @@ -42,10 +42,10 @@ type ToBackend | ProfileUnfollow_Profile__Username_ { username : String } | ProfileFollow_Article__Slug_ { username : String } | ProfileUnfollow_Article__Slug_ { username : String } - | UserAuthentication_Login { user : { email : String, password : String } } - | UserRegistration_Register { user : { username : String, email : String, password : String } } + | UserAuthentication_Login { params : { email : String, password : String } } + | UserRegistration_Register { params : { username : String, email : String, password : String } } | UserUpdate_Settings - { user : + { params : { username : String , email : String , password : Maybe String diff --git a/src/Pages/Login.elm b/src/Pages/Login.elm index d5d3ce1..5edaa2e 100644 --- a/src/Pages/Login.elm +++ b/src/Pages/Login.elm @@ -83,7 +83,7 @@ update req msg model = ( model , (Effect.fromCmd << sendToBackend) <| UserAuthentication_Login - { user = + { params = { email = model.email , password = model.password } diff --git a/src/Pages/Register.elm b/src/Pages/Register.elm index a9bd409..dc2d666 100644 --- a/src/Pages/Register.elm +++ b/src/Pages/Register.elm @@ -91,7 +91,7 @@ update req msg model = ( model , (Effect.fromCmd << sendToBackend) <| UserRegistration_Register - { user = + { params = { username = model.username , email = model.email , password = model.password diff --git a/src/Pages/Settings.elm b/src/Pages/Settings.elm index 586440f..28b9dc5 100644 --- a/src/Pages/Settings.elm +++ b/src/Pages/Settings.elm @@ -108,7 +108,7 @@ update msg model = ( { model | message = Nothing, errors = [] } , (Effect.fromCmd << sendToBackend) <| UserUpdate_Settings - { user = + { params = { username = model.username , email = model.email , password = model.password diff --git a/src/Stubs.elm b/src/Stubs.elm deleted file mode 100644 index 3cbafb9..0000000 --- a/src/Stubs.elm +++ /dev/null @@ -1,142 +0,0 @@ -module Stubs exposing (..) - -import Api.Article exposing (Article, Listing) -import Api.Article.Comment exposing (Comment) -import Api.Profile exposing (Profile) -import Api.User exposing (..) -import Time - - -stubListing : Listing -stubListing = - { articles = [ stubArticle ] - , page = 1 - , totalPages = 1 - } - - -stubArticle : Article -stubArticle = - { slug = "stub" - , title = "stub" - , description = "stub" - , body = "stub" - , tags = [ "stub" ] - , createdAt = Time.millisToPosix 0 - , updatedAt = Time.millisToPosix 0 - , favorited = False - , favoritesCount = 123 - , author = stubProfile - } - - -stubArticle2 : Article -stubArticle2 = - { slug = "stub" - , title = "stub" - , description = "stub" - , body = "stub" - , tags = [ "stub" ] - , createdAt = Time.millisToPosix 0 - , updatedAt = Time.millisToPosix 0 - , favorited = False - , favoritesCount = 123 - , author = { stubProfile | username = "bob@bob.com" } - } - - -stubArticle_ : Int -> Article -stubArticle_ i = - let - iden = - "stub-" ++ String.fromInt i - in - { slug = iden - , title = iden - , description = "stub" - , body = iden - , tags = [ iden ] - , createdAt = Time.millisToPosix 1234567890123 - , updatedAt = Time.millisToPosix 1234567890123 - , favorited = False - , favoritesCount = 123 - , author = stubProfile - } - - -stubArticleCreate : { title : String, description : String, body : String, tags : List String } -> Article -stubArticleCreate p = - { slug = "stub" - , title = p.title - , description = p.description - , body = p.body - , tags = p.tags - , createdAt = Time.millisToPosix 0 - , updatedAt = Time.millisToPosix 0 - , favorited = False - , favoritesCount = 123 - , author = stubProfile - } - - -stubProfile : Profile -stubProfile = - { username = "test@test.com" - , bio = Just "testing" - , image = "https://static.productionready.io/images/smiley-cyrus.jpg" - , following = False - } - - -stubUser : User -stubUser = - { email = "test@test.com" - , username = "test@test.com" - , bio = Just "test bio" - , image = "https://static.productionready.io/images/smiley-cyrus.jpg" - } - - -stubUserFull : UserFull -stubUserFull = - { email = "test@test.com" - , username = "test@test.com" - , bio = Just "test bio" - , image = "https://static.productionready.io/images/smiley-cyrus.jpg" - , password = "test" - , favorites = [] - , following = [] - } - - -stubUserFull2 : UserFull -stubUserFull2 = - { email = "bob@bob.com" - , username = "bob@bob.com" - , bio = Just "just bob" - , image = "https://static.productionready.io/images/smiley-cyrus.jpg" - , password = "bob" - , favorites = [] - , following = [] - } - - -stubUsersFull : List UserFull -stubUsersFull = - [ stubUserFull - , stubUserFull2 - ] - - -stubComment : Comment -stubComment = - { id = 0 - , createdAt = Time.millisToPosix 0 - , updatedAt = Time.millisToPosix 0 - , body = "test comment" - , author = stubProfile - } - - -stubComments = - [ stubComment ]