Commit f9f7a373 authored by Darya Rednikina's avatar Darya Rednikina
Browse files

Merge branch 'login/errors' into 'develop'

Login/errors - displaying input errors according to related issue

Closes #1

See merge request !1
parents 644d5eda 0c5614c9
This diff is collapsed.
......@@ -5,6 +5,7 @@ import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Route exposing (..)
import Validation as V exposing (..)
init : ( Model, Cmd Msg )
......@@ -32,11 +33,6 @@ type alias Form =
}
type Problem
= InvalidEntry String
| ServerError String
-- UPDATE
......@@ -60,6 +56,18 @@ update msg model =
EnteredPassword password ->
updateForm (\form -> { form | password = password }) model
SubmittedForm ->
case validate model.form of
Ok validForm ->
( { model | problems = [] }
, Cmd.none
)
Err problems ->
( { model | problems = problems }
, Cmd.none
)
_ ->
( model, Cmd.none )
......@@ -122,15 +130,58 @@ view model =
]
viewProblem : Problem -> Html msg
viewProblem problem =
-- VALIDATION
type TrimmedForm
= Trimmed Form
trimFields : Form -> TrimmedForm
trimFields form =
Trimmed
{ email = String.trim form.email
, password = String.trim form.password
}
fieldsToValidate : List ValidatedField
fieldsToValidate =
[ Email
, Password
]
validate : Form -> Result (List Problem) TrimmedForm
validate form =
let
trimmedForm =
trimFields form
in
case List.concatMap (validateField trimmedForm) fieldsToValidate of
[] ->
Ok trimmedForm
problems ->
Err problems
validateField : TrimmedForm -> ValidatedField -> List Problem
validateField (Trimmed form) field =
let
errorMessage =
case problem of
InvalidEntry _ ->
"f"
emptyValidation : String -> String -> List String
emptyValidation str msg =
if String.isEmpty str then
[ msg ]
ServerError str ->
str
else
[]
in
li [] [ text errorMessage ]
List.map (V.InvalidEntry field) <|
case field of
Email ->
emptyValidation form.email "email can't be blank."
_ ->
emptyValidation form.password "password can't be blank."
......@@ -54,6 +54,18 @@ update msg model =
in
m1
( RegisterMsg registerMsg, RegisterPage m ) ->
let
( updatedPageModel, updatedCmd ) =
Register.update registerMsg m
m1 =
( { model | page = RegisterPage updatedPageModel }
, Cmd.map RegisterMsg updatedCmd
)
in
m1
( LinkClicked urlReq, _ ) ->
case urlReq of
Browser.Internal url ->
......
......@@ -4,7 +4,9 @@ import Browser exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import List exposing (..)
import Route exposing (..)
import Validation as V exposing (Problem, ValidatedField)
......@@ -17,11 +19,6 @@ type alias Model =
}
type Problem
= InvalidEntry String
| ServerError String
type alias Form =
{ login : String
, email : String
......@@ -69,6 +66,18 @@ update msg model =
EnteredName name ->
updateForm (\form -> { form | name = name }) model
SubmittedForm ->
case validate model.form of
Ok validForm ->
( { model | problems = [] }
, Cmd.none
)
Err problems ->
( { model | problems = problems }
, Cmd.none
)
_ ->
( model, Cmd.none )
......@@ -135,11 +144,101 @@ view model =
[ a [ Route.href Route.Login ]
[ text "Already have an account?" ]
]
-- , ul [ class "error-messages" ]
-- (List.map viewProblem model.problems)
, ul [ class "error-messages" ]
(List.map V.viewProblem model.problems)
, viewForm model.form
]
]
]
]
-- VALIDATION
{-| to not send unvalidated fields
-}
type TrimmedForm
= Trimmed Form
fieldsToValidate : List ValidatedField
fieldsToValidate =
[ V.Email
, V.Password
, V.Login
, V.Name
]
trimFields : Form -> TrimmedForm
trimFields form =
Trimmed
{ email = String.trim form.email
, password = String.trim form.password
, login = String.trim form.login
, name = String.trim form.name
}
{-| Trim the form and validate its fields.
-}
validate : Form -> Result (List Problem) TrimmedForm
validate form =
let
trimmedForm =
trimFields form
in
case List.concatMap (validateField trimmedForm) fieldsToValidate of
[] ->
Ok trimmedForm
problems ->
Err problems
{-| Checks all the fields for errors.
-}
validateField : TrimmedForm -> ValidatedField -> List Problem
validateField (Trimmed form) field =
let
emptyValidation str msg =
if String.isEmpty str then
[ msg ]
else
[]
lengthValidation : String -> (Int -> Bool) -> String -> List String
lengthValidation str p msg =
if p (String.length str) then
[]
else
[ msg ]
passwordValidation : String -> List String
passwordValidation pswd =
let
errEmpty =
emptyValidation pswd "password can't be empty."
errLength =
lengthValidation pswd (\x -> x > 8) "password must be > 8."
in
List.append errEmpty errLength
in
List.map (V.InvalidEntry field) <|
case field of
V.Email ->
emptyValidation form.email "email can't be blank."
V.Password ->
passwordValidation form.password
V.Login ->
emptyValidation form.login "login can't be blank."
V.Name ->
emptyValidation form.name "name can't be blank."
......@@ -38,7 +38,7 @@ href targetRoute =
routeToString : Route -> String
routeToString page =
"#/" ++ String.join "/" (routeToPieces page)
String.join "/" (routeToPieces page)
routeToPieces : Route -> List String
......
module Validation exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
type Problem
= InvalidEntry ValidatedField String
| ServerError String
type ValidatedField
= Email
| Password
| Login
| Name
viewProblem : Problem -> Html msg
viewProblem problem =
let
errorMessage =
case problem of
InvalidEntry _ msg ->
msg
ServerError str ->
str
in
li [] [ text errorMessage ]
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment