summaryrefslogtreecommitdiff
path: root/elm/User/PassSet.elm
blob: bc5cc24d1d0f7522a9ef97542a6a7eafbbc803b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
module User.PassSet exposing (main)

import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Json.Encode as JE
import Browser
import Browser.Navigation exposing (load)
import Lib.Api as Api
import Gen.Api as GApi
import Gen.UserEdit as GUE
import Lib.Html exposing (..)


main : Program String Model Msg
main = Browser.element
  { init = \url -> (init url, Cmd.none)
  , subscriptions = always Sub.none
  , view = view
  , update = update
  }


type alias Model =
  { url      : String
  , newpass1 : String
  , newpass2 : String
  , state    : Api.State
  , noteq    : Bool
  }


init : String -> Model
init url =
  { url      = url
  , newpass1 = ""
  , newpass2 = ""
  , state    = Api.Normal
  , noteq    = False
  }


encodeForm : Model -> JE.Value
encodeForm o = JE.object
  [ ("password", JE.string o.newpass1) ]


type Msg
  = Newpass1 String
  | Newpass2 String
  | Submit
  | Submitted GApi.Response


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    Newpass1 n -> ({ model | newpass1 = n, noteq = False }, Cmd.none)
    Newpass2 n -> ({ model | newpass2 = n, noteq = False }, Cmd.none)

    Submit ->
      if model.newpass1 /= model.newpass2
      then ( { model | noteq = True }, Cmd.none)
      else ( { model | state = Api.Loading }
           , Api.post model.url (encodeForm model) Submitted )

    Submitted GApi.Success -> (model, load "/")
    Submitted e            -> ({ model | state = Api.Error e }, Cmd.none)


view : Model -> Html Msg
view model =
  form_ Submit (model.state == Api.Loading)
  [ div [ class "mainbox" ]
    [ h1 [] [ text "Set your password" ]
    , p [] [ text "Now you can set a password for your account. You will be logged in automatically after your password has been saved." ]
    , table [ class "formtable" ]
      [ formField "newpass1::New password" [ inputPassword "newpass1" model.newpass1 Newpass1 GUE.valPasswordNew ]
      , formField "newpass2::Repeat"
        [ inputPassword "newpass2" model.newpass2 Newpass2 GUE.valPasswordNew
        , br_ 1
        , if model.noteq then b [ class "standout" ] [ text "Passwords do not match" ] else text ""
        ]
      ]
   ]
  , div [ class "mainbox" ]
    [ fieldset [ class "submit" ] [ submitButton "Submit" model.state True ]
    ]
  ]