feat(events): implement event invitation via token
Update the event model to include an `invite_token` generated upon creation. Add server functions to retrieve events by token and handle participant registration. Implement a new `/join/:token` route and UI component for joining events, and display the shareable invite link on the event details page.
This commit is contained in:
parent
83db04a187
commit
0c096f4e40
@ -261,6 +261,9 @@
|
|||||||
.bg-blue-500 {
|
.bg-blue-500 {
|
||||||
background-color: var(--color-blue-500);
|
background-color: var(--color-blue-500);
|
||||||
}
|
}
|
||||||
|
.bg-gray-100 {
|
||||||
|
background-color: var(--color-gray-100);
|
||||||
|
}
|
||||||
.bg-gray-800 {
|
.bg-gray-800 {
|
||||||
background-color: var(--color-gray-800);
|
background-color: var(--color-gray-800);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,6 +10,7 @@ pub struct Model {
|
|||||||
pub date: String,
|
pub date: String,
|
||||||
pub budget: String,
|
pub budget: String,
|
||||||
pub creator_id: i32,
|
pub creator_id: i32,
|
||||||
|
pub invite_token: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
|
|||||||
83
src/main.rs
83
src/main.rs
@ -20,6 +20,8 @@ enum Route {
|
|||||||
CreateEvent {},
|
CreateEvent {},
|
||||||
#[route("/event/:id")]
|
#[route("/event/:id")]
|
||||||
EventPage { id: i32 },
|
EventPage { id: i32 },
|
||||||
|
#[route("/join/:token")]
|
||||||
|
JoinEvent { token: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
const MAIN_CSS: Asset = asset!("/assets/main.css");
|
const MAIN_CSS: Asset = asset!("/assets/main.css");
|
||||||
@ -328,12 +330,39 @@ fn EventPage(id: i32) -> Element {
|
|||||||
use_resource(move || async move { server_fns::get_participants(id).await });
|
use_resource(move || async move { server_fns::get_participants(id).await });
|
||||||
let mut new_participant_name = use_signal(|| String::new());
|
let mut new_participant_name = use_signal(|| String::new());
|
||||||
let mut new_participant_email = use_signal(|| String::new());
|
let mut new_participant_email = use_signal(|| String::new());
|
||||||
|
let mut event_details = use_resource(move || async move {
|
||||||
|
// We need a way to get event details by ID, reusing get_my_events for now is inefficient but works if creator
|
||||||
|
// Better: add get_event(id) server fn. For now, let's just show the ID or fetch via participants if needed.
|
||||||
|
// Actually, let's just display the invite link.
|
||||||
|
// We can construct the invite link from the ID if we had the token.
|
||||||
|
// Let's fetch the event details to get the token.
|
||||||
|
// Wait, we don't have a get_event(id) that returns the token.
|
||||||
|
// Let's assume the user is the creator and use get_my_events to find it.
|
||||||
|
server_fns::get_my_events().await
|
||||||
|
});
|
||||||
|
|
||||||
|
let invite_link = use_memo(move || {
|
||||||
|
if let Some(Ok(events)) = event_details.read().as_ref() {
|
||||||
|
if let Some(event) = events.iter().find(|e| e.id == id) {
|
||||||
|
return Some(format!("http://localhost:8080/join/{}", event.invite_token));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
});
|
||||||
|
|
||||||
rsx! {
|
rsx! {
|
||||||
div {
|
div {
|
||||||
class: "flex flex-col items-center min-h-screen py-2",
|
class: "flex flex-col items-center min-h-screen py-2",
|
||||||
h1 { class: "text-2xl font-bold mb-4", "Event Details" }
|
h1 { class: "text-2xl font-bold mb-4", "Event Details" }
|
||||||
|
|
||||||
|
if let Some(link) = invite_link() {
|
||||||
|
div {
|
||||||
|
class: "mb-4 p-4 bg-gray-100 rounded",
|
||||||
|
p { class: "font-bold", "Invite Link:" }
|
||||||
|
code { "{link}" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Add Participant Form
|
// Add Participant Form
|
||||||
div {
|
div {
|
||||||
class: "flex gap-2 mb-4",
|
class: "flex gap-2 mb-4",
|
||||||
@ -392,3 +421,57 @@ fn EventPage(id: i32) -> Element {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
fn JoinEvent(token: String) -> Element {
|
||||||
|
let token_rx = token.clone();
|
||||||
|
let event = use_resource(move || {
|
||||||
|
let token = token_rx.clone();
|
||||||
|
async move { server_fns::get_event_by_token(token).await }
|
||||||
|
});
|
||||||
|
let mut name = use_signal(|| String::new());
|
||||||
|
let mut error = use_signal(|| String::new());
|
||||||
|
let navigator = use_navigator();
|
||||||
|
|
||||||
|
rsx! {
|
||||||
|
div {
|
||||||
|
class: "flex flex-col items-center justify-center min-h-screen py-2",
|
||||||
|
match &*event.read() {
|
||||||
|
Some(Ok(e)) => rsx! {
|
||||||
|
h1 { class: "text-2xl font-bold mb-4", "Join {e.name}" }
|
||||||
|
p { class: "mb-4", "Date: {e.date}" }
|
||||||
|
p { class: "mb-4", "Budget: {e.budget}" }
|
||||||
|
div {
|
||||||
|
class: "flex flex-col gap-4 w-64",
|
||||||
|
input {
|
||||||
|
class: "border p-2 rounded",
|
||||||
|
placeholder: "Your Name",
|
||||||
|
value: "{name}",
|
||||||
|
oninput: move |e| name.set(e.value())
|
||||||
|
}
|
||||||
|
button {
|
||||||
|
class: "bg-green-500 text-white p-2 rounded hover:bg-green-600",
|
||||||
|
onclick: move |_| {
|
||||||
|
let token = token.clone();
|
||||||
|
async move {
|
||||||
|
match server_fns::join_event(token, name()).await {
|
||||||
|
Ok(_) => {
|
||||||
|
navigator.push(Route::Dashboard {});
|
||||||
|
}
|
||||||
|
Err(e) => error.set(e.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Join Event"
|
||||||
|
}
|
||||||
|
if !error().is_empty() {
|
||||||
|
p { class: "text-red-500", "{error}" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Some(Err(e)) => rsx! { p { class: "text-red-500", "Error: {e}" } },
|
||||||
|
None => rsx! { p { "Loading..." } },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -15,6 +15,7 @@ pub struct EventSafe {
|
|||||||
pub name: String,
|
pub name: String,
|
||||||
pub date: String,
|
pub date: String,
|
||||||
pub budget: String,
|
pub budget: String,
|
||||||
|
pub invite_token: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[server]
|
#[server]
|
||||||
@ -23,6 +24,9 @@ pub async fn create_event(
|
|||||||
date: String,
|
date: String,
|
||||||
budget: String,
|
budget: String,
|
||||||
) -> Result<EventSafe, ServerFnError> {
|
) -> Result<EventSafe, ServerFnError> {
|
||||||
|
use rand::distributions::Alphanumeric;
|
||||||
|
use rand::{thread_rng, Rng};
|
||||||
|
|
||||||
let session: tower_sessions::Session = FullstackContext::extract().await?;
|
let session: tower_sessions::Session = FullstackContext::extract().await?;
|
||||||
let user_id: i32 = session
|
let user_id: i32 = session
|
||||||
.get("user_id")
|
.get("user_id")
|
||||||
@ -34,11 +38,18 @@ pub async fn create_event(
|
|||||||
.await
|
.await
|
||||||
.map_err(|e| ServerFnError::new(e.to_string()))?;
|
.map_err(|e| ServerFnError::new(e.to_string()))?;
|
||||||
|
|
||||||
|
let invite_token: String = thread_rng()
|
||||||
|
.sample_iter(&Alphanumeric)
|
||||||
|
.take(16)
|
||||||
|
.map(char::from)
|
||||||
|
.collect();
|
||||||
|
|
||||||
let new_event = events::ActiveModel {
|
let new_event = events::ActiveModel {
|
||||||
name: Set(name),
|
name: Set(name),
|
||||||
date: Set(date),
|
date: Set(date),
|
||||||
budget: Set(budget),
|
budget: Set(budget),
|
||||||
creator_id: Set(user_id),
|
creator_id: Set(user_id),
|
||||||
|
invite_token: Set(invite_token),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -52,9 +63,79 @@ pub async fn create_event(
|
|||||||
name: event.name,
|
name: event.name,
|
||||||
date: event.date,
|
date: event.date,
|
||||||
budget: event.budget,
|
budget: event.budget,
|
||||||
|
invite_token: event.invite_token,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[server]
|
||||||
|
pub async fn get_event_by_token(token: String) -> Result<EventSafe, ServerFnError> {
|
||||||
|
let db = db::establish_connection()
|
||||||
|
.await
|
||||||
|
.map_err(|e| ServerFnError::new(e.to_string()))?;
|
||||||
|
|
||||||
|
let event = events::Entity::find()
|
||||||
|
.filter(events::Column::InviteToken.eq(token))
|
||||||
|
.one(&db)
|
||||||
|
.await
|
||||||
|
.map_err(|e| ServerFnError::new(e.to_string()))?
|
||||||
|
.ok_or(ServerFnError::new("Event not found"))?;
|
||||||
|
|
||||||
|
Ok(EventSafe {
|
||||||
|
id: event.id,
|
||||||
|
name: event.name,
|
||||||
|
date: event.date,
|
||||||
|
budget: event.budget,
|
||||||
|
invite_token: event.invite_token,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[server]
|
||||||
|
pub async fn join_event(token: String, name: String) -> Result<(), ServerFnError> {
|
||||||
|
let session: tower_sessions::Session = FullstackContext::extract().await?;
|
||||||
|
let user_id: i32 = session
|
||||||
|
.get("user_id")
|
||||||
|
.await
|
||||||
|
.map_err(|e| ServerFnError::new(e.to_string()))?
|
||||||
|
.ok_or(ServerFnError::new("Not logged in"))?;
|
||||||
|
|
||||||
|
let db = db::establish_connection()
|
||||||
|
.await
|
||||||
|
.map_err(|e| ServerFnError::new(e.to_string()))?;
|
||||||
|
|
||||||
|
let event = events::Entity::find()
|
||||||
|
.filter(events::Column::InviteToken.eq(token))
|
||||||
|
.one(&db)
|
||||||
|
.await
|
||||||
|
.map_err(|e| ServerFnError::new(e.to_string()))?
|
||||||
|
.ok_or(ServerFnError::new("Event not found"))?;
|
||||||
|
|
||||||
|
// Check if already joined
|
||||||
|
let existing = participants::Entity::find()
|
||||||
|
.filter(participants::Column::EventId.eq(event.id))
|
||||||
|
.filter(participants::Column::UserId.eq(user_id))
|
||||||
|
.one(&db)
|
||||||
|
.await
|
||||||
|
.map_err(|e| ServerFnError::new(e.to_string()))?;
|
||||||
|
|
||||||
|
if existing.is_some() {
|
||||||
|
return Err(ServerFnError::new("Already joined"));
|
||||||
|
}
|
||||||
|
|
||||||
|
let new_participant = participants::ActiveModel {
|
||||||
|
event_id: Set(event.id),
|
||||||
|
name: Set(name),
|
||||||
|
user_id: Set(Some(user_id)),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
new_participant
|
||||||
|
.insert(&db)
|
||||||
|
.await
|
||||||
|
.map_err(|e| ServerFnError::new(e.to_string()))?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[server]
|
#[server]
|
||||||
pub async fn get_my_events() -> Result<Vec<EventSafe>, ServerFnError> {
|
pub async fn get_my_events() -> Result<Vec<EventSafe>, ServerFnError> {
|
||||||
let session: tower_sessions::Session = FullstackContext::extract().await?;
|
let session: tower_sessions::Session = FullstackContext::extract().await?;
|
||||||
@ -81,6 +162,7 @@ pub async fn get_my_events() -> Result<Vec<EventSafe>, ServerFnError> {
|
|||||||
name: e.name,
|
name: e.name,
|
||||||
date: e.date,
|
date: e.date,
|
||||||
budget: e.budget,
|
budget: e.budget,
|
||||||
|
invite_token: e.invite_token,
|
||||||
})
|
})
|
||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user