Skip to content
This repository has been archived by the owner on Aug 24, 2024. It is now read-only.

Commit

Permalink
feat: cta guest user (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
b-avb authored Mar 27, 2024
1 parent a2c200b commit b061e15
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 71 deletions.
31 changes: 31 additions & 0 deletions src/components/molecules/guest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use dioxus::prelude::*;

use crate::components::atoms::Button;

#[derive(Props)]
pub struct GuestProps<'a> {
description: &'a str,
cta: &'a str,
on_click: EventHandler<'a, MouseEvent>,
}

pub fn Guest<'a>(cx: Scope<'a, GuestProps<'a>>) -> Element<'a> {
render!(rsx!(
section {
class: "guest",
span {
"{cx.props.description}",
}
div {
class: "guest__cta",
Button {
text: "{cx.props.cta}",
on_click: move |event| {
cx.props.on_click.call(event)
},
status: None
}
}
}
))
}
2 changes: 2 additions & 0 deletions src/components/molecules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
pub mod attach_preview;
pub mod guest;
pub mod input_message;
pub mod list;
pub mod menu;
pub mod modal;
pub mod rooms;

pub use attach_preview::AttachPreview;
pub use guest::Guest;
pub use input_message::InputMessage;
pub use list::List;
pub use menu::Menu;
Expand Down
104 changes: 39 additions & 65 deletions src/pages/chat/room/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ use crate::{
attach::AttachType, button::Variant, Attach, Avatar, Button, Close, Header, Icon,
MessageInput, RoomView,
},
molecules::rooms::CurrentRoom,
molecules::{rooms::CurrentRoom, Guest},
},
hooks::{
use_attach::{use_attach, AttachError, AttachFile},
use_client::{use_client, UseClientState},
use_client::use_client,
use_notification::use_notification,
use_room::use_room,
use_session::use_session,
},
pages::chat::room::new::CreationStatus,
services::matrix::matrix::create_room,
services::matrix::matrix::{create_room, find_user_by_id},
utils::{
i18n_get_key_value::i18n_get_key_value,
matrix::{mxc_to_thumbnail_uri, ImageMethod, ImageSize},
Expand Down Expand Up @@ -74,6 +75,9 @@ pub fn RoomGroup(cx: Scope) -> Element {
let key_input_message_file_type = translate!(i18, "chat.input_message.file_type");
let key_input_message_not_found = translate!(i18, "chat.input_message.not_found");

let key_chat_guest_signup_description = translate!(i18, "chat.guest.signup.description");
let key_chat_guest_signup_cta = translate!(i18, "chat.guest.signup.cta");

let i18n_map = HashMap::from([
(key_group_title, translate!(i18, "group.title")),
(
Expand Down Expand Up @@ -109,6 +113,7 @@ pub fn RoomGroup(cx: Scope) -> Element {
let attach = use_attach(cx);
let notification = use_notification(cx);
let room = use_room(cx);
let session = use_session(cx);

let selected_users =
use_shared_state::<SelectedProfiles>(cx).expect("Unable to use SelectedProfile");
Expand All @@ -130,7 +135,7 @@ pub fn RoomGroup(cx: Scope) -> Element {
let element = users.read().clone().into_iter().find(|u| u.id.eq(&id));

if let None = element {
match process_find_user_by_id(&id, &client).await {
match find_user_by_id(&id, &client.get()).await {
Ok(profile) => users.with_mut(|user| user.push(profile)),
Err(_) => {
notification.handle_error(&key_group_error_not_found);
Expand Down Expand Up @@ -414,25 +419,35 @@ pub fn RoomGroup(cx: Scope) -> Element {
}
)
} else {
rsx!(
div {
class: "group__cta__wrapper row",
Button {
text: "{i18n_get_key_value(&i18n_map, key_group_meta_cta_back)}",
status: None,
variant: &Variant::Secondary,
on_click: move |_| {
handle_complete_group.set(false)
}
if session.is_guest() {
rsx!(
Guest {
description: "{key_chat_guest_signup_description}",
cta: "{key_chat_guest_signup_cta}",
on_click: move |_| {}
}
Button {
text: "{i18n_get_key_value(&i18n_map, key_group_meta_cta_create)}",
status: None,
disabled: group_name.get().len() == 0,
on_click: on_handle_create
)
} else {
rsx!(
div {
class: "group__cta__wrapper row",
Button {
text: "{i18n_get_key_value(&i18n_map, key_group_meta_cta_back)}",
status: None,
variant: &Variant::Secondary,
on_click: move |_| {
handle_complete_group.set(false)
}
}
Button {
text: "{i18n_get_key_value(&i18n_map, key_group_meta_cta_create)}",
status: None,
disabled: group_name.get().len() == 0,
on_click: on_handle_create
}
}
}
)
)
}
}
)
} else {
Expand Down Expand Up @@ -493,11 +508,12 @@ pub fn RoomGroup(cx: Scope) -> Element {
)
})
}

div {
class: "group__cta__wrapper",
Button {
text: "{i18n_get_key_value(&i18n_map, key_group_select_cta)}",
disabled: if selected_users.read().profiles.len() == 0 { true } else { false },
disabled: selected_users.read().profiles.is_empty(),
status: None,
on_click: move |_| {
handle_complete_group.set(true)
Expand All @@ -506,48 +522,6 @@ pub fn RoomGroup(cx: Scope) -> Element {
}
)
}
}
}

pub(crate) async fn process_find_user_by_id(
id: &str,
client: &UseClientState,
) -> Result<Profile, CreateRoomError> {
let u = UserId::parse(&id).map_err(|_| CreateRoomError::InvalidUserId)?;

let u = u.deref();

let request = matrix_sdk::ruma::api::client::profile::get_profile::v3::Request::new(u);

let response = client
.get()
.send(request, None)
.await
.map_err(|_| CreateRoomError::UserNotFound)?;

let displayname = response
.displayname
.ok_or(CreateRoomError::InvalidUsername)?;

let avatar_uri = response
.avatar_url
.map(|uri| {
mxc_to_thumbnail_uri(
&uri,
ImageSize {
width: 48,
height: 48,
},
ImageMethod::CROP,
)
})
.flatten();

let profile = Profile {
displayname,
avatar_uri,
id: id.to_string(),
};

Ok(profile)
}
}
36 changes: 30 additions & 6 deletions src/pages/chat/room/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use matrix_sdk::ruma::UserId;
use crate::{
components::{
atoms::{button::Variant, Button, Header, MessageInput, RoomView},
molecules::rooms::CurrentRoom,
molecules::{rooms::CurrentRoom, Guest},
},
hooks::{
use_client::use_client, use_notification::use_notification, use_room::use_room,
use_auth::use_auth, use_client::use_client, use_init_app::BeforeSession, use_notification::use_notification, use_room::use_room, use_session::use_session
},
pages::chat::room::group::{self, CreateRoomError, Profile},
services::matrix::matrix::create_room,
pages::chat::room::group::{CreateRoomError, Profile},
services::matrix::matrix::{create_room, find_user_by_id},
utils::{i18n_get_key_value::i18n_get_key_value, sync_room::sync_created_room},
};
use futures_util::{StreamExt, TryFutureExt};
Expand All @@ -35,6 +35,9 @@ pub fn RoomNew(cx: Scope) -> Element {
let key_dm_error_not_found = translate!(i18, "dm.error.not_found");
let key_dm_error_profile = translate!(i18, "dm.error.profile");

let key_chat_guest_signup_description = translate!(i18, "chat.guest.signup.description");
let key_chat_guest_signup_cta = translate!(i18, "chat.guest.signup.cta");

let key_dm_title = "dm-title";
let key_dm_label = "dm-label";
let key_dm_placeholder = "dm-placeholder";
Expand All @@ -51,8 +54,13 @@ pub fn RoomNew(cx: Scope) -> Element {
let client = use_client(cx);
let notification = use_notification(cx);
let room = use_room(cx);
let session = use_session(cx);
let auth = use_auth(cx);

let before_session =
use_shared_state::<BeforeSession>(cx).expect("Unable to use before session");

let user_id = use_state::<String>(cx, || String::from(""));
let user_id = use_state::<String>(cx, || String::from("@edith-test-1:matrix.org"));
let user = use_state::<Option<Profile>>(cx, || None);
let error_field = use_state::<Option<String>>(cx, || None);
let status = use_state::<CreationStatus>(cx, || CreationStatus::Start);
Expand All @@ -62,7 +70,7 @@ pub fn RoomNew(cx: Scope) -> Element {

async move {
while let Some(id) = rx.next().await {
match group::process_find_user_by_id(&id, &client).await {
match find_user_by_id(&id, &client.get()).await {
Ok(profile) => user.set(Some(profile)),
Err(_) => {
notification.handle_error(&key_dm_error_not_found);
Expand All @@ -73,6 +81,10 @@ pub fn RoomNew(cx: Scope) -> Element {
});

let on_handle_create = move |_| {
if session.is_guest() {
return;
}

cx.spawn({
to_owned![
client,
Expand Down Expand Up @@ -172,6 +184,18 @@ pub fn RoomNew(cx: Scope) -> Element {
}
)
}
if session.is_guest() {
rsx!(
Guest {
description: "{key_chat_guest_signup_description}",
cta: "{key_chat_guest_signup_cta}",
on_click: move |_| {
auth.set_logged_in(false);
*before_session.write() = BeforeSession::Signup;
}
}
)
}
match status.get() {
CreationStatus::Creating => {
render!(rsx! {
Expand Down

0 comments on commit b061e15

Please sign in to comment.