Added checkbox to booking form - remember customer
This commit is contained in:
@@ -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<Customer> {
|
||||
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<ApiResponse<Vec<Customer>>, ServerFnError
|
||||
.await?;
|
||||
|
||||
Ok(ApiResponse::Data(customers))
|
||||
}
|
||||
|
||||
#[server]
|
||||
pub async fn get_remembered() -> Result<Option<Customer>, ServerFnError> {
|
||||
use actix_session::*;
|
||||
use leptos_actix::extract;
|
||||
|
||||
let session = extract::<Session>().await;
|
||||
|
||||
if session.is_err() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
Ok(session.unwrap().get::<Customer>("customer").unwrap_or(None))
|
||||
}
|
||||
+5
-1
@@ -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)]
|
||||
|
||||
@@ -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::<Vec<_>>();
|
||||
map.entry(i32::from_str(slot_str.get(1).unwrap_or(&"")).unwrap_or(0))
|
||||
|
||||
Reference in New Issue
Block a user