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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
module Login exposing (..)
import Browser
import Browser.Navigation as Nav
import Css exposing (..)
import Html
import Html.Styled exposing (..)
import Html.Styled.Attributes exposing (..)
import Html.Styled.Events exposing (..)
import Http
import Icons exposing (..)
import Json.Encode as Encode
import Styles exposing (..)
import Url
import Url.Parser as P exposing ((</>), Parser, int, oneOf, s, string)
type alias Model =
{ username : String
, password : String
, loginStatus : LoginStatus
}
type LoginStatus
= NotLoggedIn
| LoggedIn
| InvalidLogin
| LoggingIn
type Msg
= PassEntered String
| UserEntered String
| LoginPressed
| LoginSuccess (Result Http.Error ())
| LoginFail
init : Model
init =
Model "" "" NotLoggedIn
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
PassEntered s ->
( { model | password = s }
, Cmd.none
)
UserEntered s ->
( { model | username = s }
, Cmd.none
)
LoginPressed ->
( { model | loginStatus = LoggingIn }, tryLogin model )
LoginSuccess res ->
case res of
Ok s ->
( { model | loginStatus = LoggedIn }, Cmd.none )
Err e ->
( { model | loginStatus = InvalidLogin }, Cmd.none )
LoginFail ->
( { model | loginStatus = InvalidLogin }, Cmd.none )
encodeLogin : Model -> Encode.Value
encodeLogin model =
Encode.object
[ ( "username", Encode.string model.username )
, ( "password", Encode.string model.password )
]
tryLogin : Model -> Cmd Msg
tryLogin model =
Http.riskyRequest
{ method = "POST"
, headers = []
, url = "http://127.0.0.1:7878/user/login"
, body = model |> encodeLogin |> Http.jsonBody
, expect = Http.expectWhatever LoginSuccess
, timeout = Nothing
, tracker = Nothing
}
viewStatus : LoginStatus -> String
viewStatus ls =
case ls of
NotLoggedIn ->
""
InvalidLogin ->
"Invalid Login"
LoggedIn ->
"Logged in!"
LoggingIn ->
"Logging In ..."
viewInput : String -> String -> String -> (String -> msg) -> Html msg
viewInput t p v toMsg =
loginInputField [ type_ t, placeholder p, value v, onInput toMsg ] []
fieldPadding =
css
[ paddingTop (px 10)
, paddingBottom (px 10)
]
view : Model -> Html Msg
view model =
div
[ css
[ margin auto
, marginTop (pct 10)
, padding (px 20)
, Css.width (pct 30)
]
]
[ div [ fieldPadding, css [ bigHeading ] ] [ text "Login" ]
, div [ fieldPadding ] [ viewInput "text" "Enter name here" model.username UserEntered ]
, div [ fieldPadding ] [ viewInput "password" "Password" model.password PassEntered ]
, div [ css [ textAlign center ], fieldPadding ] [ furbyButton [ onClick LoginPressed ] [ text "Login" ] ]
, div [ css [ textAlign center ] ] [ text (viewStatus model.loginStatus) ]
, div [ fieldPadding ] [ text "Don't have an account? ", a [ href "/signup" ] [ text "Register now!" ] ]
]
|