diff options
Diffstat (limited to 'src/ui/ui_contacts.cpp')
| -rw-r--r-- | src/ui/ui_contacts.cpp | 163 |
1 files changed, 25 insertions, 138 deletions
diff --git a/src/ui/ui_contacts.cpp b/src/ui/ui_contacts.cpp index 92dd21a..b471f58 100644 --- a/src/ui/ui_contacts.cpp +++ b/src/ui/ui_contacts.cpp @@ -18,6 +18,7 @@ #include <stdlib.h> #include "strops.hpp" +#include "config.hpp" #include "ui.hpp" #include "imgui.h" #include "administration.hpp" @@ -29,76 +30,15 @@ static contact selected_for_removal; static contact active_contact; -void ui_draw_address_form(address* buffer) +void ui_draw_address_form(address* buffer, a_err last_err) { - const char* selected_country = NULL; - float widthAvailable = ImGui::GetContentRegionAvail().x; - - char id[MAX_LEN_ADDRESS]; - - ImGui::SetNextItemWidth(widthAvailable*0.5f); - snprintf(id, sizeof(id), "%s##%p", localize("contact.form.address1"), buffer); - ImGui::InputTextWithHint(id, localize("contact.form.address1"), buffer->address1, IM_ARRAYSIZE(buffer->address1)); - ImGui::SameLine();ui_helper_draw_required_tag(); - - ImGui::SetNextItemWidth(widthAvailable*0.5f); - snprintf(id, sizeof(id), "%s##%p", localize("contact.form.address2"), buffer); - ImGui::InputTextWithHint(id, localize("contact.form.address2"), buffer->address2, IM_ARRAYSIZE(buffer->address2)); - //ImGui::SameLine();ui_helper_draw_required_tag(); - - ImGui::SetNextItemWidth(widthAvailable*0.5f); - snprintf(id, sizeof(id), "%s##%p", localize("contact.form.city"), buffer); - ImGui::InputTextWithHint(id, localize("contact.form.city"), buffer->city, IM_ARRAYSIZE(buffer->city)); - ImGui::SameLine();ui_helper_draw_required_tag(); - - ImGui::SetNextItemWidth(widthAvailable*0.5f); - snprintf(id, sizeof(id), "%s##%p", localize("contact.form.postal"), buffer); - ImGui::InputTextWithHint(id, localize("contact.form.postal"), buffer->postal, IM_ARRAYSIZE(buffer->postal)); - ImGui::SameLine();ui_helper_draw_required_tag(); - - ImGui::SetNextItemWidth(widthAvailable*0.5f); - snprintf(id, sizeof(id), "%s##%p", localize("contact.form.region"), buffer); - ImGui::InputTextWithHint(id, localize("contact.form.region"), buffer->region, IM_ARRAYSIZE(buffer->region)); - ImGui::SameLine();ui_helper_draw_required_tag(); - - // 5. Country dropdown. - ImGui::SetNextItemWidth(widthAvailable*0.5f); - const char* countries[] = { localize("country.AT"),localize("country.BE"),localize("country.BG"),localize("country.HR"),localize("country.CY"),localize("country.CZ"),localize("country.DK"),localize("country.EE"),localize("country.FI"),localize("country.FR"),localize("country.DE"),localize("country.GR"),localize("country.HU"),localize("country.IE"),localize("country.IT"),localize("country.LV"),localize("country.LT"),localize("country.LU"),localize("country.MT"),localize("country.NL"),localize("country.PL"),localize("country.PT"),localize("country.RO"),localize("country.SK"),localize("country.SI"),localize("country.ES"),localize("country.SE") }; - const char* country_codes[] = { - "AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR", - "DE", "GR", "HU", "IE", "IT", "LV", "LT", "LU", "MT", "NL", - "PL", "PT", "RO", "SK", "SI", "ES", "SE" - }; - s32 country_count = sizeof(countries) / sizeof(countries[0]); - if (selected_country == 0) { - for (int i = 0; i < country_count; i++) - { - if (strcmp(country_codes[i], buffer->country_code) == 0) - { - selected_country = countries[i]; - break; - } - } - } + ImGui::FormInputTextWithErrorHint(localize("contact.form.address1"), buffer, buffer->address1, IM_ARRAYSIZE(buffer->address1), last_err & A_ERR_MISSING_ADDRESS1); + ImGui::FormInputTextWithErrorHint(localize("contact.form.address2"), buffer, buffer->address2, IM_ARRAYSIZE(buffer->address2), 0); + ImGui::FormInputTextWithErrorHint(localize("contact.form.city"), buffer, buffer->city, IM_ARRAYSIZE(buffer->city), last_err & A_ERR_MISSING_CITY); + ImGui::FormInputTextWithErrorHint(localize("contact.form.postal"), buffer, buffer->postal, IM_ARRAYSIZE(buffer->postal), last_err & A_ERR_MISSING_POSTAL); + ImGui::FormInputTextWithErrorHint(localize("contact.form.region"), buffer, buffer->region, IM_ARRAYSIZE(buffer->region), 0); - int selected_country_index = -1; - snprintf(id, sizeof(id), "%s##%p", localize("contact.form.country"), buffer); - if (ImGui::BeginCombo(id, selected_country)) - { - for (int n = 0; n < IM_ARRAYSIZE(countries); n++) - { - bool is_selected = (selected_country == countries[n]); - if (ImGui::Selectable(countries[n], is_selected)) { - selected_country = countries[n]; - selected_country_index = n; - } - } - ImGui::EndCombo(); - } - if (selected_country_index != -1) { - strops_copy(buffer->country_code, country_codes[selected_country_index], IM_ARRAYSIZE(buffer->country_code)); - } - ImGui::SameLine();ui_helper_draw_required_tag(); + ImGui::FormCountryCombo(buffer, buffer->country_code, IM_ARRAYSIZE(buffer->country_code)); } void ui_setup_contacts() @@ -108,89 +48,34 @@ void ui_setup_contacts() memset(&selected_for_removal, 0, sizeof(contact)); } -void draw_contact_form_ex(contact* buffer, bool viewing_only = false, bool with_autocomplete = false, bool* on_autocomplete = 0) +void draw_contact_form_ex(contact* buffer, bool viewing_only = false, bool with_autocomplete = false) { + a_err last_err = administration_contact_is_valid(*buffer); + ImGui::PushID(buffer); ImGui::Spacing(); - float widthAvailable = ImGui::GetContentRegionAvail().x; - ImGui::BeginDisabled(); - // 1. Identifier - //ImGui::SetNextItemWidth(widthAvailable*0.2f); - //ImGui::InputText(localize("contact.form.identifier"), buffer->id, IM_ARRAYSIZE(buffer->id)); - if (!viewing_only) ImGui::EndDisabled(); - // 2. Full name - ImGui::SetNextItemWidth(widthAvailable*0.5f); - if (with_autocomplete) { - contact autocomplete_list[5]; - int autocomplete_count = administration_contact_get_autocompletions(autocomplete_list, 5, buffer->name); - char* autocomplete_strings[5]; - - for (int i = 0; i < autocomplete_count; i++) - { - autocomplete_strings[i] = (char*)malloc(200); - snprintf(autocomplete_strings[i], 200, "%s (%s %s)", autocomplete_list[i].name, autocomplete_list[i].address.address1, autocomplete_list[i].address.address2); - } - - int autocomplete_index = TextInputWithAutocomplete(localize("contact.form.fullname"), localize("contact.form.fullname"), - buffer->name, IM_ARRAYSIZE(buffer->name), (char**)autocomplete_strings, autocomplete_count); - - if (on_autocomplete) { - *on_autocomplete = autocomplete_index != -1; - } + if (with_autocomplete) ImGui::FormContactAutocomplete(buffer, last_err & A_ERR_MISSING_NAME); + else ImGui::FormInputTextWithErrorHint(localize("contact.form.fullname"), buffer, buffer->name, IM_ARRAYSIZE(buffer->name), last_err & A_ERR_MISSING_NAME); - if (autocomplete_index != -1) - { - memcpy(buffer, &autocomplete_list[autocomplete_index], sizeof(contact)); - } + ui_draw_address_form(&buffer->address, last_err); - for (int i = 0; i < autocomplete_count; i++) - { - free(autocomplete_strings[i]); - } - } - else ImGui::InputTextWithHint(localize("contact.form.fullname"), localize("contact.form.fullname"), buffer->name, IM_ARRAYSIZE(buffer->name)); - ImGui::SameLine();ui_helper_draw_required_tag(); - - // 3. Address line 1, 4. address line 2, 5. country - ui_draw_address_form(&buffer->address); - - // 6. Contact type dropdown. - ImGui::SetNextItemWidth(widthAvailable*0.5f); - const char* customer_types[2] = { localize("contact.form.type.business"), localize("contact.form.type.consumer") }; - int currentItem = static_cast<int>(buffer->type); - if (ImGui::Combo(localize("contact.form.type"), ¤tItem, customer_types, IM_ARRAYSIZE(customer_types))) - { - buffer->type = static_cast<contact_type>(currentItem); - } + ImGui::FormContactTypeCombo(&buffer->type); // Fields only required for businesses. if (buffer->type == contact_type::CONTACT_BUSINESS) { - // 7. Tax number - ImGui::SetNextItemWidth(widthAvailable*0.5f); - ImGui::InputTextWithHint(localize("contact.form.taxnumber"), localize("contact.form.taxnumber"), buffer->taxid, IM_ARRAYSIZE(buffer->taxid)); - - // 8. Business number / Chamber of commerce - ImGui::SetNextItemWidth(widthAvailable*0.5f); - ImGui::InputTextWithHint(localize("contact.form.businessnumber"), localize("contact.form.businessnumber"), buffer->businessid, IM_ARRAYSIZE(buffer->businessid)); + ImGui::FormInputTextWithErrorHint(localize("contact.form.taxnumber"), buffer, buffer->taxid, IM_ARRAYSIZE(buffer->taxid), last_err & A_ERR_MISSING_TAXID); + ImGui::FormInputTextWithErrorHint(localize("contact.form.businessnumber"), buffer, buffer->businessid, IM_ARRAYSIZE(buffer->businessid), last_err & A_ERR_MISSING_BUSINESSID); } - // 9. Email - ImGui::SetNextItemWidth(widthAvailable*0.5f); - ImGui::InputTextWithHint(localize("contact.form.email"), localize("contact.form.email"), buffer->email, IM_ARRAYSIZE(buffer->email)); - - // 10. Phone number - ImGui::SetNextItemWidth(widthAvailable*0.5f); - ImGui::InputTextWithHint(localize("contact.form.phonenumber"), localize("contact.form.phonenumber"), buffer->phone_number, IM_ARRAYSIZE(buffer->phone_number)); - - // 11. Bank account. - ImGui::SetNextItemWidth(widthAvailable*0.5f); - ImGui::InputTextWithHint(localize("contact.form.bankaccount"), localize("contact.form.bankaccount"), buffer->bank_account, IM_ARRAYSIZE(buffer->bank_account)); + ImGui::FormInputTextWithErrorHint(localize("contact.form.email"), buffer, buffer->email, IM_ARRAYSIZE(buffer->email), last_err & A_ERR_MISSING_EMAIL); + ImGui::FormInputTextWithErrorHint(localize("contact.form.phonenumber"), buffer, buffer->phone_number, IM_ARRAYSIZE(buffer->phone_number), 0); + ImGui::FormInputTextWithErrorHint(localize("contact.form.bankaccount"), buffer, buffer->bank_account, IM_ARRAYSIZE(buffer->bank_account), 0); if (viewing_only) ImGui::EndDisabled(); ImGui::PopID(); @@ -198,7 +83,7 @@ void draw_contact_form_ex(contact* buffer, bool viewing_only = false, bool with_ void draw_contact_form(contact* buffer, bool viewing_only = false) { - draw_contact_form_ex(buffer, viewing_only, false, 0); + draw_contact_form_ex(buffer, viewing_only, false); } @@ -313,7 +198,8 @@ static void ui_draw_contacts_create() draw_contact_form(&active_contact); - bool can_save = administration_contact_is_valid(active_contact) == A_ERR_SUCCESS; + a_err contact_validation_err = administration_contact_is_valid(active_contact); + bool can_save = contact_validation_err == A_ERR_SUCCESS; if (!can_save) ImGui::BeginDisabled(); // Save button ImGui::Spacing(); @@ -332,7 +218,8 @@ static void ui_draw_contacts_update() draw_contact_form(&active_contact); - bool can_save = administration_contact_is_valid(active_contact) == A_ERR_SUCCESS; + a_err contact_validation_err = administration_contact_is_valid(active_contact); + bool can_save = contact_validation_err == A_ERR_SUCCESS; if (!can_save) ImGui::BeginDisabled(); // Save button ImGui::Spacing(); |
