Skip to content

Commit 910453e

Browse files
committed
refactor: Client Editor
1 parent 0372952 commit 910453e

File tree

1 file changed

+33
-80
lines changed

1 file changed

+33
-80
lines changed

app/clients/view.py

+33-80
Original file line numberDiff line numberDiff line change
@@ -164,65 +164,62 @@ def __init__(
164164
)
165165

166166
self.first_name_field = views.TTextField(
167-
on_change=self.on_fname_changed,
168167
label="First Name",
169168
hint=self.invoicing_contact.first_name,
170169
initial_value=self.invoicing_contact.first_name,
171170
)
172171

173172
self.last_name_field = views.TTextField(
174-
on_change=self.on_lname_changed,
175173
label="Last Name",
176174
hint=self.invoicing_contact.last_name,
177175
initial_value=self.invoicing_contact.last_name,
178176
)
179177
self.company_field = views.TTextField(
180-
on_change=self.on_company_changed,
181178
label="Company",
182179
hint=self.invoicing_contact.company,
183180
initial_value=self.invoicing_contact.company,
184181
)
185182
self.email_field = views.TTextField(
186-
on_change=self.on_email_changed,
187183
label="Email",
188184
hint=self.invoicing_contact.email,
189185
initial_value=self.invoicing_contact.email,
190186
)
191187

192188
self.street_field = views.TTextField(
193-
on_change=self.on_street_changed,
194189
label="Street",
195190
hint=self.invoicing_contact.address.street,
196191
initial_value=self.invoicing_contact.address.street,
197192
width=half_of_pop_up_width,
198193
)
199194
self.street_num_field = views.TTextField(
200-
on_change=self.on_street_num_changed,
201195
label="Street No.",
202196
hint=self.invoicing_contact.address.number,
203197
initial_value=self.invoicing_contact.address.number,
204198
width=half_of_pop_up_width,
205199
)
206200
self.postal_code_field = views.TTextField(
207-
on_change=self.on_postal_code_changed,
208201
label="Postal code",
209202
hint=self.invoicing_contact.address.postal_code,
210203
initial_value=self.invoicing_contact.address.postal_code,
211204
width=half_of_pop_up_width,
212205
)
213206
self.city_field = views.TTextField(
214-
on_change=self.on_city_changed,
215207
label="City",
216208
hint=self.invoicing_contact.address.city,
217209
initial_value=self.invoicing_contact.address.city,
218210
width=half_of_pop_up_width,
219211
)
220212
self.country_field = views.TTextField(
221-
on_change=self.on_country_changed,
222213
label="Country",
223214
hint=self.invoicing_contact.address.country,
224215
initial_value=self.invoicing_contact.address.country,
225216
)
217+
self.client_name_field = views.TTextField(
218+
label="Client's name",
219+
hint=self.client.name,
220+
initial_value=self.client.name,
221+
)
222+
226223
self.contacts_dropdown = views.TDropDown(
227224
on_change=self.on_contact_selected,
228225
label="Select contact",
@@ -241,12 +238,7 @@ def __init__(
241238
views.Spacer(xs_space=True),
242239
self.form_error_field,
243240
views.Spacer(xs_space=True),
244-
views.TTextField(
245-
on_change=self.on_client_name_changed,
246-
label="Client's name",
247-
hint=self.client.name,
248-
initial_value=self.client.name,
249-
),
241+
self.client_name_field,
250242
views.Spacer(xs_space=True),
251243
views.THeading(
252244
title="Invoicing Contact",
@@ -282,16 +274,6 @@ def __init__(
282274
],
283275
)
284276
super().__init__(dialog=dialog, dialog_controller=dialog_controller)
285-
self.title = ""
286-
self.fname = ""
287-
self.lname = ""
288-
self.company = ""
289-
self.email = ""
290-
self.street = ""
291-
self.street_num = ""
292-
self.postal_code = ""
293-
self.city = ""
294-
self.country = ""
295277
self.on_submit_callback = on_submit
296278
self.on_error_callback = on_error
297279
self.form_error = ""
@@ -305,20 +287,18 @@ def get_contacts_as_list(self):
305287
contacts_list.append(item)
306288
return contacts_list
307289

308-
def get_contact_dropdown_item(self, key):
290+
def get_contact_dropdown_item(self, contact_id):
309291
"""appends an id to the contact name for dropdown options"""
310-
if key is not None and key in self.contacts_as_map:
311-
return f"#{key} {self.contacts_as_map[key].name}"
292+
if contact_id is not None and contact_id in self.contacts_as_map:
293+
return f"{contact_id}. {self.contacts_as_map[contact_id].name}"
312294
return ""
313295

314296
def on_contact_selected(self, e):
315297
# parse selected value to extract id
316298
selected = e.control.value
317299
id = ""
318300
for c in selected:
319-
if c == "#":
320-
continue
321-
if c == " ":
301+
if c == ".":
322302
break
323303
id = id + c
324304
if int(id) in self.contacts_as_map:
@@ -343,86 +323,59 @@ def toggle_form_error(self):
343323
self.form_error_field.visible = True if self.form_error else False
344324
self.dialog.update()
345325

346-
def on_client_name_changed(self, e):
347-
self.title = e.control.value
348-
349-
def on_fname_changed(self, e):
350-
self.fname = e.control.value
351-
352-
def on_lname_changed(self, e):
353-
self.lname = e.control.value
354-
355-
def on_company_changed(self, e):
356-
self.company = e.control.value
357-
358-
def on_email_changed(self, e):
359-
self.email = e.control.value
360-
361-
def on_street_changed(self, e):
362-
self.street = e.control.value
363-
364-
def on_street_num_changed(self, e):
365-
self.street_num = e.control.value
366-
367-
def on_postal_code_changed(self, e):
368-
self.postal_code = e.control.value
369-
370-
def on_city_changed(self, e):
371-
self.city = e.control.value
372-
373-
def on_country_changed(self, e):
374-
self.country = e.control.value
375-
376326
def on_submit_btn_clicked(self, e):
377327
"""validates the form and calls the on_submit callback"""
378328
self.form_error = ""
379329
self.toggle_form_error()
380330

331+
# get values from fields
332+
client_name = self.client_name_field.value.strip()
333+
first_name = self.first_name_field.value.strip()
334+
last_name = self.last_name_field.value.strip()
335+
company = self.company_field.value.strip()
336+
email = self.email_field.value.strip()
337+
street = self.street_field.value.strip()
338+
street_num = self.street_num_field.value.strip()
339+
postal_code = self.postal_code_field.value.strip()
340+
city = self.city_field.value.strip()
341+
country = self.country_field.value.strip()
342+
381343
self.client.name = (
382-
self.title.strip() if self.title.strip() else self.client.name
344+
client_name if client_name else self.client.name
383345
)
384346
self.invoicing_contact.first_name = (
385-
self.fname.strip()
386-
if self.fname.strip()
387-
else self.invoicing_contact.first_name
347+
first_name if first_name else self.invoicing_contact.first_name
388348
)
389349
self.invoicing_contact.last_name = (
390-
self.lname.strip()
391-
if self.lname.strip()
350+
last_name if last_name
392351
else self.invoicing_contact.last_name
393352
)
394353
self.invoicing_contact.company = (
395-
self.company.strip()
396-
if self.company.strip()
354+
company if company
397355
else self.invoicing_contact.company
398356
)
399357
self.invoicing_contact.email = (
400-
self.email.strip() if self.email.strip() else self.invoicing_contact.email
358+
email if email else self.invoicing_contact.email
401359
)
402360
self.address.street = (
403-
self.street.strip()
404-
if self.street.strip()
361+
street if street
405362
else self.invoicing_contact.address.street
406363
)
407364

408365
self.address.number = (
409-
self.street_num.strip()
410-
if self.street_num.strip()
366+
street_num if street_num
411367
else self.invoicing_contact.address.number
412368
)
413369
self.address.postal_code = (
414-
self.postal_code.strip()
415-
if self.postal_code.strip()
370+
postal_code if postal_code
416371
else self.invoicing_contact.address.postal_code
417372
)
418373
self.address.city = (
419-
self.city.strip()
420-
if self.city.strip()
374+
city if city
421375
else self.invoicing_contact.address.city
422376
)
423377
self.address.country = (
424-
self.country.strip()
425-
if self.country.strip()
378+
country if country
426379
else self.invoicing_contact.address.country
427380
)
428381
self.invoicing_contact.address = self.address

0 commit comments

Comments
 (0)