summaryrefslogtreecommitdiff
path: root/elm/User/PassSet.elm
blob: 9ad10748d98fced66030f230de362e36b9231bdd (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
module User.PassSet exposing (main)

import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Browser
import Browser.Navigation exposing (load)
import Lib.Api as Api
import Gen.Api as GApi
import Gen.UserPassSet as GUPS
import Lib.Html exposing (..)


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


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


init : GUPS.Recv -> Model
init f =
  { token    = f.token
  , uid      = f.uid
  , newpass1 = ""
  , newpass2 = ""
  , state    = Api.Normal
  , noteq    = False
  }


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 }
           , GUPS.send { token = model.token, uid = model.uid, password = model.newpass1 } 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 GUPS.valPassword ]
      , formField "newpass2::Repeat"
        [ inputPassword "newpass2" model.newpass2 Newpass2 GUPS.valPassword
        , 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 ]
    ]
  ]