docspell/modules/webapp/src/main/elm/Comp/ChangePasswordForm.elm

261 lines
6.7 KiB
Elm
Raw Normal View History

2019-12-29 20:55:12 +00:00
module Comp.ChangePasswordForm exposing
( Model
, Msg(..)
, emptyModel
, update
, view
)
import Api
import Api.Model.BasicResult exposing (BasicResult)
2019-12-29 20:55:12 +00:00
import Api.Model.PasswordChange exposing (PasswordChange)
import Data.Flags exposing (Flags)
2019-12-29 20:55:12 +00:00
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick, onInput)
import Http
import Util.Http
type alias Model =
2019-12-29 20:55:12 +00:00
{ current : String
, newPass1 : String
, newPass2 : String
, showCurrent : Bool
, showPass1 : Bool
, showPass2 : Bool
, errors : List String
, loading : Bool
, successMsg : String
}
2019-12-29 20:55:12 +00:00
emptyModel : Model
emptyModel =
validateModel
2019-12-29 20:55:12 +00:00
{ current = ""
, newPass1 = ""
, newPass2 = ""
, showCurrent = False
, showPass1 = False
, showPass2 = False
, errors = []
, loading = False
, successMsg = ""
}
type Msg
= SetCurrent String
| SetNew1 String
| SetNew2 String
| ToggleShowPass1
| ToggleShowPass2
| ToggleShowCurrent
| Submit
| SubmitResp (Result Http.Error BasicResult)
2019-12-29 20:55:12 +00:00
validate : Model -> List String
validate model =
List.concat
2019-12-29 20:55:12 +00:00
[ if model.newPass1 /= "" && model.newPass2 /= "" && model.newPass1 /= model.newPass2 then
[ "New passwords do not match." ]
else
[]
, if model.newPass1 == "" || model.newPass2 == "" || model.current == "" then
[ "Please fill in required fields." ]
else
[]
]
2019-12-29 20:55:12 +00:00
validateModel : Model -> Model
validateModel model =
let
2019-12-29 20:55:12 +00:00
err =
validate model
in
2019-12-29 20:55:12 +00:00
{ model
| errors = err
, successMsg =
if err == [] then
model.successMsg
else
""
}
-- Update
2019-12-29 20:55:12 +00:00
update : Flags -> Msg -> Model -> ( Model, Cmd Msg )
update flags msg model =
case msg of
SetCurrent s ->
2019-12-29 20:55:12 +00:00
( validateModel { model | current = s }, Cmd.none )
SetNew1 s ->
2019-12-29 20:55:12 +00:00
( validateModel { model | newPass1 = s }, Cmd.none )
SetNew2 s ->
2019-12-29 20:55:12 +00:00
( validateModel { model | newPass2 = s }, Cmd.none )
ToggleShowCurrent ->
2019-12-29 20:55:12 +00:00
( { model | showCurrent = not model.showCurrent }, Cmd.none )
ToggleShowPass1 ->
2019-12-29 20:55:12 +00:00
( { model | showPass1 = not model.showPass1 }, Cmd.none )
ToggleShowPass2 ->
2019-12-29 20:55:12 +00:00
( { model | showPass2 = not model.showPass2 }, Cmd.none )
Submit ->
let
2019-12-29 20:55:12 +00:00
valid =
validate model
cp =
PasswordChange model.current model.newPass1
in
2019-12-29 20:55:12 +00:00
if List.isEmpty valid then
( { model | loading = True, errors = [], successMsg = "" }, Api.changePassword flags cp SubmitResp )
else
( model, Cmd.none )
SubmitResp (Ok res) ->
let
2019-12-29 20:55:12 +00:00
em =
{ emptyModel | errors = [], successMsg = "Password has been changed." }
in
2019-12-29 20:55:12 +00:00
if res.success then
( em, Cmd.none )
else
( { model | errors = [ res.message ], loading = False, successMsg = "" }, Cmd.none )
SubmitResp (Err err) ->
let
2019-12-29 20:55:12 +00:00
str =
Util.Http.errorToString err
in
2019-12-29 20:55:12 +00:00
( { model | errors = [ str ], loading = False, successMsg = "" }, Cmd.none )
-- View
2019-12-29 20:55:12 +00:00
view : Model -> Html Msg
view model =
2019-12-29 20:55:12 +00:00
div
[ classList
[ ( "ui form", True )
, ( "error", List.isEmpty model.errors |> not )
, ( "success", model.successMsg /= "" )
]
]
2019-12-29 20:55:12 +00:00
[ div
[ classList
[ ( "field", True )
, ( "error", model.current == "" )
]
]
[ label [] [ text "Current Password*" ]
, div [ class "ui action input" ]
[ input
[ type_ <|
if model.showCurrent then
"text"
else
"password"
, onInput SetCurrent
, value model.current
]
[]
, button [ class "ui icon button", onClick ToggleShowCurrent ]
[ i [ class "eye icon" ] []
]
]
]
, div
[ classList
[ ( "field", True )
, ( "error", model.newPass1 == "" )
]
]
[ label [] [ text "New Password*" ]
, div [ class "ui action input" ]
[ input
[ type_ <|
if model.showPass1 then
"text"
else
"password"
, onInput SetNew1
, value model.newPass1
]
[]
, button [ class "ui icon button", onClick ToggleShowPass1 ]
[ i [ class "eye icon" ] []
]
]
]
, div
[ classList
[ ( "field", True )
, ( "error", model.newPass2 == "" )
]
]
[ label [] [ text "New Password (repeat)*" ]
, div [ class "ui action input" ]
[ input
[ type_ <|
if model.showPass2 then
"text"
else
"password"
, onInput SetNew2
, value model.newPass2
]
[]
, button [ class "ui icon button", onClick ToggleShowPass2 ]
[ i [ class "eye icon" ] []
]
]
]
, div [ class "ui horizontal divider" ] []
, div [ class "ui success message" ]
[ text model.successMsg
]
, div [ class "ui error message" ]
[ case model.errors of
a :: [] ->
text a
_ ->
ul [ class "ui list" ]
(List.map (\em -> li [] [ text em ]) model.errors)
]
2019-12-29 20:55:12 +00:00
, div [ class "ui horizontal divider" ] []
, button [ class "ui primary button", onClick Submit ]
[ text "Submit"
]
2019-12-29 20:55:12 +00:00
, div
[ classList
[ ( "ui dimmer", True )
, ( "active", model.loading )
]
]
2019-12-29 20:55:12 +00:00
[ div [ class "ui loader" ] []
]
]