Skip to content

Commit

Permalink
feat: change increment view logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dancixx committed Sep 10, 2024
1 parent b2fa5f4 commit cb10092
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
12 changes: 2 additions & 10 deletions src/home.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use leptos::*;
use leptos_router::use_navigate;

use crate::api::{increment_views, select_posts, select_tags};
use crate::api::{select_posts, select_tags};

#[component]
pub fn Component() -> impl IntoView {
Expand All @@ -15,12 +15,6 @@ pub fn Component() -> impl IntoView {
move || selected_tags.get(),
move |selected_tags| async move { select_posts(selected_tags).await.unwrap_or_default() },
);
let increment_view = create_action(move |id: &String| {
let id = id.clone();
async move {
let _ = increment_views(id.to_string()).await;
}
});

view! {
<Suspense fallback=|| ()>
Expand Down Expand Up @@ -92,8 +86,6 @@ pub fn Component() -> impl IntoView {
view! {
<article
on:click=move |_| {
#[cfg(not(debug_assertions))]
increment_view.dispatch(post.id.id.to_string());
navigate(
&format!("/post/{}", post.slug.as_ref().map_or("", |v| v)),
Default::default(),
Expand All @@ -115,7 +107,7 @@ pub fn Component() -> impl IntoView {
<p>{format!("{} views", post.total_views)}</p>
<p>{post.created_at}</p>
<a
href={post.author.github.unwrap_or_default()}
href=post.author.github.unwrap_or_default()
target="_blank"
rel="noopener noreferrer"
on:click=move |e| {
Expand Down
14 changes: 13 additions & 1 deletion src/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use leptos::*;
use leptos_meta::*;
use leptos_router::*;

use crate::api::select_post;
use crate::api::{increment_views, select_post};

#[component]
pub fn Component() -> impl IntoView {
Expand All @@ -12,6 +12,18 @@ pub fn Component() -> impl IntoView {
|| (),
move |_| async move { select_post(slug()).await.unwrap() },
);
let increment_view = create_action(move |id: &String| {
let id = id.clone();
async move {
let _ = increment_views(id.to_string()).await;
}
});
create_render_effect(move |_| {
#[cfg(not(debug_assertions))]
if post.get().is_some() {
increment_view.dispatch(post.get().as_ref().unwrap().id.id.to_string());
}
});

view! {
<Suspense fallback=|| ()>
Expand Down

0 comments on commit cb10092

Please sign in to comment.