From 8ee911e257d54ab6360ba7c4c41bbcd5adafd146 Mon Sep 17 00:00:00 2001 From: auraticabhi Date: Fri, 15 Mar 2024 13:16:48 +0530 Subject: [PATCH 1/6] Generating keywords and adding to Posts --- app/page.tsx | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/app/page.tsx b/app/page.tsx index f9bbb33..7f212d4 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -53,7 +53,7 @@ import { auth , db , storage } from "@/utils/firebase"; import { useAuthState } from "react-firebase-hooks/auth"; import { useRouter , useSearchParams } from "next/navigation"; -import { addDoc, collection, serverTimestamp } from "firebase/firestore"; +import { addDoc, collection, doc, onSnapshot, serverTimestamp, updateDoc } from "firebase/firestore"; import { ref , uploadBytes, uploadBytesResumable , getDownloadURL} from "firebase/storage"; import { DialogClose } from "@radix-ui/react-dialog"; @@ -159,6 +159,8 @@ export default function Home() { }); async function createQuestionPost(data: Input) { + + console.log("creating"); const docRef = await addDoc(collection(db, "questions"), { title: data.title, @@ -172,13 +174,42 @@ export default function Home() { // ansNumbers: 0, }); + const quesId = docRef.id; + toast({ title: "Question Posted", description: "Your question has been posted successfully.", }); + try { + console.log("keyword Gen.....") + const docRef = await addDoc(collection(db, 'keywords'), { + prompt: `Generate some keywords on the topic ${data.title}`, + }); + console.log('Keyword Document written with ID: ', docRef.id); + + // Listen for changes to the document + const unsubscribe = onSnapshot(doc(db, 'keywords', docRef.id), async(snap) => { + const data = snap.data(); + if (data && data.response) { + console.log('RESPONSE: ' + data.response); + const keywordsString = `${data.response}`; + + const questionDocRef = doc(db, 'questions', quesId); + await updateDoc(questionDocRef, { + keywords: keywordsString, // Add your keywords here + }); + } + }); + + // Stop listening after some time (for demonstration purposes) + setTimeout(() => unsubscribe(), 60000); + } catch (error) { + console.error('Error adding document: ', error); + } + console.log("Document written with ID: ", docRef.id); - console.log(data); + //console.log(data); } function onSubmit(data: Input) { From 1cfa915bab4ded5629c21a36052d5f685cf9bb83 Mon Sep 17 00:00:00 2001 From: auraticabhi Date: Fri, 15 Mar 2024 14:22:55 +0530 Subject: [PATCH 2/6] Displaying keywords on post page --- app/[postTitle]/page.tsx | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/app/[postTitle]/page.tsx b/app/[postTitle]/page.tsx index 81e476b..166e2de 100644 --- a/app/[postTitle]/page.tsx +++ b/app/[postTitle]/page.tsx @@ -54,6 +54,8 @@ import { Tiptap } from "@/components/TipTapAns"; import { z } from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { AnswerDescriptionType } from "@/schemas/answer"; +import { Table, TableBody, TableHead, TableHeader, TableRow } from "@/components/ui/table"; +import { Separator } from "@/components/ui/separator"; type Input = z.infer; @@ -87,6 +89,7 @@ type QuestionType = { shares: number; questionImageURL: string; createdAt: string; + keywords: string; anonymity: boolean; // Add any other fields as necessary }; @@ -359,9 +362,29 @@ const PostPage = ({ params: { postTitle } }: Props) => { +
+
+ +
+ + + + HashTags + + + + +
+ {queObject.keywords?queObject.keywords:"No tags Available"} +
+
+
+
+
+
); }; From 597f5ed78ca695c07a169112011b42920144adbf Mon Sep 17 00:00:00 2001 From: auraticabhi Date: Fri, 15 Mar 2024 15:21:23 +0530 Subject: [PATCH 3/6] more changes --- components/Navbar.tsx | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/components/Navbar.tsx b/components/Navbar.tsx index 4b69059..b309c2d 100644 --- a/components/Navbar.tsx +++ b/components/Navbar.tsx @@ -80,7 +80,7 @@ import { QuestionType } from "@/schemas/question"; import { db , storage } from "@/utils/firebase"; import { useSearchParams } from "next/navigation"; -import { addDoc, collection, serverTimestamp } from "firebase/firestore"; +import { addDoc, collection, doc, onSnapshot, serverTimestamp, updateDoc } from "firebase/firestore"; import { ref , uploadBytes, uploadBytesResumable , getDownloadURL} from "firebase/storage"; import { DialogClose } from "@radix-ui/react-dialog"; import MobileSidebar from "./MobileSidebar"; @@ -187,11 +187,40 @@ const Navbar = ({}: Props) => { // ansNumbers: 0, }); + const quesId = docRef.id; + toast({ title: "Question Posted", description: "Try refreshing in case you don't see your question", }) + try { + console.log("keyword Gen.....") + const docRef = await addDoc(collection(db, 'keywords'), { + prompt: `Generate some keywords on the topic ${data.title}`, + }); + console.log('Keyword Document written with ID: ', docRef.id); + + // Listen for changes to the document + const unsubscribe = onSnapshot(doc(db, 'keywords', docRef.id), async(snap) => { + const data = snap.data(); + if (data && data.response) { + console.log('RESPONSE: ' + data.response); + const keywordsString = `${data.response}`; + + const questionDocRef = doc(db, 'questions', quesId); + await updateDoc(questionDocRef, { + keywords: keywordsString, // Add your keywords here + }); + } + }); + + // Stop listening after some time (for demonstration purposes) + setTimeout(() => unsubscribe(), 60000); + } catch (error) { + console.error('Error adding document: ', error); + } + console.log("Document written with ID: ", docRef.id); console.log(data); } From 0817f21e5895bd6ecffc8f43e3598fc2dc2a5a29 Mon Sep 17 00:00:00 2001 From: auraticabhi Date: Fri, 15 Mar 2024 17:05:07 +0530 Subject: [PATCH 4/6] Image preview while uploading images --- app/[postTitle]/page.tsx | 27 ++++++++++++++++++++++++++- app/page.tsx | 26 +++++++++++++++++++++++++- components/Navbar.tsx | 24 ++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/app/[postTitle]/page.tsx b/app/[postTitle]/page.tsx index 166e2de..c7208ba 100644 --- a/app/[postTitle]/page.tsx +++ b/app/[postTitle]/page.tsx @@ -8,6 +8,7 @@ import { Tiptap as TipTap } from "@/components/TipTap"; import AnsPost from "@/components/queAnsPage/AnsPost"; import RecentFeed from "@/components/queAnsPage/RecentFeed"; import imageCompression from 'browser-image-compression'; +import Image from "next/image"; import { auth, db, storage } from "@/utils/firebase"; import { @@ -130,10 +131,27 @@ const PostPage = ({ params: { postTitle } }: Props) => { const router = useRouter(); const [user, loading] = useAuthState(auth); + const [previewImg, setPreviewImg] = useState(null); const uploadImage = async(file: any) => { if (file == null) return; + if (file) { + const reader = new FileReader(); + reader.onload = (event) => { + if (event.target) { + const imageUrl = event.target.result; + setPreviewImg(imageUrl); + } else { + console.error('Error reading file:', file); + setPreviewImg(null); + } + }; + reader.readAsDataURL(file); + } else { + setPreviewImg(null); + } + const storageRef = ref(storage, `answers/${file.name}`); try { @@ -312,7 +330,14 @@ const PostPage = ({ params: { postTitle } }: Props) => { )} /> - + {(progress||0)>0&&{`${Math.ceil((progress||0))} % Uploaded`}} +
+ { + previewImg&&
+ previewImage +
+ } +
{/* anonymity toggle */} (null); const [imageUrl, setImageUrl] = useState(null); const [progress , setProgress] = useState(0); + const [previewImg, setPreviewImg] = useState(null); const uploadImage = async(file: any) => { if(file == null) return; + if (file) { + const reader = new FileReader(); + reader.onload = (event) => { + if (event.target) { + const imageUrl = event.target.result; + setPreviewImg(imageUrl); + } else { + console.error('Error reading file:', file); + setPreviewImg(null); + } + }; + reader.readAsDataURL(file); + } else { + setPreviewImg(null); + } + const storageRef = ref(storage, `questions/${file.name}`); try { @@ -267,7 +284,6 @@ export default function Home() { else { - return ( <> @@ -384,6 +400,14 @@ export default function Home() { {(progress||0)>0&&{`${Math.ceil((progress||0))} % Uploaded`}} {/* "0" to make upload percentage invisible when no image is selected */} {/* anonymity toggle */} + +
+ { + previewImg&&
+ previewImage +
+ } +
{ const [imageUpload , setImageUpload] = useState(null); const [imageUrl, setImageUrl] = useState(null); const [progress , setProgress] = useState(0); + const [previewImg, setPreviewImg] = useState(null); const dispatch = useDispatch(); @@ -131,6 +132,22 @@ const Navbar = ({}: Props) => { const uploadImage = async(file: any) => { if(file == null) return; + if (file) { + const reader = new FileReader(); + reader.onload = (event) => { + if (event.target) { + const imageUrl = event.target.result; + setPreviewImg(imageUrl); + } else { + console.error('Error reading file:', file); + setPreviewImg(null); + } + }; + reader.readAsDataURL(file); + } else { + setPreviewImg(null); + } + const storageRef = ref(storage, `questions/${file.name}`); try { @@ -408,6 +425,13 @@ const Navbar = ({}: Props) => { {(progress||0)>0&&{`${Math.ceil((progress||0))} % Uploaded`}} {/* "0" to make upload percentage invisible when no image is selected */} {/* anonymity toggle */} +
+ { + previewImg&&
+ previewImage +
+ } +
Date: Fri, 15 Mar 2024 21:08:27 +0530 Subject: [PATCH 5/6] prompt updation --- app/page.tsx | 2 +- components/Navbar.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/page.tsx b/app/page.tsx index cfed2ed..d677379 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -201,7 +201,7 @@ export default function Home() { try { console.log("keyword Gen.....") const docRef = await addDoc(collection(db, 'keywords'), { - prompt: `Generate some keywords on the topic ${data.title}`, + prompt: `Generate some keywords and hashtags on topic ${data.title}`, }); console.log('Keyword Document written with ID: ', docRef.id); diff --git a/components/Navbar.tsx b/components/Navbar.tsx index 486723b..f5ec74e 100644 --- a/components/Navbar.tsx +++ b/components/Navbar.tsx @@ -214,7 +214,7 @@ const Navbar = ({}: Props) => { try { console.log("keyword Gen.....") const docRef = await addDoc(collection(db, 'keywords'), { - prompt: `Generate some keywords on the topic ${data.title}`, + prompt: `Generate some keywords and hashtags on topic ${data.title}`, }); console.log('Keyword Document written with ID: ', docRef.id); From 05db097928f496d7b59e9d223c1559c4cdf2b958 Mon Sep 17 00:00:00 2001 From: auraticabhi Date: Fri, 15 Mar 2024 22:44:30 +0530 Subject: [PATCH 6/6] comment --- app/[postTitle]/page.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/app/[postTitle]/page.tsx b/app/[postTitle]/page.tsx index c7208ba..dace215 100644 --- a/app/[postTitle]/page.tsx +++ b/app/[postTitle]/page.tsx @@ -287,6 +287,7 @@ const PostPage = ({ params: { postTitle } }: Props) => { }, [postTitleWithSpaces, postTitle]); const [description, setDescription] = useState(""); + //console.log("Keywords ", queObject.keywords); return (