You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.3 KiB
Rust

use crate::backend::data::ApiResponse;
use crate::components::modal_box::{DialogOpener, ModalBody, ModalDialog, ModalFooter};
use leptos::*;
use leptos::server_fn::client::browser::BrowserClient;
use leptos::server_fn::codec::{Json, PostUrl};
use leptos::server_fn::error::NoCustomError;
use leptos::server_fn::ServerFn;
use leptos_router::*;
use serde::de::DeserializeOwned;
use validator::Validate;
use crate::components::server_err::ServerErr;
use crate::components::validation_err::ValidationErr;
use crate::locales::trl;
use crate::validator::Validator;
pub trait ForValidation {
fn entity(&self) -> &dyn Validate;
}
#[component]
pub fn data_form<T: 'static + Clone +
ServerFn<Client = BrowserClient,
InputEncoding = PostUrl,
OutputEncoding = Json,
Error = NoCustomError,
Output = ApiResponse<()>> +
DeserializeOwned + ForValidation>(
opener: DialogOpener,
action: Action<T, Result<ApiResponse<()>, ServerFnError>>,
title: &'static str,
children: Children
) -> impl IntoView {
let upd_val = action.value();
let validator = Validator::new();
view! {
<ActionForm
on:submit=move |ev| {
let act = T::from_event(&ev);
if !act.is_err() {
validator.check(act.unwrap().entity(), &ev);
}
}
action=action>
<ModalDialog opener=opener title=title validator=validator>
<ModalBody>
<ServerErr result={upd_val} opener=opener/>
<ValidationErr validator=validator />
{children()}
</ModalBody>
<ModalFooter>
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal"
on:click=move |_| {
validator.reset();
opener.hide();}>
{trl("Close")}
</button>
<button type="submit" class="btn btn-primary">
{trl("Save changes")}
</button>
</ModalFooter>
</ModalDialog>
</ActionForm>
}
}
#[component]
pub fn question_dialog<T: 'static
+ ServerFn<Client = BrowserClient,
InputEncoding = PostUrl,
OutputEncoding = Json,
Error = NoCustomError,
Output = ApiResponse<()>>
+ DeserializeOwned
+ Clone>(
opener: DialogOpener,
action: Action<T, Result<ApiResponse<()>, ServerFnError>>,
title: &'static str,
children: Children
) -> impl IntoView {
let upd_val = action.value();
view! {
<ActionForm action=action>
<ModalDialog opener=opener title=title>
<ModalBody>
<ServerErr result={upd_val} opener=opener/>
{children()}
</ModalBody>
<ModalFooter>
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal"
on:click=move |_| {
opener.hide();}>
{trl("No")}
</button>
<button type="submit" class="btn btn-primary">
{trl("Yes")}
</button>
</ModalFooter>
</ModalDialog>
</ActionForm>
}
}