aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/Login.elm
blob: dd168f02f52d8b6feb31cff4333d8d501700e43a (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
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
module Login exposing (..)

import Browser
import Browser.Navigation as Nav
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Http
import Json.Encode as Encode
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 ->
            "Not Logged In"

        InvalidLogin ->
            "Invalid Login"

        LoggedIn ->
            "Logged in!"

        LoggingIn ->
            "Logging In ..."


viewInput : String -> String -> String -> (String -> msg) -> Html msg
viewInput t p v toMsg =
    input [ type_ t, placeholder p, value v, onInput toMsg ] []


view : Model -> Html Msg
view model =
    div []
        [ div [] [ viewInput "text" "Enter name here" model.username UserEntered ]
        , div [] [ viewInput "password" "Password" model.password PassEntered ]
        , div [] [ button [ onClick LoginPressed ] [ text "Login" ] ]
        , div [] [ text (viewStatus model.loginStatus) ]
        , div [] [ text "Don't have an account? ", a [ href "/signup" ] [ text "Register now!" ] ]
        ]