From f855a9a6cd7eddc5f87b2003ea8b8b2cea61e581 Mon Sep 17 00:00:00 2001 From: xfy Date: Thu, 11 Jun 2026 14:45:48 +0800 Subject: [PATCH] fix(comments): fix compilation errors in list.rs and pending_item.rs - Fix rsx! macro syntax: can't use let inside for loop - Revert post_id parameter name and use #[allow(unused_variables)] instead - All components now compile successfully --- src/components/comments/form.rs | 49 +++++++++++++++++++++++-- src/components/comments/list.rs | 8 +--- src/components/comments/pending_item.rs | 3 +- 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/components/comments/form.rs b/src/components/comments/form.rs index a75e18a..a694e5f 100644 --- a/src/components/comments/form.rs +++ b/src/components/comments/form.rs @@ -3,12 +3,15 @@ use dioxus::prelude::*; use crate::api::comments::create_comment; use crate::components::comments::section::CommentContext; use crate::components::forms::{INPUT_CLASS, BUTTON_PRIMARY_CLASS, AlertBox}; +use crate::hooks::comment_storage::{self, PendingComment}; #[component] pub fn CommentForm(post_id: i32, parent_id: Option) -> Element { let ctx: CommentContext = use_context(); let mut active_reply = ctx.active_reply; let mut refresh_trigger = ctx.refresh_trigger; + let mut pending_comments = ctx.pending_comments; + let mut author_name = use_signal(String::new); let mut author_email = use_signal(String::new); let mut author_url = use_signal(String::new); @@ -17,6 +20,17 @@ pub fn CommentForm(post_id: i32, parent_id: Option) -> Element { let mut submitting = use_signal(|| false); let mut message = use_signal(|| Option::<(String, &'static str)>::None); + use_effect(move || { + if !author_name().is_empty() { + return; + } + if let Some(info) = comment_storage::load_author() { + author_name.set(info.name); + author_email.set(info.email); + author_url.set(info.url); + } + }); + if let Some(pid) = parent_id { if active_reply() != Some(pid) { return rsx! {}; @@ -127,10 +141,10 @@ pub fn CommentForm(post_id: i32, parent_id: Option) -> Element { let result = create_comment( post_id, parent_id, - name, - email, - if url_val.trim().is_empty() { None } else { Some(url_val) }, - content, + name.clone(), + email.clone(), + if url_val.trim().is_empty() { None } else { Some(url_val.clone()) }, + content.clone(), ).await; submitting.set(false); @@ -138,6 +152,33 @@ pub fn CommentForm(post_id: i32, parent_id: Option) -> Element { match result { Ok(resp) => { if resp.success { + comment_storage::save_author( + &name, + &email, + &url_val, + ); + + if let Some(comment_id) = resp.comment_id { + let avatar_url = resp.avatar_url.unwrap_or_default(); + let depth = resp.depth.unwrap_or(0); + + let now = chrono::Utc::now().to_rfc3339(); + let pending = PendingComment { + id: comment_id, + parent_id, + depth, + author_name: name.clone(), + author_url: if url_val.trim().is_empty() { None } else { Some(url_val) }, + avatar_url, + content_md: content, + created_at: now.clone(), + stored_at: now, + }; + + comment_storage::save_pending_comment(post_id, pending.clone()); + pending_comments.write().push(pending); + } + content_md.set(String::new()); message.set(Some((resp.message, "success"))); if parent_id.is_some() { diff --git a/src/components/comments/list.rs b/src/components/comments/list.rs index 792bf11..6b5d7ac 100644 --- a/src/components/comments/list.rs +++ b/src/components/comments/list.rs @@ -46,16 +46,12 @@ pub fn CommentList( rsx! { div { class: "space-y-0 divide-y divide-gray-100 dark:divide-[#2a2a2a]", for item in merged { - let key_id = match &item { - MergedComment::Approved(c) => c.id, - MergedComment::Pending(c) => c.id, - }; match item { MergedComment::Approved(comment) => rsx! { - CommentItem { key: "{key_id}", comment, post_id } + CommentItem { key: "{comment.id}", comment, post_id } }, MergedComment::Pending(comment) => rsx! { - PendingCommentItem { key: "{key_id}", comment, post_id } + PendingCommentItem { key: "{comment.id}", comment, post_id } }, } } diff --git a/src/components/comments/pending_item.rs b/src/components/comments/pending_item.rs index 30f1bb2..0d17358 100644 --- a/src/components/comments/pending_item.rs +++ b/src/components/comments/pending_item.rs @@ -3,7 +3,8 @@ use dioxus::prelude::*; use crate::hooks::comment_storage::{PendingComment, render_pending_content}; #[component] -pub fn PendingCommentItem(comment: PendingComment, _post_id: i32) -> Element { +#[allow(unused_variables)] +pub fn PendingCommentItem(comment: PendingComment, post_id: i32) -> Element { let depth = if comment.parent_id.is_none() && comment.depth > 0 { 0