use crate::components::modal_box::DialogOpener;
use leptos::*;
use crate::backend::data::ApiResponse;
use crate::locales::trl;

#[component]
pub fn ServerErr(
    result: RwSignal<Option<Result<ApiResponse<()>, ServerFnError>>>,
    opener: DialogOpener,
) -> impl IntoView {
    view! {{move || {
        if let Some(val) = result.get() {
            match val {
                Ok(resp) => if let ApiResponse::Error(err) = resp {
                    view! {
                    <div class="alert alert-danger">
                        {trl(&err)}
                    </div>
                    }
                }
                else {
                    opener.hide();
                    view! {<div></div>}
                }
                Err(e) => {
                    view! {
                    <div class="alert alert-danger">
                        "Server error: " {e.to_string()}
                    </div>
                    }
                }
            }
            /*if let Err(e) = val {
                view! {
                <div class="alert alert-danger">
                    "Server error: " {e.to_string()}
                </div>
                }
            } else {
                opener.hide();
                view! {<div></div>}
            }*/
        } else {
            view! {<div></div>}
        }
    }}
    }
}
