From 6366fb3c96e4ed281e233279c85bbfd90ab3ecbc Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Wed, 4 Sep 2024 23:38:21 -0400 Subject: Support joining channels. --- src/index.rs | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) (limited to 'src/index.rs') diff --git a/src/index.rs b/src/index.rs index 605d9f6..9de91d5 100644 --- a/src/index.rs +++ b/src/index.rs @@ -16,10 +16,15 @@ async fn index( async fn index_authenticated(db: SqlitePool, login: Login) -> Result { let mut tx = db.begin().await?; - let channels = tx.channels().for_login(&login.id).await?; + let joined_channels = tx.channels().joined(&login.id).await?; + let unjoined_channels = tx.channels().unjoined(&login.id).await?; tx.commit().await?; - Ok(templates::authenticated(login, &channels)) + Ok(templates::authenticated( + login, + &joined_channels, + &unjoined_channels, + )) } pub fn router() -> Router { @@ -33,7 +38,8 @@ mod templates { pub fn authenticated<'c>( login: Login, - channels: impl IntoIterator, + joined_channels: impl IntoIterator, + unjoined_channels: impl IntoIterator, ) -> Markup { html! { (DOCTYPE) @@ -42,7 +48,8 @@ mod templates { } body { section { - (channel_list(channels)) + (channel_list(joined_channels)) + (join_channel(unjoined_channels)) (create_channel()) } section { @@ -52,21 +59,21 @@ mod templates { } } - fn channel_list<'c>(channels: impl IntoIterator) -> Markup { + fn channel_list<'c>(joined_channels: impl IntoIterator) -> Markup { html! { ul { - @for channel in channels { - (channel_entry(&channel)) + @for channel in joined_channels { + (joined_channel_entry(&channel)) } } } } - fn channel_entry(channel: &Channel) -> Markup { + fn joined_channel_entry(channel: &Channel) -> Markup { let leave_url = format!("/{}/leave", channel.id); html! { li { - (channel.name) "(" (channel.id) ")" + (channel.name) " (" (channel.id) ")" form action=(leave_url) method="post" { button { "leave" @@ -76,6 +83,24 @@ mod templates { } } + fn join_channel<'c>(unjoined_channels: impl IntoIterator) -> Markup { + html! { + form action="join" method="post" { + select name="channel" required { + option value="" { "channel" } + @for channel in unjoined_channels { + option value=(channel.id) { + (channel.name) " (" (channel.id) ")" + } + } + } + button { + "join" + } + } + } + } + fn create_channel() -> Markup { html! { form action="/create" method="post" { -- cgit v1.2.3