summaryrefslogtreecommitdiff
path: root/src/views/contacts.cpp
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrikboy@gmail.com>2025-08-08 22:04:47 +0200
committerAldrik Ramaekers <aldrikboy@gmail.com>2025-08-08 22:04:47 +0200
commitb94a7ae06b20d550c727d5192cea8baf3e8fb641 (patch)
tree3258eb27e2e3f266f8c220662ebb24ebe3071b79 /src/views/contacts.cpp
parent21496e32695744d4679fc11105352c61522ce601 (diff)
project crud
Diffstat (limited to 'src/views/contacts.cpp')
-rw-r--r--src/views/contacts.cpp72
1 files changed, 21 insertions, 51 deletions
diff --git a/src/views/contacts.cpp b/src/views/contacts.cpp
index 4b352bc..31f95e3 100644
--- a/src/views/contacts.cpp
+++ b/src/views/contacts.cpp
@@ -3,56 +3,26 @@
#include "views.hpp"
#include "imgui.h"
#include "../administration.hpp"
+#include "../locales/locales.hpp"
-typedef enum {
- LIST,
- EDIT,
- CREATE,
- VIEW,
-} contact_view_state;
-
-static contact_view_state view_state = LIST;
+static view_state current_view_state = LIST;
static contact selected_for_removal;
static contact active_contact;
-static void draw_required_tag()
-{
- ImDrawList* draw_list = ImGui::GetWindowDrawList();
-
- const char* text = localize("form.required");
- ImVec2 text_pos = ImGui::GetCursorScreenPos();
- ImVec2 text_size = ImGui::CalcTextSize(text);
- text_pos.y += text_size.y/4.0f;
-
- ImVec4 bg_color = ImVec4(0.9f, 0.235f, 0.235f, 0.4f); // Red background
- ImVec4 text_color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); // White text
- float rounding = 2.0f;
- float padding = 2.0f;
-
- // Background rectangle
- ImVec2 bg_min = ImVec2(text_pos.x - padding, text_pos.y - padding);
- ImVec2 bg_max = ImVec2(text_pos.x + text_size.x + padding, text_pos.y + text_size.y + padding);
- draw_list->AddRectFilled(bg_min, bg_max, ImColor(bg_color), rounding);
-
- // Foreground text
- ImGui::PushStyleColor(ImGuiCol_Text, text_color);
- ImGui::TextUnformatted(text);
- ImGui::PopStyleColor();
-}
-
static void draw_contact_form()
{
static const char* selected_country = NULL;
if (ImGui::Button(localize("form.back"))) {
- view_state = contact_view_state::LIST;
+ current_view_state = view_state::LIST;
memset(&active_contact, 0, sizeof(contact));
selected_country = 0;
+ return;
}
ImGui::Spacing();
- bool viewing_only = (view_state == contact_view_state::VIEW);
+ bool viewing_only = (current_view_state == view_state::VIEW);
ImGui::BeginDisabled();
@@ -64,15 +34,15 @@ static void draw_contact_form()
ImGui::SetNextItemWidth(widthAvailable*0.5f);
ImGui::InputTextWithHint(localize("contact.form.fullname"), localize("contact.form.fullname"), active_contact.name, IM_ARRAYSIZE(active_contact.name));
- ImGui::SameLine();draw_required_tag();
+ ImGui::SameLine();view_draw_required_tag();
ImGui::SetNextItemWidth(widthAvailable*0.5f);
ImGui::InputTextWithHint(localize("contact.form.address1"), localize("contact.form.address1"), active_contact.address1, IM_ARRAYSIZE(active_contact.address1));
- ImGui::SameLine();draw_required_tag();
+ ImGui::SameLine();view_draw_required_tag();
ImGui::SetNextItemWidth(widthAvailable*0.5f);
ImGui::InputTextWithHint(localize("contact.form.address2"), localize("contact.form.address2"), active_contact.address2, IM_ARRAYSIZE(active_contact.address2));
- ImGui::SameLine();draw_required_tag();
+ ImGui::SameLine();view_draw_required_tag();
ImGui::SetNextItemWidth(widthAvailable*0.5f);
@@ -102,7 +72,7 @@ static void draw_contact_form()
if (selected_country) {
strncpy(active_contact.country, selected_country, IM_ARRAYSIZE(active_contact.country));
}
- ImGui::SameLine();draw_required_tag();
+ ImGui::SameLine();view_draw_required_tag();
ImGui::SetNextItemWidth(widthAvailable*0.5f);
ImGui::InputTextWithHint(localize("contact.form.taxnumber"), localize("contact.form.taxnumber"), active_contact.taxid, IM_ARRAYSIZE(active_contact.taxid));
@@ -129,14 +99,14 @@ static void draw_contact_form()
// Save button
ImGui::Spacing();
if (ImGui::Button(localize("form.save"))) {
- if (view_state == contact_view_state::CREATE)
+ if (current_view_state == view_state::CREATE)
administration_create_contact(active_contact);
- else if (view_state == contact_view_state::EDIT)
+ else if (current_view_state == view_state::EDIT)
administration_update_contact(active_contact);
memset(&active_contact, 0, sizeof(contact));
- view_state = contact_view_state::LIST;
+ current_view_state = view_state::LIST;
selected_country = 0;
}
if (!can_save) ImGui::EndDisabled();
@@ -148,14 +118,14 @@ static void draw_contact_form()
static void draw_contact_list()
{
- const u32 items_per_page = 5;
+ const u32 items_per_page = 50;
static s32 current_page = 0;
s32 max_page = (administration_get_contact_count() + items_per_page - 1) / items_per_page;
if (max_page == 0) max_page = 1;
if (ImGui::Button(localize("form.create")))
{
- view_state = contact_view_state::CREATE;
+ current_view_state = view_state::CREATE;
memset(&active_contact, 0, sizeof(contact));
snprintf(active_contact.id, IM_ARRAYSIZE(active_contact.id), "C/%d", administration_create_id());
}
@@ -205,7 +175,7 @@ static void draw_contact_list()
sprintf(btn_name, "%s##%d", localize("form.view"), i);
if (ImGui::Button(btn_name)) {
active_contact = c;
- view_state = contact_view_state::VIEW;
+ current_view_state = view_state::VIEW;
}
ImGui::SameLine();
@@ -213,7 +183,7 @@ static void draw_contact_list()
sprintf(btn_name, "%s##%d", localize("form.change"), i);
if (ImGui::Button(btn_name)) {
active_contact = c;
- view_state = contact_view_state::EDIT;
+ current_view_state = view_state::EDIT;
}
ImGui::SameLine();
@@ -247,11 +217,11 @@ static void draw_contact_list()
void views_draw_contacts()
{
- switch(view_state)
+ switch(current_view_state)
{
- case contact_view_state::LIST: draw_contact_list(); break;
- case contact_view_state::CREATE: draw_contact_form(); break;
- case contact_view_state::EDIT: draw_contact_form(); break;
- case contact_view_state::VIEW: draw_contact_form(); break;
+ case view_state::LIST: draw_contact_list(); break;
+ case view_state::CREATE: draw_contact_form(); break;
+ case view_state::EDIT: draw_contact_form(); break;
+ case view_state::VIEW: draw_contact_form(); break;
}
} \ No newline at end of file