From 0d589e12a0c32936303de46b1e462dd19648170d Mon Sep 17 00:00:00 2001 From: Joris Guyonvarch Date: Sun, 19 Jul 2015 16:07:15 +0200 Subject: Login with a token validation --- src/client/Model/View.elm | 3 ++- src/client/Model/View/SignIn.elm | 15 +++++++++++++++ src/client/Update.elm | 15 +++++++++++---- src/client/Update/SignIn.elm | 15 +++++++++++++++ src/client/View/Page.elm | 22 ++++++++++++++++------ 5 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 src/client/Model/View/SignIn.elm create mode 100644 src/client/Update/SignIn.elm (limited to 'src/client') diff --git a/src/client/Model/View.elm b/src/client/Model/View.elm index ca819e3..3e3cbca 100644 --- a/src/client/Model/View.elm +++ b/src/client/Model/View.elm @@ -3,8 +3,9 @@ module Model.View ) where import Model.Payment exposing (Payments) +import Model.View.SignIn exposing (..) type View = LoadingView | PaymentView Payments - | SignInView String + | SignInView SignIn diff --git a/src/client/Model/View/SignIn.elm b/src/client/Model/View/SignIn.elm new file mode 100644 index 0000000..1c8eae7 --- /dev/null +++ b/src/client/Model/View/SignIn.elm @@ -0,0 +1,15 @@ +module Model.View.SignIn + ( SignIn + , initSignIn + ) where + +type alias SignIn = + { login : String + , authentication : Maybe (Result String String) + } + +initSignIn : SignIn +initSignIn = + { login = "" + , authentication = Nothing + } diff --git a/src/client/Update.elm b/src/client/Update.elm index 3937888..1d0fe95 100644 --- a/src/client/Update.elm +++ b/src/client/Update.elm @@ -7,11 +7,14 @@ module Update import Model exposing (Model) import Model.Payment exposing (Payments) import Model.View exposing (..) +import Model.View.SignIn exposing (..) + +import Update.SignIn exposing (..) type Action = NoOp | SignIn - | UpdateLogin String + | UpdateSignIn SignInAction | UpdatePayments Payments actions : Signal.Mailbox Action @@ -23,8 +26,12 @@ updateModel action model = NoOp -> model SignIn -> - { model | view <- SignInView "" } - UpdateLogin login -> - { model | view <- SignInView login } + { model | view <- SignInView initSignIn } + UpdateSignIn signInAction -> + case model.view of + SignInView signIn -> + { model | view <- SignInView (updateSignIn signInAction signIn) } + _ -> + model UpdatePayments payments -> { model | view <- PaymentView payments } diff --git a/src/client/Update/SignIn.elm b/src/client/Update/SignIn.elm new file mode 100644 index 0000000..a962f90 --- /dev/null +++ b/src/client/Update/SignIn.elm @@ -0,0 +1,15 @@ +module Update.SignIn + ( SignInAction(..) + , updateSignIn + ) where + +import Model.View.SignIn exposing (..) + +type SignInAction = + UpdateLogin String + +updateSignIn : SignInAction -> SignIn -> SignIn +updateSignIn action signIn = + case action of + UpdateLogin login -> + { signIn | login <- login } diff --git a/src/client/View/Page.elm b/src/client/View/Page.elm index 1683cf3..eb86132 100644 --- a/src/client/View/Page.elm +++ b/src/client/View/Page.elm @@ -13,11 +13,14 @@ import Date exposing (Date) import String exposing (append) +import Json.Decode as Json + import Model exposing (Model) import Model.Payment exposing (Payments, Payment) import Model.View exposing (..) import Update exposing (..) +import Update.SignIn exposing (..) import ServerCommunication as SC import ServerCommunication exposing (serverCommunications) @@ -38,7 +41,7 @@ renderHeader model = [] [ h1 [] - [ text "Payments" ] + [ text "Shared Cost" ] , case model.view of LoadingView -> text "" @@ -57,7 +60,7 @@ renderMain model = case model.view of LoadingView -> loadingView - SignInView login -> + SignInView { login } -> signInView login PaymentView payments -> paymentsView payments @@ -67,18 +70,25 @@ loadingView = text "" signInView : String -> Html signInView login = - H.form + div [ class "signIn" ] [ input [ value login - , on "input" targetValue (Signal.message actions.address << UpdateLogin) + , on "input" targetValue (Signal.message actions.address << UpdateSignIn << UpdateLogin) + , onEnter serverCommunications.address (SC.SignIn login) ] [] , button [ onClick serverCommunications.address (SC.SignIn login) ] - [ renderIcon "sign-in" ] + [ text "Sign in" ] ] +onEnter : Signal.Address a -> a -> Attribute +onEnter address value = + on "keydown" + (Json.customDecoder keyCode (\code -> if code == 13 then Ok () else Err "")) + (\_ -> Signal.message address value) + paymentsView : Payments -> Html paymentsView payments = table @@ -112,5 +122,5 @@ paymentLine payment = renderDate : Date -> String renderDate date = toString (Date.day date) - |> flip append (" " ++ (toString (Date.month date))) + |> flip append (" " ++ (toString (Date.month date)) ++ ".") |> flip append (" " ++ (toString (Date.year date))) -- cgit v1.2.3