diff --git a/src/backend/customer.rs b/src/backend/customer.rs index 085a362..6c6c93e 100644 --- a/src/backend/customer.rs +++ b/src/backend/customer.rs @@ -9,6 +9,8 @@ cfg_if! { if #[cfg(feature = "ssr")] { use sqlx::Error; use std::ops::DerefMut; use leptos::expect_context; + use leptos_actix::extract; + use actix_session::Session; pub async fn find_customer_by_email(email: &str, tx: &mut Transaction<'_, Postgres>) -> Option { let customer = query_as::<_, Customer>("SELECT * FROM customer WHERE email = $1") @@ -50,6 +52,12 @@ cfg_if! { if #[cfg(feature = "ssr")] { .await?; Ok(find_customer_by_email(email, tx).await.ok_or(Error::RowNotFound)?) } + + pub async fn remember_customer(customer: Customer) -> Result<(), ServerFnError> { + let session: Session = extract().await?; + session.insert("customer", customer)?; + Ok(()) + } }} #[server] @@ -66,4 +74,18 @@ pub async fn get_customers() -> Result>, ServerFnError .await?; Ok(ApiResponse::Data(customers)) +} + +#[server] +pub async fn get_remembered() -> Result, ServerFnError> { + use actix_session::*; + use leptos_actix::extract; + + let session = extract::().await; + + if session.is_err() { + return Ok(None); + } + + Ok(session.unwrap().get::("customer").unwrap_or(None)) } \ No newline at end of file diff --git a/src/backend/data.rs b/src/backend/data.rs index 729076a..7c08382 100644 --- a/src/backend/data.rs +++ b/src/backend/data.rs @@ -376,7 +376,8 @@ pub struct CrReservation { email: String, #[validate(length(min = 1,message = "Enter your phone number"))] phone: String, - note: String + note: String, + remember: bool } #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] @@ -420,6 +421,9 @@ impl CrReservation { pub fn note(&self) -> &str { &self.note } + pub fn remember(&self) -> bool { + self.remember + } } #[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, Default)] diff --git a/src/backend/reservation.rs b/src/backend/reservation.rs index 00d2b87..69df9e3 100644 --- a/src/backend/reservation.rs +++ b/src/backend/reservation.rs @@ -261,6 +261,7 @@ pub async fn create_reservation(reservation: CrReservation, pow: String) -> Resu use crate::backend::customer::find_customer_by_email; use crate::backend::customer::sync_customer_data; use crate::backend::customer::create_customer; + use crate::backend::customer::remember_customer; use crate::backend::property::get_prop_by_id; use crate::backend::data::{TmCheck, ReservationState, Reservations}; use std::collections::HashMap; @@ -272,6 +273,15 @@ pub async fn create_reservation(reservation: CrReservation, pow: String) -> Resu Pow::validate(&pow)?; + if reservation.remember() { + remember_customer(Customer::new( + 0, + reservation.full_name().to_string(), + reservation.email().to_string(), + reservation.phone().to_string(), + 0)).await?; + } + let slots = reservation.slots().iter().fold(HashMap::new(), |mut map, s| { let slot_str = s.split("|").collect::>(); map.entry(i32::from_str(slot_str.get(1).unwrap_or(&"")).unwrap_or(0)) diff --git a/src/locales/catalogues.rs b/src/locales/catalogues.rs index 09dab1f..abe3488 100644 --- a/src/locales/catalogues.rs +++ b/src/locales/catalogues.rs @@ -131,7 +131,8 @@ lazy_static! { ("Active", "Aktivní"), ("Are you sure you want to delete property ", "Opravdu chcete smazat předmět "), ("Delete property", "Smazat předmět"), - ("Are you sure you want to delete user ", "Opravdu chcete smazat uživatele ") + ("Are you sure you want to delete user ", "Opravdu chcete smazat uživatele "), + ("Remember for next time", "Zapamatovat pro příště") ])), ("sk", HashMap::from( [ ("Dashboard", "Prehlad"), diff --git a/src/pages/public.rs b/src/pages/public.rs index 9ce7a66..006f872 100644 --- a/src/pages/public.rs +++ b/src/pages/public.rs @@ -3,7 +3,8 @@ use leptos::*; use leptos_captcha::{Captcha, pow_dispatch}; use leptos_router::*; use rust_decimal::Decimal; -use crate::backend::data::{ApiResponse, DayHour, Reservation, ResProperty, SlotType, TmCheck}; +use crate::backend::customer::get_remembered; +use crate::backend::data::{ApiResponse, Customer, DayHour, Reservation, ResProperty, SlotType, TmCheck}; use crate::backend::reservation::{CreateReservation, get_public_form_data, is_reserved}; use crate::backend::user::get_pow; use crate::components::data_form::ForValidation; @@ -100,6 +101,9 @@ pub fn Public() -> impl IntoView { let result_dlg = DialogOpener::new(); let result = cr_reservation.value(); let is_pending = create_rw_signal(None); + let active_str = create_rw_signal("true".to_string()); + let get_customer = create_blocking_resource(||(), move |_| get_remembered()); + let customer = create_rw_signal(Customer::default()); create_effect(move |_| { day.set(Local::now().date_naive()); @@ -157,6 +161,14 @@ pub fn Public() -> impl IntoView { {trl("Loading...")}

}> {move || { + get_customer.get().map(|c| match c { + Err(_) => {}, + Ok(c) => { + if let Some(c) = c { + customer.set(c); + } + } + }); form_data.get().map(|u| match u { Err(e) => { view! {
{e.to_string()}
}} @@ -216,7 +228,7 @@ pub fn Public() -> impl IntoView { id="full_name" class="form-control" placeholder={trl("Enter full name")} - //prop:value={move || opener.empty()} + prop:value={move || customer.get().full_name} name="reservation[full_name]" /> @@ -229,7 +241,7 @@ pub fn Public() -> impl IntoView { id="email" class="form-control" placeholder={trl("Enter e-mail address")} - //prop:value={move || opener.empty()} + prop:value={move || customer.get().email} name="reservation[email]" /> @@ -242,7 +254,7 @@ pub fn Public() -> impl IntoView { id="phone" class="form-control" placeholder={trl("Enter phone number")} - //prop:value={move || opener.empty()} + prop:value={move || customer.get().phone} name="reservation[phone]" /> @@ -260,6 +272,15 @@ pub fn Public() -> impl IntoView { /> + + +