From 25d6e98a173278058b1043cae14e63a20495a83e Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 1 Jul 2023 17:25:27 +0200 Subject: [PATCH] Show new created comment reply after creation --- src/components/comment_row.rs | 14 ++++++---- src/components/post_page.rs | 52 +++++++++++++++++------------------ src/main.rs | 2 +- 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/src/components/comment_row.rs b/src/components/comment_row.rs index 5d24b9b..f162fa9 100644 --- a/src/components/comment_row.rs +++ b/src/components/comment_row.rs @@ -14,7 +14,7 @@ use crate::util; use crate::util::get_web_image_url; use crate::util::markdown_to_pango_markup; -use super::post_page::PostInput; +use super::post_page::PostPageInput; use super::voting_row::VotingRowModel; use super::voting_row::VotingStats; @@ -39,10 +39,10 @@ pub enum CommentRowMsg { impl FactoryComponent for CommentRow { type Init = CommentView; type Input = CommentRowMsg; - type Output = PostInput; + type Output = PostPageInput; type CommandOutput = (); type Widgets = PostViewWidgets; - type ParentInput = PostInput; + type ParentInput = PostPageInput; type ParentWidget = gtk::Box; view! { @@ -171,7 +171,7 @@ impl FactoryComponent for CommentRow { fn update(&mut self, message: Self::Input, sender: FactorySender) { match message { CommentRowMsg::OpenPerson => { - sender.output(PostInput::PassAppMessage(crate::AppMsg::OpenPerson( + sender.output(PostPageInput::PassAppMessage(crate::AppMsg::OpenPerson( self.comment.creator.id.clone(), ))); } @@ -179,7 +179,7 @@ impl FactoryComponent for CommentRow { let comment_id = self.comment.comment.id; std::thread::spawn(move || { let _ = api::comment::delete_comment(comment_id); - sender.output_sender().emit(PostInput::PassAppMessage( + sender.output_sender().emit(PostPageInput::PassAppMessage( crate::AppMsg::StartFetchPosts(None, true), )); }); @@ -223,7 +223,9 @@ impl FactoryComponent for CommentRow { std::thread::spawn(move || { match api::comment::create_comment(post_id, data.body, Some(parent_id)) { - Ok(_comment) => {} + Ok(comment) => { + sender.output_sender().emit(PostPageInput::CreatedComment(comment.comment_view)); + } Err(err) => { println!("{}", err.to_string()); } diff --git a/src/components/post_page.rs b/src/components/post_page.rs index c9b7b72..d791285 100644 --- a/src/components/post_page.rs +++ b/src/components/post_page.rs @@ -30,7 +30,7 @@ pub struct PostPage { } #[derive(Debug)] -pub enum PostInput { +pub enum PostPageInput { UpdatePost(GetPostResponse), DoneFetchComments(Vec), OpenPerson, @@ -49,7 +49,7 @@ pub enum PostInput { #[relm4::component(pub)] impl SimpleComponent for PostPage { type Init = GetPostResponse; - type Input = PostInput; + type Input = PostPageInput; type Output = crate::AppMsg; view! { @@ -102,7 +102,7 @@ impl SimpleComponent for PostPage { gtk::Button { #[watch] set_label: &model.info.post_view.creator.name, - connect_clicked => PostInput::OpenPerson, + connect_clicked => PostPageInput::OpenPerson, }, gtk::Label { @@ -122,7 +122,7 @@ impl SimpleComponent for PostPage { gtk::Button { #[watch] set_label: &model.info.community_view.community.title, - connect_clicked => PostInput::OpenCommunity, + connect_clicked => PostPageInput::OpenCommunity, }, gtk::Label { @@ -136,13 +136,13 @@ impl SimpleComponent for PostPage { gtk::Button { set_label: "View", - connect_clicked => PostInput::OpenLink, + connect_clicked => PostPageInput::OpenLink, }, if model.info.post_view.creator.id.0 == settings::get_current_account().id { gtk::Button { set_icon_name: "document-edit", - connect_clicked => PostInput::OpenEditPostDialog, + connect_clicked => PostPageInput::OpenEditPostDialog, set_margin_start: 5, } } else { @@ -152,7 +152,7 @@ impl SimpleComponent for PostPage { if model.info.post_view.creator.id.0 == settings::get_current_account().id { gtk::Button { set_icon_name: "edit-delete", - connect_clicked => PostInput::DeletePost, + connect_clicked => PostPageInput::DeletePost, set_margin_start: 5, } } else { @@ -178,7 +178,7 @@ impl SimpleComponent for PostPage { gtk::Button { set_label: "Comment", set_margin_start: 10, - connect_clicked => PostInput::OpenCreateCommentDialog, + connect_clicked => PostPageInput::OpenCreateCommentDialog, } } else { gtk::Box {} @@ -208,8 +208,8 @@ impl SimpleComponent for PostPage { .transient_for(root) .launch(EditorType::Comment) .forward(sender.input_sender(), |msg| match msg { - EditorOutput::CreateRequest(comment, _) => PostInput::CreateCommentRequest(comment), - EditorOutput::EditRequest(post, _) => PostInput::EditPostRequest(post), + EditorOutput::CreateRequest(comment, _) => PostPageInput::CreateCommentRequest(comment), + EditorOutput::EditRequest(post, _) => PostPageInput::EditPostRequest(post), }); let voting_row = VotingRowModel::builder() .launch(VotingStats::default()) @@ -236,7 +236,7 @@ impl SimpleComponent for PostPage { fn update(&mut self, message: Self::Input, sender: ComponentSender) { match message { - PostInput::UpdatePost(post) => { + PostPageInput::UpdatePost(post) => { self.info = post.clone(); self.image @@ -259,28 +259,28 @@ impl SimpleComponent for PostPage { } let comments = api::post::get_comments(post.post_view.post.id); if let Ok(comments) = comments { - sender.input(PostInput::DoneFetchComments(comments)); + sender.input(PostPageInput::DoneFetchComments(comments)); } }); } - PostInput::DoneFetchComments(comments) => { + PostPageInput::DoneFetchComments(comments) => { for comment in comments { self.comments.guard().push_back(comment); } } - PostInput::OpenPerson => { + PostPageInput::OpenPerson => { let person_id = self.info.post_view.creator.id.clone(); sender .output_sender() .emit(crate::AppMsg::OpenPerson(person_id)); } - PostInput::OpenCommunity => { + PostPageInput::OpenCommunity => { let community_id = self.info.community_view.community.id.clone(); sender .output_sender() .emit(crate::AppMsg::OpenCommunity(community_id)); } - PostInput::OpenLink => { + PostPageInput::OpenLink => { let post = self.info.post_view.post.clone(); let mut link = get_web_image_url(post.url); if link.is_empty() { @@ -294,19 +294,19 @@ impl SimpleComponent for PostPage { } gtk::show_uri(None::<&relm4::gtk::Window>, &link, 0); } - PostInput::OpenCreateCommentDialog => { + PostPageInput::OpenCreateCommentDialog => { let sender = self.create_comment_dialog.sender(); sender.emit(DialogMsg::UpdateType(EditorType::Comment, true)); sender.emit(DialogMsg::Show); } - PostInput::CreatedComment(comment) => { + PostPageInput::CreatedComment(comment) => { self.comments.guard().push_front(comment); } - PostInput::CreateCommentRequest(post) => { + PostPageInput::CreateCommentRequest(post) => { let id = self.info.post_view.post.id; std::thread::spawn(move || { let message = match api::comment::create_comment(id, post.body, None) { - Ok(comment) => Some(PostInput::CreatedComment(comment.comment_view)), + Ok(comment) => Some(PostPageInput::CreatedComment(comment.comment_view)), Err(err) => { println!("{}", err.to_string()); None @@ -317,7 +317,7 @@ impl SimpleComponent for PostPage { }; }); } - PostInput::DeletePost => { + PostPageInput::DeletePost => { let post_id = self.info.post_view.post.id; std::thread::spawn(move || { let _ = api::post::delete_post(post_id); @@ -326,7 +326,7 @@ impl SimpleComponent for PostPage { .emit(crate::AppMsg::StartFetchPosts(None, true)); }); } - PostInput::OpenEditPostDialog => { + PostPageInput::OpenEditPostDialog => { let url = match self.info.post_view.post.url.clone() { Some(url) => url.to_string(), None => String::from(""), @@ -347,11 +347,11 @@ impl SimpleComponent for PostPage { sender.emit(DialogMsg::UpdateType(EditorType::Post, false)); sender.emit(DialogMsg::Show); } - PostInput::EditPostRequest(post) => { + PostPageInput::EditPostRequest(post) => { let id = self.info.post_view.post.id.0; std::thread::spawn(move || { let message = match api::post::edit_post(post.name, post.url, post.body, id) { - Ok(post) => Some(PostInput::DoneEditPost(post.post_view)), + Ok(post) => Some(PostPageInput::DoneEditPost(post.post_view)), Err(err) => { println!("{}", err.to_string()); None @@ -362,10 +362,10 @@ impl SimpleComponent for PostPage { }; }); } - PostInput::DoneEditPost(post) => { + PostPageInput::DoneEditPost(post) => { self.info.post_view = post; } - PostInput::PassAppMessage(message) => { + PostPageInput::PassAppMessage(message) => { sender.output_sender().emit(message); } } diff --git a/src/main.rs b/src/main.rs index 790151c..ad247cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -453,7 +453,7 @@ impl SimpleComponent for App { AppMsg::DoneFetchPost(post) => { self.post_page .sender() - .emit(post_page::PostInput::UpdatePost(post)); + .emit(post_page::PostPageInput::UpdatePost(post)); self.state = AppState::Post; } AppMsg::ShowLogin => {