Show new created comment reply after creation

This commit is contained in:
Bnyro 2023-07-01 17:25:27 +02:00
parent 8fe42f3838
commit 25d6e98a17
3 changed files with 35 additions and 33 deletions

View File

@ -14,7 +14,7 @@ use crate::util;
use crate::util::get_web_image_url; use crate::util::get_web_image_url;
use crate::util::markdown_to_pango_markup; 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::VotingRowModel;
use super::voting_row::VotingStats; use super::voting_row::VotingStats;
@ -39,10 +39,10 @@ pub enum CommentRowMsg {
impl FactoryComponent for CommentRow { impl FactoryComponent for CommentRow {
type Init = CommentView; type Init = CommentView;
type Input = CommentRowMsg; type Input = CommentRowMsg;
type Output = PostInput; type Output = PostPageInput;
type CommandOutput = (); type CommandOutput = ();
type Widgets = PostViewWidgets; type Widgets = PostViewWidgets;
type ParentInput = PostInput; type ParentInput = PostPageInput;
type ParentWidget = gtk::Box; type ParentWidget = gtk::Box;
view! { view! {
@ -171,7 +171,7 @@ impl FactoryComponent for CommentRow {
fn update(&mut self, message: Self::Input, sender: FactorySender<Self>) { fn update(&mut self, message: Self::Input, sender: FactorySender<Self>) {
match message { match message {
CommentRowMsg::OpenPerson => { CommentRowMsg::OpenPerson => {
sender.output(PostInput::PassAppMessage(crate::AppMsg::OpenPerson( sender.output(PostPageInput::PassAppMessage(crate::AppMsg::OpenPerson(
self.comment.creator.id.clone(), self.comment.creator.id.clone(),
))); )));
} }
@ -179,7 +179,7 @@ impl FactoryComponent for CommentRow {
let comment_id = self.comment.comment.id; let comment_id = self.comment.comment.id;
std::thread::spawn(move || { std::thread::spawn(move || {
let _ = api::comment::delete_comment(comment_id); let _ = api::comment::delete_comment(comment_id);
sender.output_sender().emit(PostInput::PassAppMessage( sender.output_sender().emit(PostPageInput::PassAppMessage(
crate::AppMsg::StartFetchPosts(None, true), crate::AppMsg::StartFetchPosts(None, true),
)); ));
}); });
@ -223,7 +223,9 @@ impl FactoryComponent for CommentRow {
std::thread::spawn(move || { std::thread::spawn(move || {
match api::comment::create_comment(post_id, data.body, Some(parent_id)) 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) => { Err(err) => {
println!("{}", err.to_string()); println!("{}", err.to_string());
} }

View File

@ -30,7 +30,7 @@ pub struct PostPage {
} }
#[derive(Debug)] #[derive(Debug)]
pub enum PostInput { pub enum PostPageInput {
UpdatePost(GetPostResponse), UpdatePost(GetPostResponse),
DoneFetchComments(Vec<CommentView>), DoneFetchComments(Vec<CommentView>),
OpenPerson, OpenPerson,
@ -49,7 +49,7 @@ pub enum PostInput {
#[relm4::component(pub)] #[relm4::component(pub)]
impl SimpleComponent for PostPage { impl SimpleComponent for PostPage {
type Init = GetPostResponse; type Init = GetPostResponse;
type Input = PostInput; type Input = PostPageInput;
type Output = crate::AppMsg; type Output = crate::AppMsg;
view! { view! {
@ -102,7 +102,7 @@ impl SimpleComponent for PostPage {
gtk::Button { gtk::Button {
#[watch] #[watch]
set_label: &model.info.post_view.creator.name, set_label: &model.info.post_view.creator.name,
connect_clicked => PostInput::OpenPerson, connect_clicked => PostPageInput::OpenPerson,
}, },
gtk::Label { gtk::Label {
@ -122,7 +122,7 @@ impl SimpleComponent for PostPage {
gtk::Button { gtk::Button {
#[watch] #[watch]
set_label: &model.info.community_view.community.title, set_label: &model.info.community_view.community.title,
connect_clicked => PostInput::OpenCommunity, connect_clicked => PostPageInput::OpenCommunity,
}, },
gtk::Label { gtk::Label {
@ -136,13 +136,13 @@ impl SimpleComponent for PostPage {
gtk::Button { gtk::Button {
set_label: "View", set_label: "View",
connect_clicked => PostInput::OpenLink, connect_clicked => PostPageInput::OpenLink,
}, },
if model.info.post_view.creator.id.0 == settings::get_current_account().id { if model.info.post_view.creator.id.0 == settings::get_current_account().id {
gtk::Button { gtk::Button {
set_icon_name: "document-edit", set_icon_name: "document-edit",
connect_clicked => PostInput::OpenEditPostDialog, connect_clicked => PostPageInput::OpenEditPostDialog,
set_margin_start: 5, set_margin_start: 5,
} }
} else { } else {
@ -152,7 +152,7 @@ impl SimpleComponent for PostPage {
if model.info.post_view.creator.id.0 == settings::get_current_account().id { if model.info.post_view.creator.id.0 == settings::get_current_account().id {
gtk::Button { gtk::Button {
set_icon_name: "edit-delete", set_icon_name: "edit-delete",
connect_clicked => PostInput::DeletePost, connect_clicked => PostPageInput::DeletePost,
set_margin_start: 5, set_margin_start: 5,
} }
} else { } else {
@ -178,7 +178,7 @@ impl SimpleComponent for PostPage {
gtk::Button { gtk::Button {
set_label: "Comment", set_label: "Comment",
set_margin_start: 10, set_margin_start: 10,
connect_clicked => PostInput::OpenCreateCommentDialog, connect_clicked => PostPageInput::OpenCreateCommentDialog,
} }
} else { } else {
gtk::Box {} gtk::Box {}
@ -208,8 +208,8 @@ impl SimpleComponent for PostPage {
.transient_for(root) .transient_for(root)
.launch(EditorType::Comment) .launch(EditorType::Comment)
.forward(sender.input_sender(), |msg| match msg { .forward(sender.input_sender(), |msg| match msg {
EditorOutput::CreateRequest(comment, _) => PostInput::CreateCommentRequest(comment), EditorOutput::CreateRequest(comment, _) => PostPageInput::CreateCommentRequest(comment),
EditorOutput::EditRequest(post, _) => PostInput::EditPostRequest(post), EditorOutput::EditRequest(post, _) => PostPageInput::EditPostRequest(post),
}); });
let voting_row = VotingRowModel::builder() let voting_row = VotingRowModel::builder()
.launch(VotingStats::default()) .launch(VotingStats::default())
@ -236,7 +236,7 @@ impl SimpleComponent for PostPage {
fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>) { fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>) {
match message { match message {
PostInput::UpdatePost(post) => { PostPageInput::UpdatePost(post) => {
self.info = post.clone(); self.info = post.clone();
self.image self.image
@ -259,28 +259,28 @@ impl SimpleComponent for PostPage {
} }
let comments = api::post::get_comments(post.post_view.post.id); let comments = api::post::get_comments(post.post_view.post.id);
if let Ok(comments) = comments { 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 { for comment in comments {
self.comments.guard().push_back(comment); self.comments.guard().push_back(comment);
} }
} }
PostInput::OpenPerson => { PostPageInput::OpenPerson => {
let person_id = self.info.post_view.creator.id.clone(); let person_id = self.info.post_view.creator.id.clone();
sender sender
.output_sender() .output_sender()
.emit(crate::AppMsg::OpenPerson(person_id)); .emit(crate::AppMsg::OpenPerson(person_id));
} }
PostInput::OpenCommunity => { PostPageInput::OpenCommunity => {
let community_id = self.info.community_view.community.id.clone(); let community_id = self.info.community_view.community.id.clone();
sender sender
.output_sender() .output_sender()
.emit(crate::AppMsg::OpenCommunity(community_id)); .emit(crate::AppMsg::OpenCommunity(community_id));
} }
PostInput::OpenLink => { PostPageInput::OpenLink => {
let post = self.info.post_view.post.clone(); let post = self.info.post_view.post.clone();
let mut link = get_web_image_url(post.url); let mut link = get_web_image_url(post.url);
if link.is_empty() { if link.is_empty() {
@ -294,19 +294,19 @@ impl SimpleComponent for PostPage {
} }
gtk::show_uri(None::<&relm4::gtk::Window>, &link, 0); gtk::show_uri(None::<&relm4::gtk::Window>, &link, 0);
} }
PostInput::OpenCreateCommentDialog => { PostPageInput::OpenCreateCommentDialog => {
let sender = self.create_comment_dialog.sender(); let sender = self.create_comment_dialog.sender();
sender.emit(DialogMsg::UpdateType(EditorType::Comment, true)); sender.emit(DialogMsg::UpdateType(EditorType::Comment, true));
sender.emit(DialogMsg::Show); sender.emit(DialogMsg::Show);
} }
PostInput::CreatedComment(comment) => { PostPageInput::CreatedComment(comment) => {
self.comments.guard().push_front(comment); self.comments.guard().push_front(comment);
} }
PostInput::CreateCommentRequest(post) => { PostPageInput::CreateCommentRequest(post) => {
let id = self.info.post_view.post.id; let id = self.info.post_view.post.id;
std::thread::spawn(move || { std::thread::spawn(move || {
let message = match api::comment::create_comment(id, post.body, None) { 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) => { Err(err) => {
println!("{}", err.to_string()); println!("{}", err.to_string());
None None
@ -317,7 +317,7 @@ impl SimpleComponent for PostPage {
}; };
}); });
} }
PostInput::DeletePost => { PostPageInput::DeletePost => {
let post_id = self.info.post_view.post.id; let post_id = self.info.post_view.post.id;
std::thread::spawn(move || { std::thread::spawn(move || {
let _ = api::post::delete_post(post_id); let _ = api::post::delete_post(post_id);
@ -326,7 +326,7 @@ impl SimpleComponent for PostPage {
.emit(crate::AppMsg::StartFetchPosts(None, true)); .emit(crate::AppMsg::StartFetchPosts(None, true));
}); });
} }
PostInput::OpenEditPostDialog => { PostPageInput::OpenEditPostDialog => {
let url = match self.info.post_view.post.url.clone() { let url = match self.info.post_view.post.url.clone() {
Some(url) => url.to_string(), Some(url) => url.to_string(),
None => String::from(""), None => String::from(""),
@ -347,11 +347,11 @@ impl SimpleComponent for PostPage {
sender.emit(DialogMsg::UpdateType(EditorType::Post, false)); sender.emit(DialogMsg::UpdateType(EditorType::Post, false));
sender.emit(DialogMsg::Show); sender.emit(DialogMsg::Show);
} }
PostInput::EditPostRequest(post) => { PostPageInput::EditPostRequest(post) => {
let id = self.info.post_view.post.id.0; let id = self.info.post_view.post.id.0;
std::thread::spawn(move || { std::thread::spawn(move || {
let message = match api::post::edit_post(post.name, post.url, post.body, id) { 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) => { Err(err) => {
println!("{}", err.to_string()); println!("{}", err.to_string());
None None
@ -362,10 +362,10 @@ impl SimpleComponent for PostPage {
}; };
}); });
} }
PostInput::DoneEditPost(post) => { PostPageInput::DoneEditPost(post) => {
self.info.post_view = post; self.info.post_view = post;
} }
PostInput::PassAppMessage(message) => { PostPageInput::PassAppMessage(message) => {
sender.output_sender().emit(message); sender.output_sender().emit(message);
} }
} }

View File

@ -453,7 +453,7 @@ impl SimpleComponent for App {
AppMsg::DoneFetchPost(post) => { AppMsg::DoneFetchPost(post) => {
self.post_page self.post_page
.sender() .sender()
.emit(post_page::PostInput::UpdatePost(post)); .emit(post_page::PostPageInput::UpdatePost(post));
self.state = AppState::Post; self.state = AppState::Post;
} }
AppMsg::ShowLogin => { AppMsg::ShowLogin => {