use leptos::*;
use crate::locales::trl;
use crate::validator::Validator;

#[component]
pub fn ValidationErr(
    validator: Validator,
) -> impl IntoView {
    view! {{move || {
        if !validator.is_valid() {
            if let Some(msgs) = validator.messages() {
                let out_msgs = msgs.into_iter().map(move |e| {
                    view! {
                        <div class="alert alert-danger">
                            {trl(&e)}
                        </div>
                    }
                }).collect_view();
                view! {
                    <div>
                        {out_msgs}
                    </div>
                }
            } else {
                view! {
                <div class="alert alert-danger">
                    {trl("Validation error")}
                </div>
                }
            }
        } else {
            view! {<div></div>}
        }
    }}
    }
}