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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
module Signup 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
, phoneNumber : String
, emailId : String
, address : Maybe String
, status : Status
}
type Status
= UsernameTaken
| InvalidPhone
| InvalidEmail
| CreatedSuccessfully
| CreatingUser
| Empty
type Msg
= UserEntered String
| PassEntered String
| PhoneEntered String
| EmailEntered String
| AddressEntered String
| CreatePressed
| CreationSuccess (Result Http.Error ())
| UsernameExists (Result Http.Error String)
| CreationFail
init : Model
init =
Model "" "" "" "" Nothing Empty
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
UserEntered s ->
( { model | username = s }
, Cmd.none
)
PassEntered s ->
( { model | password = s }
, Cmd.none
)
PhoneEntered s ->
let
status =
if String.length s /= 10 || (List.all (not << Char.isDigit) <| String.toList s) then
InvalidPhone
else
Empty
in
( { model | phoneNumber = s, status = status }
, Cmd.none
)
EmailEntered s ->
let
status =
if not <| String.contains "@" s then
InvalidEmail
else
Empty
in
( { model | emailId = s, status = status }
, Cmd.none
)
AddressEntered s ->
( { model | address = Just s }
, Cmd.none
)
CreatePressed ->
( { model | status = CreatingUser }, checkExists model )
CreationSuccess res ->
case res of
Ok _ ->
( { model | status = CreatedSuccessfully }, Cmd.none )
Err _ ->
( model, Cmd.none )
CreationFail ->
( init, Cmd.none )
UsernameExists res ->
case res of
Ok "true" ->
( { model | status = UsernameTaken }, Cmd.none )
Ok "false" ->
let
_ =
Debug.log "signup" "Hit create user ..."
in
( { model | status = CreatingUser }, createUser model )
_ ->
( model, Cmd.none )
encodeCreateUser : Model -> Encode.Value
encodeCreateUser model =
Encode.object
[ ( "username", Encode.string model.username )
, ( "password", Encode.string model.password )
, ( "phone_number", Encode.string model.phoneNumber )
, ( "email_id", Encode.string model.emailId )
, ( "address", Encode.string <| Maybe.withDefault "" model.address )
]
checkExists : Model -> Cmd Msg
checkExists model =
Http.post
{ url = "http://127.0.0.1:7878/user/existing"
, body = Http.stringBody "application/json" model.username
, expect = Http.expectString UsernameExists
}
createUser : Model -> Cmd Msg
createUser model =
Http.riskyRequest
{ method = "POST"
, headers = []
, url = "http://127.0.0.1:7878/user/new"
, body = model |> encodeCreateUser |> Http.jsonBody
, expect = Http.expectWhatever CreationSuccess
, timeout = Nothing
, tracker = Nothing
}
viewStatus : Status -> String
viewStatus s =
case s of
UsernameTaken ->
"This username is taken!"
InvalidPhone ->
"Invalid phone number!"
InvalidEmail ->
"Invalid email address!"
CreatedSuccessfully ->
"User created successfully"
CreatingUser ->
"Creating user ..."
Empty ->
""
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 Username" model.username UserEntered ]
, div [] [ viewInput "password" "Password" model.password PassEntered ]
, div [] [ viewInput "text" "Email" model.emailId EmailEntered ]
, div [] [ viewInput "text" "Enter your Phone number" model.phoneNumber PhoneEntered ]
, div [] [ viewInput "text" "Enter Shipping address" (Maybe.withDefault "" model.address) AddressEntered ]
, div [] [ button [ onClick CreatePressed ] [ text "Create" ] ]
, div []
[ text "Already have a account? "
, a [ href "/login" ] [ text "Login >" ]
]
, text (viewStatus model.status)
]
|