Commit 0c5614c9 authored by Darya Rednikina's avatar Darya Rednikina
Browse files

(#1) A little bit of decomposition, validation in login page

parent e8e5625e
......@@ -10893,6 +10893,57 @@ var $author$project$Login$updateForm = F2(
}),
$elm$core$Platform$Cmd$none);
});
var $author$project$Validation$Email = {$: 'Email'};
var $author$project$Validation$Password = {$: 'Password'};
var $author$project$Login$fieldsToValidate = _List_fromArray(
[$author$project$Validation$Email, $author$project$Validation$Password]);
var $author$project$Login$Trimmed = function (a) {
return {$: 'Trimmed', a: a};
};
var $elm$core$String$trim = _String_trim;
var $author$project$Login$trimFields = function (form) {
return $author$project$Login$Trimmed(
{
email: $elm$core$String$trim(form.email),
password: $elm$core$String$trim(form.password)
});
};
var $author$project$Validation$InvalidEntry = F2(
function (a, b) {
return {$: 'InvalidEntry', a: a, b: b};
});
var $author$project$Login$validateField = F2(
function (_v0, field) {
var form = _v0.a;
var emptyValidation = F2(
function (str, msg) {
return $elm$core$String$isEmpty(str) ? _List_fromArray(
[msg]) : _List_Nil;
});
return A2(
$elm$core$List$map,
$author$project$Validation$InvalidEntry(field),
function () {
if (field.$ === 'Email') {
return A2(emptyValidation, form.email, 'email can\'t be blank.');
} else {
return A2(emptyValidation, form.password, 'password can\'t be blank.');
}
}());
});
var $author$project$Login$validate = function (form) {
var trimmedForm = $author$project$Login$trimFields(form);
var _v0 = A2(
$elm$core$List$concatMap,
$author$project$Login$validateField(trimmedForm),
$author$project$Login$fieldsToValidate);
if (!_v0.b) {
return $elm$core$Result$Ok(trimmedForm);
} else {
var problems = _v0;
return $elm$core$Result$Err(problems);
}
};
var $author$project$Login$update = F2(
function (msg, model) {
switch (msg.$) {
......@@ -10916,6 +10967,23 @@ var $author$project$Login$update = F2(
{password: password});
},
model);
case 'SubmittedForm':
var _v1 = $author$project$Login$validate(model.form);
if (_v1.$ === 'Ok') {
var validForm = _v1.a;
return _Utils_Tuple2(
_Utils_update(
model,
{problems: _List_Nil}),
$elm$core$Platform$Cmd$none);
} else {
var problems = _v1.a;
return _Utils_Tuple2(
_Utils_update(
model,
{problems: problems}),
$elm$core$Platform$Cmd$none);
}
default:
return _Utils_Tuple2(model, $elm$core$Platform$Cmd$none);
}
......@@ -10930,16 +10998,13 @@ var $author$project$Register$updateForm = F2(
}),
$elm$core$Platform$Cmd$none);
});
var $author$project$Register$Email = {$: 'Email'};
var $author$project$Register$Login = {$: 'Login'};
var $author$project$Register$Name = {$: 'Name'};
var $author$project$Register$Password = {$: 'Password'};
var $author$project$Validation$Login = {$: 'Login'};
var $author$project$Validation$Name = {$: 'Name'};
var $author$project$Register$fieldsToValidate = _List_fromArray(
[$author$project$Register$Email, $author$project$Register$Password, $author$project$Register$Login, $author$project$Register$Name]);
[$author$project$Validation$Email, $author$project$Validation$Password, $author$project$Validation$Login, $author$project$Validation$Name]);
var $author$project$Register$Trimmed = function (a) {
return {$: 'Trimmed', a: a};
};
var $elm$core$String$trim = _String_trim;
var $author$project$Register$trimFields = function (form) {
return $author$project$Register$Trimmed(
{
......@@ -10949,10 +11014,6 @@ var $author$project$Register$trimFields = function (form) {
password: $elm$core$String$trim(form.password)
});
};
var $author$project$Register$InvalidEntry = F2(
function (a, b) {
return {$: 'InvalidEntry', a: a, b: b};
});
var $author$project$Register$validateField = F2(
function (_v0, field) {
var form = _v0.a;
......@@ -10980,7 +11041,7 @@ var $author$project$Register$validateField = F2(
};
return A2(
$elm$core$List$map,
$author$project$Register$InvalidEntry(field),
$author$project$Validation$InvalidEntry(field),
function () {
switch (field.$) {
case 'Email':
......@@ -11265,10 +11326,11 @@ var $author$project$Login$viewForm = function (form) {
]))
]));
};
var $author$project$Login$viewProblem = function (problem) {
var $author$project$Validation$viewProblem = function (problem) {
var errorMessage = function () {
if (problem.$ === 'InvalidEntry') {
return 'f';
var msg = problem.b;
return msg;
} else {
var str = problem.a;
return str;
......@@ -11350,7 +11412,7 @@ var $author$project$Login$view = function (model) {
[
$elm$html$Html$Attributes$class('error-messages')
]),
A2($elm$core$List$map, $author$project$Login$viewProblem, model.problems)),
A2($elm$core$List$map, $author$project$Validation$viewProblem, model.problems)),
$author$project$Login$viewForm(model.form)
]))
]))
......@@ -11470,24 +11532,6 @@ var $author$project$Register$viewForm = function (form) {
]))
]));
};
var $author$project$Register$viewProblem = function (problem) {
var errorMessage = function () {
if (problem.$ === 'InvalidEntry') {
var msg = problem.b;
return msg;
} else {
var str = problem.a;
return str;
}
}();
return A2(
$elm$html$Html$li,
_List_Nil,
_List_fromArray(
[
$elm$html$Html$text(errorMessage)
]));
};
var $author$project$Register$view = function (model) {
return A2(
$elm$html$Html$div,
......@@ -11556,7 +11600,7 @@ var $author$project$Register$view = function (model) {
[
$elm$html$Html$Attributes$class('error-messages')
]),
A2($elm$core$List$map, $author$project$Register$viewProblem, model.problems)),
A2($elm$core$List$map, $author$project$Validation$viewProblem, model.problems)),
$author$project$Register$viewForm(model.form)
]))
]))
......
......@@ -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."
......@@ -6,6 +6,7 @@ import Html.Attributes exposing (..)
import Html.Events exposing (..)
import List exposing (..)
import Route exposing (..)
import Validation as V exposing (Problem, ValidatedField)
......@@ -18,11 +19,6 @@ type alias Model =
}
type Problem
= InvalidEntry ValidatedField String
| ServerError String
type alias Form =
{ login : String
, email : String
......@@ -149,7 +145,7 @@ view model =
[ text "Already have an account?" ]
]
, ul [ class "error-messages" ]
(List.map viewProblem model.problems)
(List.map V.viewProblem model.problems)
, viewForm model.form
]
]
......@@ -167,19 +163,12 @@ type TrimmedForm
= Trimmed Form
type ValidatedField
= Email
| Password
| Login
| Name
fieldsToValidate : List ValidatedField
fieldsToValidate =
[ Email
, Password
, Login
, Name
[ V.Email
, V.Password
, V.Login
, V.Name
]
......@@ -240,30 +229,16 @@ validateField (Trimmed form) field =
in
List.append errEmpty errLength
in
List.map (InvalidEntry field) <|
List.map (V.InvalidEntry field) <|
case field of
Email ->
V.Email ->
emptyValidation form.email "email can't be blank."
Password ->
V.Password ->
passwordValidation form.password
Login ->
V.Login ->
emptyValidation form.login "login can't be blank."
Name ->
V.Name ->
emptyValidation form.name "name can't be blank."
viewProblem : Problem -> Html msg
viewProblem problem =
let
errorMessage =
case problem of
InvalidEntry _ msg ->
msg
ServerError str ->
str
in
li [] [ text errorMessage ]
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