Merge: doc: fixed some typos and other misc. corrections
[nit.git] / contrib / benitlux / src / client / views / user_views.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # User preference window and other user-related view
16 module user_views
17
18 import base
19
20 redef class Label
21 # Update the content of `lbl_welcome`
22 fun set_user_name
23 do
24 var name = app.user
25 self.text = if name != null then
26 "Logged in as %0".t.format(name)
27 else "Not logged in".t
28 end
29
30 # Set `text` to welcome an authentified user or invite to authentify
31 fun set_welcome
32 do
33 var name = app.user
34 self.text = if name != null then
35 "Welcome %0".t.format(name)
36 else ""
37 end
38 end
39
40 # User preference window
41 class UserWindow
42 super Window
43
44 # Main window layout
45 var layout = new ListLayout(parent=self)
46
47 private var layout_user_options = new VerticalLayout(parent=layout)
48
49 private var user_options_header = new SectionHeader(parent=layout_user_options)
50 private var lbl_user_options_title = new SectionTitle(parent=user_options_header,
51 text="Account options".t)
52
53 var lbl_welcome = new DescLabel(parent=layout_user_options)
54 private var but_logout = new Button(parent=layout_user_options, text="Logout".t)
55
56 # Refesh displayed text
57 fun refresh
58 do
59 lbl_welcome.set_user_name
60 but_logout.enabled = app.user != null
61 end
62
63 init do refresh
64
65 redef fun on_event(event)
66 do
67 if event isa ButtonPressEvent then
68 var sender = event.sender
69 if sender == but_logout then
70 app.user = null
71 app.token = "none"
72 app.on_log_in
73 refresh
74 end
75 end
76
77 super
78 end
79 end
80
81 # Window for signing up a new user or logging in
82 class SignupWindow
83 super Window
84
85 private var list = new ListLayout(parent=self)
86
87 private var login_header = new SectionHeader(parent=list)
88 private var login_title = new SectionTitle(parent=login_header, text="Login".t)
89
90 # Layout for the top form
91 var layout_login = new SignupForm(parent=list)
92
93 # ---
94 # First the login options
95
96 # Name
97 private var name_line = new HorizontalLayout(parent=layout_login)
98 private var lbl_name = new Label(parent=name_line, text="Username".t)
99 private var txt_name = new TextInput(parent=name_line, text=app.user)
100
101 # Password
102 private var pass_line = new HorizontalLayout(parent=layout_login)
103 private var lbl_pass = new Label(parent=pass_line, text="Password".t)
104 private var txt_pass = new TextInput(parent=pass_line, is_password=true)
105 private var lbl_pass_desc = new DescLabel(parent=layout_login, size = 0.5,
106 text="Passwords must be composed of at least 6 characters.".t)
107
108 private var but_login = new Button(parent=layout_login, text="Login".t)
109
110 private var lbl_feedback = new DescLabel(parent=layout_login, text=" ")
111
112 # ---
113 # Then, the signup options
114
115 private var signup_header = new SectionHeader(parent=list)
116 private var signup_title = new SectionTitle(parent=signup_header, text="Signup".t)
117
118 # Layout for the 2nd form
119 var layout_signup = new SignupForm(parent=list)
120
121 private var lbl_signup_desc = new DescLabel(parent=layout_signup, size = 0.5,
122 text="Fill the following fields to sign up.".t)
123
124 # Repeat password
125 private var pass_line2 = new HorizontalLayout(parent=layout_signup)
126 private var lbl_pass2 = new Label(parent=pass_line2, text="Repeat password".t)
127 private var txt_pass2 = new TextInput(parent=pass_line2, is_password=true)
128
129 # Email
130 private var email_line = new HorizontalLayout(parent=layout_signup)
131 private var lbl_email = new Label(parent=email_line, text="Email".t)
132 private var txt_email = new TextInput(parent=email_line)
133
134 private var but_signup = new Button(parent=layout_signup, text="Signup".t)
135
136 redef fun on_event(event)
137 do
138 if debug then print "BenitluxWindow::on_event {event}"
139
140 if event isa ButtonPressEvent then
141 var sender = event.sender
142 if sender == but_login or sender == but_signup then
143
144 var name = txt_name.text
145 if name == null or not name.name_is_ok then
146 feedback "Invalid username.".t
147 return
148 end
149
150 var pass = txt_pass.text
151 if pass == null or not pass.pass_is_ok then
152 feedback "Invalid password.".t
153 return
154 end
155
156 if sender == but_login then
157 feedback "Logging in...".t
158 (new LoginOrSignupAction(self, "rest/login?name={name.to_percent_encoding}&pass={pass.pass_hash}")).start
159 else if sender == but_signup then
160 if pass != txt_pass2.text then
161 feedback "Passwords do not match.".t
162 return
163 end
164
165 var email = txt_email.text
166 if email == null or email.is_empty then
167 feedback "Invalid email".t
168 return
169 end
170
171 feedback "Signing up...".t
172 (new LoginOrSignupAction(self, "rest/signup?name={name.to_percent_encoding}&pass={pass.pass_hash}&email={email.to_percent_encoding}")).start
173 end
174 end
175 end
176
177 super
178 end
179
180 # Show lasting feedback to the user in a label
181 fun feedback(text: String) do lbl_feedback.text = text
182 end
183
184 # Form for login or signup
185 class SignupForm
186 super VerticalLayout
187 super ItemView
188 end
189
190 # ---
191 # Async RESTful actions
192
193 # Async request for login in or signing up
194 class LoginOrSignupAction
195 super WindowHttpRequest
196
197 redef type W: SignupWindow
198
199 init do affected_views.add_all([window.but_login, window.but_signup])
200
201 redef fun on_load(res, status)
202 do
203 if intercept_error(res) then return
204
205 if not res isa LoginResult then
206 on_fail new Error("Server sent unexpected data {res or else "null"}")
207 return
208 end
209
210 app.token = res.token
211 app.user = res.user.name
212
213 app.on_log_in
214 end
215
216 redef fun feedback(text) do window.feedback text
217 end