Leptos updated to 0.5

This commit is contained in:
2023-09-15 13:55:01 +02:00
parent 9dd0993204
commit bbad2bb183
18 changed files with 234 additions and 281 deletions
+90 -100
View File
@@ -12,108 +12,98 @@ use crate::components::validation_err::ValidationErr;
#[component]
pub fn CompanyEdit(
cx: Scope,
company: ReadSignal<Option<Company>>,
company: ReadSignal<Company>,
opener: DialogOpener,
) -> impl IntoView {
view! {cx,
{move ||
if let Some(c) = company.get() {
let update_company = create_server_action::<UpdateCompany>(cx);
let upd_val = update_company.value();
let validator = Validator::new(cx);
view! {cx,
<ActionForm
on:submit=move |ev| {
let act = UpdateCompany::from_event(&ev);
if !act.is_err() {
validator.check(&act.unwrap().company, &ev);
}
}
action=update_company>
<ModalDialog opener=opener title="Edit company">
<ModalBody>
<ServerErr result={upd_val} opener=opener/>
<ValidationErr validator=validator />
<input type="hidden" value=c.id() name="company[id]"/>
<div class="row">
<div class="col mb-3">
<label for="nameWithTitle" class="form-label">"Name"</label>
<input
type="text"
id="nameWithTitle"
class="form-control"
placeholder="Enter Name"
value=c.name
name="company[name]"
/>
</div>
</div>
<div class="row">
<div class="col mb-3">
<label for="street" class="form-label">"Street"</label>
<input
type="text"
id="street"
class="form-control"
placeholder="Enter Street"
value=c.street
name="company[street]"
/>
</div>
<div class="col-4 mb-3">
<label for="houseNumber" class="form-label">"House number"</label>
<input
type="text"
id="houseNumber"
class="form-control"
placeholder="Enter House number"
value=c.house_number
name="company[house_number]"
/>
</div>
</div>
<div class="row">
<div class="col-4 mb-3">
<label for="zip" class="form-label">"ZIP code"</label>
<input
type="text"
id="zip"
class="form-control"
placeholder="Enter ZIP code"
value=c.zip_code
name="company[zip_code]"
/>
</div>
<div class="col mb-3">
<label for="city" class="form-label">"City"</label>
<input
type="text"
id="city"
class="form-control"
placeholder="Enter City"
value=c.city
name="company[city]"
/>
</div>
</div>
</ModalBody>
<ModalFooter>
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal" on:click=move |_| opener.hide()>
{trl(cx, "Close")}
</button>
<button type="submit" class="btn btn-primary">
{trl(cx, "Save changes")}
</button>
</ModalFooter>
</ModalDialog>
</ActionForm>
let update_company = create_server_action::<UpdateCompany>();
let upd_val = update_company.value();
let validator = Validator::new();
view! {
<ActionForm
on:submit=move |ev| {
let act = UpdateCompany::from_event(&ev);
if !act.is_err() {
validator.check(&act.unwrap().company, &ev);
}
}
} else {
view! {cx,
<DlgNotLoaded opener=opener title="Edit company" />
}
}
}
action=update_company>
<ModalDialog opener=opener title="Edit company">
<ModalBody>
<ServerErr result={upd_val} opener=opener/>
<ValidationErr validator=validator />
<input type="hidden" value={move || company.get().id()} name="company[id]"/>
<div class="row">
<div class="col mb-3">
<label for="nameWithTitle" class="form-label">"Name"</label>
<input
type="text"
id="nameWithTitle"
class="form-control"
placeholder="Enter Name"
value={move || company.get().name}
name="company[name]"
/>
</div>
</div>
<div class="row">
<div class="col mb-3">
<label for="street" class="form-label">"Street"</label>
<input
type="text"
id="street"
class="form-control"
placeholder="Enter Street"
value={move || company.get().street}
name="company[street]"
/>
</div>
<div class="col-4 mb-3">
<label for="houseNumber" class="form-label">"House number"</label>
<input
type="text"
id="houseNumber"
class="form-control"
placeholder="Enter House number"
value={move || company.get().house_number}
name="company[house_number]"
/>
</div>
</div>
<div class="row">
<div class="col-4 mb-3">
<label for="zip" class="form-label">"ZIP code"</label>
<input
type="text"
id="zip"
class="form-control"
placeholder="Enter ZIP code"
value={move || company.get().zip_code}
name="company[zip_code]"
/>
</div>
<div class="col mb-3">
<label for="city" class="form-label">"City"</label>
<input
type="text"
id="city"
class="form-control"
placeholder="Enter City"
value={move || company.get().city}
name="company[city]"
/>
</div>
</div>
</ModalBody>
<ModalFooter>
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal" on:click=move |_| opener.hide()>
{trl("Close")}
</button>
<button type="submit" class="btn btn-primary">
{trl("Save changes")}
</button>
</ModalFooter>
</ModalDialog>
</ActionForm>
}
}
+12 -11
View File
@@ -1,32 +1,33 @@
use leptos::*;
use serde::de::Unexpected::Option;
use crate::backend::company::get_company;
use crate::backend::data::Company;
use crate::components::modal_box::DialogOpener;
use crate::locales::trl;
use crate::pages::company_edit::CompanyEdit;
#[component]
pub fn CompanyInfo(cx: Scope) -> impl IntoView {
let editor = DialogOpener::new(cx);
let company = create_resource(cx, move|| editor.visible(), move |_| { get_company(cx) });
let (cmp, set_cmp) = create_signal(cx, None);
pub fn CompanyInfo() -> impl IntoView {
let editor = DialogOpener::new();
let company = create_resource(move|| editor.visible(), move |_| { get_company() });
let (cmp, set_cmp) = create_signal(Company::default());
view! {cx,
view! {
<CompanyEdit company={cmp} opener=editor/>
<div class="card">
<div class="card-body">
<h5 class="card-title"><i class="bx bx-buildings"></i>" "{trl(cx, "Company info")}</h5>
<h5 class="card-title"><i class="bx bx-buildings"></i>" "{trl("Company info")}</h5>
<p class="card-text">
<Transition fallback=move || view! {cx, <p>{trl(cx, "Loading...")}</p> }>
<Transition fallback=move || view! {<p>{trl("Loading...")}</p> }>
{move || {
company.read(cx).map(|c| match c {
Err(e) => {view! {cx, <p>{trl(cx, "Error loading data")}</p>
company.read().map(|c| match c {
Err(e) => {view! {<p>{trl("Error loading data")}</p>
<p>{e.to_string()}</p>
}}
Ok(c) => {
set_cmp.update(|cmp| *cmp = Some(c.clone()));
set_cmp.update(|cmp| *cmp = c.clone());
view! {
cx, <p><b>{c.name}</b></p>
<p><b>{c.name}</b></p>
<p>{c.street}" "{c.house_number}<br/>
{c.zip_code}" "{c.city}
</p>
+9 -9
View File
@@ -5,21 +5,21 @@ use crate::locales::trl;
/// Renders the home page of your application.
#[component]
pub fn HomePage(cx: Scope) -> impl IntoView {
pub fn HomePage() -> impl IntoView {
// Creates a reactive value to update the button
let (count, set_count) = create_signal(cx, 0);
let (count, set_count) = create_signal(0);
let on_click = move |_| set_count.update(|count| *count += 1);
let dialog = DialogOpener::new(cx);
let dialog = DialogOpener::new();
//let (dialog, set_dialog) = create_signal(cx, false);
//let (dialog, set_dialog) = create_signal(false);
//let on_dialog = move |_| dialog.set_visible.update(|dialog| {*dialog = true});
//let pok = use_context::<Request>(cx);
//let pok = use_context::<Request>();
//log!("{:?}", pok);
view! { cx,
view! {
<ModalDialog opener={dialog} title="Titulek">
<ModalBody>
<div class="row">
@@ -67,14 +67,14 @@ pub fn HomePage(cx: Scope) -> impl IntoView {
<button on:click=move |_| dialog.show()>"Dialog"</button>
<button on:click=move |_| {
spawn_local(async move {
set_session(cx).await;
set_session().await;
});
}>"Session"</button>
<button on:click=move |_| {
spawn_local(async move {
get_session(cx).await;
get_session().await;
});
}>"Session get"</button>
<p>{trl(cx, "testik!")}</p>
<p>{trl("testik!")}</p>
}
}
+3 -3
View File
@@ -3,9 +3,9 @@ use crate::locales::trl;
use crate::pages::company_info::CompanyInfo;
#[component]
pub fn Settings(cx: Scope) -> impl IntoView {
view! {cx,
<h1>{trl(cx, "Settings")}</h1>
pub fn Settings() -> impl IntoView {
view! {
<h1>{trl("Settings")}</h1>
<div class="row mb-5">
<div class="col-md-6 col-lg-4 mb-3">
<CompanyInfo/>