Skip to content

Commit

Permalink
feat: add comment post functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
thraizz committed Jan 27, 2024
1 parent 1ff68cc commit 0c7b9cb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
33 changes: 32 additions & 1 deletion src/comments.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { collection, getDocs } from "firebase/firestore";
import { addDoc, collection, getDocs } from "firebase/firestore";
import { defineStore } from "pinia";
import { ref } from "vue";

Expand Down Expand Up @@ -39,6 +39,36 @@ export const useCommentStore = defineStore("comments", () => {
return comments.value.filter((comment) => comment.projectId === projectUid);
};

const addCommentToProject = (
comment: string,
projectUid: string,
userUid: string,
) => {
const commentData = {
userId: userUid,
projectId: projectUid,
textContent: comment,
timestamp: new Date(),
upvotes: [],
};

const commentCollection = collection(firestore, "comments");
addDoc(commentCollection, {
...commentData,
timestamp: commentData.timestamp.toISOString(),
})
.then((data) => {
console.log(data);
comments.value = [
...comments.value,
{ ...commentData, _id: data.id } as Comment,
];
})
.catch((error) => {
console.error("Error writing document: ", error);
});
};

// function upvoteProject(_id: string, uid?: string) {
// const project = comments.value.find((p) => p._id === _id);
// if (!project) return;
Expand Down Expand Up @@ -66,5 +96,6 @@ export const useCommentStore = defineStore("comments", () => {
projects: comments,
refetch,
getCommentsByProjectUid,
addCommentToProject,
};
});
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export type Project = {
// Comment Type
export type Comment = {
_id: string;
commentId: string;
userId: string; // author's user ID
projectId: string; // associated project ID
textContent: string;
Expand Down
9 changes: 6 additions & 3 deletions src/views/AddCommentForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const { handleSubmit } = useForm<FormData>({
const onSubmit = handleSubmit(
// Success
(values: FormData) => {
if (!user.value) return;
if (!user.value?.uid) return;
// handle form submission here
commentStore.addCommentToProject(
values.comment,
Expand All @@ -45,13 +45,16 @@ const { value: comment, errorMessage: commentError } =
</script>

<template>
<div class="mt-6 flex gap-x-3">
<div v-if="user?.uid" class="mt-6 flex gap-x-3">
<img
src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80"
v-if="user?.photoURL && user.photoURL !== ''"
:src="user.photoURL"
alt=""
class="h-6 w-6 flex-none rounded-full bg-gray-50"
/>

<div v-else class="h-6 w-6 flex-none" />

<div class="w-full">
<form class="relative flex-auto" @submit="onSubmit">
<div
Expand Down

0 comments on commit 0c7b9cb

Please sign in to comment.