use yew::prelude::*; use std::collections::HashMap; use crate::routing::AppView; use crate::components::{ViewComponent, EmptyState, RegistrationWizard}; use crate::models::*; use crate::services::{CompanyService, CompanyRegistration, RegistrationStatus}; #[derive(Properties, PartialEq)] pub struct CompaniesViewProps { pub on_navigate: Option>, #[prop_or_default] pub show_registration: bool, #[prop_or_default] pub registration_success: Option, #[prop_or_default] pub registration_failure: bool, } pub enum CompaniesViewMsg { LoadCompanies, CompaniesLoaded(Vec), LoadRegistrations, RegistrationsLoaded(Vec), SwitchToCompany(String), ShowRegistration, RegistrationComplete(Company), BackToCompanies, ViewCompany(u32), ContinueRegistration(CompanyRegistration), StartNewRegistration, DeleteRegistration(u32), ShowNewRegistrationForm, HideNewRegistrationForm, } pub struct CompaniesView { companies: Vec, registrations: Vec, loading: bool, show_registration: bool, current_registration: Option, show_new_registration_form: bool, } impl Component for CompaniesView { type Message = CompaniesViewMsg; type Properties = CompaniesViewProps; fn create(ctx: &Context) -> Self { // Load companies and registrations on component creation ctx.link().send_message(CompaniesViewMsg::LoadCompanies); ctx.link().send_message(CompaniesViewMsg::LoadRegistrations); Self { companies: Vec::new(), registrations: Vec::new(), loading: true, show_registration: ctx.props().show_registration, current_registration: None, show_new_registration_form: false, } } fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { match msg { CompaniesViewMsg::LoadCompanies => { self.loading = true; // Load companies from service let companies = CompanyService::get_companies(); ctx.link().send_message(CompaniesViewMsg::CompaniesLoaded(companies)); false } CompaniesViewMsg::CompaniesLoaded(companies) => { self.companies = companies; self.loading = false; true } CompaniesViewMsg::LoadRegistrations => { // Load actual registrations from service let registrations = CompanyService::get_registrations(); ctx.link().send_message(CompaniesViewMsg::RegistrationsLoaded(registrations)); false } CompaniesViewMsg::RegistrationsLoaded(registrations) => { self.registrations = registrations; true } CompaniesViewMsg::SwitchToCompany(company_id) => { // Navigate to company view if let Some(on_navigate) = &ctx.props().on_navigate { if let Ok(id) = company_id.parse::() { on_navigate.emit(AppView::CompanyView(id)); } } false } CompaniesViewMsg::ShowRegistration => { self.show_registration = true; self.current_registration = None; // Start fresh registration true } CompaniesViewMsg::StartNewRegistration => { self.show_registration = true; self.current_registration = None; // Start fresh registration true } CompaniesViewMsg::ContinueRegistration(registration) => { self.show_registration = true; self.current_registration = Some(registration); true } CompaniesViewMsg::RegistrationComplete(company) => { // Add new company to list and clear current registration let company_id = company.id; self.companies.push(company); self.current_registration = None; self.show_registration = false; // Navigate to registration success step if let Some(on_navigate) = &ctx.props().on_navigate { on_navigate.emit(AppView::EntitiesRegisterSuccess(company_id)); } true } CompaniesViewMsg::ViewCompany(company_id) => { // Navigate to company view if let Some(on_navigate) = &ctx.props().on_navigate { on_navigate.emit(AppView::CompanyView(company_id)); } false } CompaniesViewMsg::BackToCompanies => { self.show_registration = false; self.current_registration = None; true } CompaniesViewMsg::DeleteRegistration(registration_id) => { // Remove registration from list self.registrations.retain(|r| r.id != registration_id); // Update storage let _ = CompanyService::save_registrations(&self.registrations); true } CompaniesViewMsg::ShowNewRegistrationForm => { self.show_new_registration_form = true; true } CompaniesViewMsg::HideNewRegistrationForm => { self.show_new_registration_form = false; true } } } fn view(&self, ctx: &Context) -> Html { let link = ctx.link(); // Check if we should show success state if ctx.props().registration_success.is_some() { // Show success state html! { } } else if self.show_registration { // Registration view html! { } } else { // Main companies view with unified table html! { {self.render_companies_content(ctx)} } } } } impl CompaniesView { fn render_companies_content(&self, ctx: &Context) -> Html { let link = ctx.link(); if self.loading { return html! {
{"Loading..."}

{"Loading companies..."}

}; } if self.companies.is_empty() && self.registrations.is_empty() { return html! {
}; } html! {
{self.render_companies_table(ctx)}
} } fn render_companies_table(&self, ctx: &Context) -> Html { let link = ctx.link(); html! {
{"Companies & Registrations"}
{format!("{} companies, {} pending registrations", self.companies.len(), self.registrations.len())}
// Render active companies first {for self.companies.iter().map(|company| { let company_id = company.id; let on_view = { let link = link.clone(); Callback::from(move |e: MouseEvent| { e.prevent_default(); link.emit(CompaniesViewMsg::ViewCompany(company_id)); }) }; let on_switch = { let link = link.clone(); Callback::from(move |e: MouseEvent| { e.prevent_default(); link.emit(CompaniesViewMsg::SwitchToCompany(company_id.to_string())); }) }; html! { } })} // Render pending registrations {for self.registrations.iter().map(|registration| { let registration_clone = registration.clone(); let registration_id = registration.id; let can_continue = matches!(registration.status, RegistrationStatus::Draft | RegistrationStatus::PendingPayment | RegistrationStatus::PaymentFailed); let on_continue = { let link = link.clone(); let reg = registration_clone.clone(); Callback::from(move |e: MouseEvent| { e.prevent_default(); link.emit(CompaniesViewMsg::ContinueRegistration(reg.clone())); }) }; let on_delete = { let link = link.clone(); Callback::from(move |e: MouseEvent| { e.prevent_default(); if web_sys::window() .unwrap() .confirm_with_message("Are you sure you want to delete this registration?") .unwrap_or(false) { link.emit(CompaniesViewMsg::DeleteRegistration(registration_id)); } }) }; html! { } })}
{"Name"} {"Type"} {"Status"} {"Date"} {"Progress"} {"Actions"}
{&company.name}
{company.company_type.to_string()} {company.status.to_string()} {&company.incorporation_date} {"Complete"}
{®istration.company_name} {"(Registration)"}
{registration.company_type.to_string()} {registration.status.to_string()} {®istration.created_at}
{format!("{}/5", registration.current_step)}
{if can_continue { html! { } } else { html! { } }}
} } } #[function_component(CompaniesViewWrapper)] pub fn companies_view(props: &CompaniesViewProps) -> Html { html! { } }