aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/Signup.elm
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/src/Signup.elm')
-rw-r--r--frontend/src/Signup.elm194
1 files changed, 194 insertions, 0 deletions
diff --git a/frontend/src/Signup.elm b/frontend/src/Signup.elm
new file mode 100644
index 0000000..6395b57
--- /dev/null
+++ b/frontend/src/Signup.elm
@@ -0,0 +1,194 @@
1module Signup exposing (..)
2
3import Browser
4import Browser.Navigation as Nav
5import Html exposing (..)
6import Html.Attributes exposing (..)
7import Html.Events exposing (..)
8import Http
9import Json.Encode as Encode
10import Url
11import Url.Parser as P exposing ((</>), Parser, int, oneOf, s, string)
12
13
14type alias Model =
15 { username : String
16 , password : String
17 , phoneNumber : String
18 , emailId : String
19 , address : Maybe String
20 , status : Status
21 }
22
23
24type Status
25 = UsernameTaken
26 | InvalidPhone
27 | InvalidEmail
28 | CreatedSuccessfully
29 | CreatingUser
30 | Empty
31
32
33type Msg
34 = UserEntered String
35 | PassEntered String
36 | PhoneEntered String
37 | EmailEntered String
38 | AddressEntered String
39 | CreatePressed
40 | CreationSuccess (Result Http.Error ())
41 | UsernameExists (Result Http.Error String)
42 | CreationFail
43
44
45init : Model
46init =
47 Model "" "" "" "" Nothing Empty
48
49
50update : Msg -> Model -> ( Model, Cmd Msg )
51update msg model =
52 case msg of
53 UserEntered s ->
54 ( { model | username = s }
55 , Cmd.none
56 )
57
58 PassEntered s ->
59 ( { model | password = s }
60 , Cmd.none
61 )
62
63 PhoneEntered s ->
64 let
65 status =
66 if String.length s /= 10 || (List.all (not << Char.isDigit) <| String.toList s) then
67 InvalidPhone
68
69 else
70 Empty
71 in
72 ( { model | phoneNumber = s, status = status }
73 , Cmd.none
74 )
75
76 EmailEntered s ->
77 let
78 status =
79 if not <| String.contains "@" s then
80 InvalidEmail
81
82 else
83 Empty
84 in
85 ( { model | emailId = s, status = status }
86 , Cmd.none
87 )
88
89 AddressEntered s ->
90 ( { model | address = Just s }
91 , Cmd.none
92 )
93
94 CreatePressed ->
95 ( { model | status = CreatingUser }, checkExists model )
96
97 CreationSuccess res ->
98 case res of
99 Ok _ ->
100 ( { model | status = CreatedSuccessfully }, Cmd.none )
101
102 Err _ ->
103 ( model, Cmd.none )
104
105 CreationFail ->
106 ( init, Cmd.none )
107
108 UsernameExists res ->
109 case res of
110 Ok "true" ->
111 ( { model | status = UsernameTaken }, Cmd.none )
112
113 Ok "false" ->
114 let
115 _ =
116 Debug.log "signup" "Hit create user ..."
117 in
118 ( { model | status = CreatingUser }, createUser model )
119
120 _ ->
121 ( model, Cmd.none )
122
123
124encodeCreateUser : Model -> Encode.Value
125encodeCreateUser model =
126 Encode.object
127 [ ( "username", Encode.string model.username )
128 , ( "password", Encode.string model.password )
129 , ( "phone_number", Encode.string model.phoneNumber )
130 , ( "email_id", Encode.string model.emailId )
131 , ( "address", Encode.string <| Maybe.withDefault "" model.address )
132 ]
133
134
135checkExists : Model -> Cmd Msg
136checkExists model =
137 Http.post
138 { url = "http://127.0.0.1:7878/user/existing"
139 , body = Http.stringBody "application/json" model.username
140 , expect = Http.expectString UsernameExists
141 }
142
143
144createUser : Model -> Cmd Msg
145createUser model =
146 Http.riskyRequest
147 { method = "POST"
148 , headers = []
149 , url = "http://127.0.0.1:7878/user/new"
150 , body = model |> encodeCreateUser |> Http.jsonBody
151 , expect = Http.expectWhatever CreationSuccess
152 , timeout = Nothing
153 , tracker = Nothing
154 }
155
156
157viewStatus : Status -> String
158viewStatus s =
159 case s of
160 UsernameTaken ->
161 "This username is taken!"
162
163 InvalidPhone ->
164 "Invalid phone number!"
165
166 InvalidEmail ->
167 "Invalid email address!"
168
169 CreatedSuccessfully ->
170 "User created successfully"
171
172 CreatingUser ->
173 "Creating user ..."
174
175 Empty ->
176 ""
177
178
179viewInput : String -> String -> String -> (String -> msg) -> Html msg
180viewInput t p v toMsg =
181 input [ type_ t, placeholder p, value v, onInput toMsg ] []
182
183
184view : Model -> Html Msg
185view model =
186 div []
187 [ viewInput "text" "Enter Username" model.username UserEntered
188 , viewInput "password" "Password" model.password PassEntered
189 , viewInput "text" "Email" model.emailId EmailEntered
190 , viewInput "text" "Enter your Phone number" model.phoneNumber PhoneEntered
191 , viewInput "text" "Enter Shipping address" (Maybe.withDefault "" model.address) AddressEntered
192 , button [ onClick CreatePressed ] [ text "Create" ]
193 , text (viewStatus model.status)
194 ]