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 {
                    opener.display_err();
                    view! {
                    <div class="alert alert-danger" style={move || {
                            if opener.show_err() {
                                ""
                            } else {
                                "display: none"
                            }
                        }}>
                        {trl(&err)}
                    </div>
                    }
                }
                else {
                    opener.hide();
                    view! {<div></div>}
                }
                Err(e) => {
                    view! {
                    <div class="alert alert-danger">
                        "Server error: " {e.to_string()}
                    </div>
                    }
                }
            }
        } else {
            view! {<div></div>}
        }
    }}
    }
}
