Skip to content

Commit

Permalink
started working on analytics page
Browse files Browse the repository at this point in the history
  • Loading branch information
chuma-beep committed Oct 27, 2024
1 parent 5255a51 commit 3ce010d
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 279 deletions.
50 changes: 46 additions & 4 deletions app/post/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import 'react-toastify/dist/ReactToastify.css';
import { useCreateBlockNote } from "@blocknote/react";
import { BlockNoteView, lightDefaultTheme } from "@blocknote/mantine";
import "@blocknote/mantine/style.css";



import '@blocknote/core/fonts/inter.css';
import { Skeleton } from "@/components/ui/skeleton";
import { MdOutlineDelete } from "react-icons/md";
Expand All @@ -29,6 +26,13 @@ import Link from "next/link";
// Removed unused import
// import TagsPostsPage from '../../tags/[tag]/page';



interface User{
id: string | number;
}


interface Profile {
username: string;
avatar_url: string;
Expand Down Expand Up @@ -79,6 +83,8 @@ export default function PostPage() {
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [tags, setTags] = useState<string[]>([]);
const [currentUser, setCurrentUser] = useState<User | null>(null);


const editor = useCreateBlockNote();

Expand All @@ -92,7 +98,7 @@ export default function PostPage() {
return;
}

if (!isValidUuid(postId)) { // Validate UUID format
if (!isValidUuid(postId)) {
setError("Invalid Post ID format");
return;
}
Expand Down Expand Up @@ -121,6 +127,12 @@ export default function PostPage() {

setPost(postData as Post);

//analytics





// Fetch comments for the post
const { data: commentsData, error: commentsError } = await supabase
.from("comments")
Expand Down Expand Up @@ -279,6 +291,11 @@ export default function PostPage() {
return;
}


await supabase.from('comments')



const { data: newCommentData, error } = await supabase
.from("comments")
.insert([{
Expand Down Expand Up @@ -327,6 +344,7 @@ export default function PostPage() {
return;
}


const userId = userData?.user?.id;
if (!userId) {
toast.error("you can't delete another users comment");
Expand Down Expand Up @@ -371,6 +389,30 @@ export default function PostPage() {
}
};




async function logPostView() {
if (!currentUser) return;

const { error } = await supabase
.from('views')
.insert([{ post_id: postId, user_id: currentUser.id }]);

if (error) {
console.error("Error logging view:", error);
}
}

useEffect(() => {
if (post) {
logPostView();
}
}, [post]);




// Render loading skeletons while data is being fetched
if (loading) {
return (
Expand Down
Loading

0 comments on commit 3ce010d

Please sign in to comment.