feat: user crm metrics, product total sold, dynamic menu sync, blog views & advisor route
This commit is contained in:
1 parent
9294e89712
commit
6e23269a5c
24 files changed
+12910
-12334
No files matched your search
@@ -1,5 +1,5 @@
|
||||
"use client";
|
||||
import React, { useState } from "react";
|
||||
import React, { useState, useEffect } from "react";
|
||||
import {
|
||||
ChevronRight,
|
||||
Calendar,
|
||||
@@ -13,9 +13,11 @@ import {
|
||||
ArrowLeft,
|
||||
Send,
|
||||
Eye,
|
||||
CheckCircle2,
|
||||
} from "lucide-react";
|
||||
import Link from 'next/link';
|
||||
import SafeImage from "./SafeImage";
|
||||
import api from "../lib/services/api";
|
||||
|
||||
interface BlogPostClientProps {
|
||||
blog: any;
|
||||
@@ -25,15 +27,37 @@ interface BlogPostClientProps {
|
||||
|
||||
export default function BlogPostClient({ blog, publishedDate, authorName }: BlogPostClientProps) {
|
||||
const [copied, setCopied] = useState(false);
|
||||
const [viewCount, setViewCount] = useState<number>(blog.viewCount || 0);
|
||||
const [commentName, setCommentName] = useState("");
|
||||
const [commentEmail, setCommentEmail] = useState("");
|
||||
const [commentText, setCommentText] = useState("");
|
||||
const [commentSent, setCommentSent] = useState(false);
|
||||
const [isSubmittingComment, setIsSubmittingComment] = useState(false);
|
||||
const [commentError, setCommentError] = useState("");
|
||||
|
||||
const relatedProducts = Array.isArray(blog.relatedProducts) ? blog.relatedProducts : [];
|
||||
const relatedBlogs = Array.isArray(blog.relatedBlogs) ? blog.relatedBlogs : [];
|
||||
const comments = Array.isArray(blog.comments) ? blog.comments : [];
|
||||
|
||||
useEffect(() => {
|
||||
if (!blog?.slug) return;
|
||||
const viewKey = `viewed_blog_${blog.slug}`;
|
||||
const hasViewed = sessionStorage.getItem(viewKey);
|
||||
|
||||
api.post(`/blogs/${encodeURIComponent(blog.slug)}/view`)
|
||||
.then((res) => {
|
||||
if (res.data?.viewCount !== undefined) {
|
||||
setViewCount(res.data.viewCount);
|
||||
} else {
|
||||
setViewCount((prev) => prev + (hasViewed ? 0 : 1));
|
||||
}
|
||||
sessionStorage.setItem(viewKey, '1');
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('Failed to register view count:', err);
|
||||
});
|
||||
}, [blog?.slug]);
|
||||
|
||||
const handleShare = () => {
|
||||
if (typeof window !== 'undefined') {
|
||||
if (navigator.share) {
|
||||
@@ -46,12 +70,28 @@ export default function BlogPostClient({ blog, publishedDate, authorName }: Blog
|
||||
}
|
||||
};
|
||||
|
||||
const handleCommentSubmit = (e: React.FormEvent) => {
|
||||
const handleCommentSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setCommentSent(true);
|
||||
setCommentName("");
|
||||
setCommentEmail("");
|
||||
setCommentText("");
|
||||
if (!commentText.trim() || !commentName.trim()) return;
|
||||
|
||||
try {
|
||||
setIsSubmittingComment(true);
|
||||
setCommentError("");
|
||||
await api.post(`/blogs/${encodeURIComponent(blog.slug || blog.id)}/comments`, {
|
||||
userName: commentName.trim(),
|
||||
email: commentEmail.trim() || undefined,
|
||||
content: commentText.trim(),
|
||||
});
|
||||
setCommentSent(true);
|
||||
setCommentName("");
|
||||
setCommentEmail("");
|
||||
setCommentText("");
|
||||
} catch (err: any) {
|
||||
console.error('Failed to submit comment:', err);
|
||||
setCommentError(err.response?.data?.message || "خطا در ارسال دیدگاه. لطفاً دوباره تلاش کنید.");
|
||||
} finally {
|
||||
setIsSubmittingComment(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -116,7 +156,7 @@ export default function BlogPostClient({ blog, publishedDate, authorName }: Blog
|
||||
</div>
|
||||
<div className="flex items-center gap-1">
|
||||
<Eye className="w-3.5 h-3.5 text-canina-blue" />
|
||||
<span>{blog.viewCount || 0} بازدید</span>
|
||||
<span>{Number(viewCount).toLocaleString('fa-IR')} بازدید</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in new issue
Block a user