diff --git a/app/[postTitle]/page.tsx b/app/[postTitle]/page.tsx index 81e476b..dace215 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 { @@ -54,6 +55,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 +90,7 @@ type QuestionType = { shares: number; questionImageURL: string; createdAt: string; + keywords: string; anonymity: boolean; // Add any other fields as necessary }; @@ -127,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 { @@ -266,6 +287,7 @@ const PostPage = ({ params: { postTitle } }: Props) => { }, [postTitleWithSpaces, postTitle]); const [description, setDescription] = useState(""); + //console.log("Keywords ", queObject.keywords); return ( @@ -309,7 +331,14 @@ const PostPage = ({ params: { postTitle } }: Props) => { )} /> - + {(progress||0)>0&&{`${Math.ceil((progress||0))} % Uploaded`}} +
+ { + previewImg&&
+ previewImage +
+ } +
{/* anonymity toggle */} { +
+
+ +
+ + + + HashTags + + + + +
+ {queObject.keywords?queObject.keywords:"No tags Available"} +
+
+
+
+
+
); }; diff --git a/app/page.tsx b/app/page.tsx index f9bbb33..d677379 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"; @@ -79,10 +79,27 @@ export default function Home() { const [imageUpload , setImageUpload] = useState(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 { @@ -159,6 +176,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 +191,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 and hashtags on 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) { @@ -236,7 +284,6 @@ export default function Home() { else { - return ( <> @@ -353,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 { @@ -187,11 +204,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 and hashtags on 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); } @@ -379,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 +
+ } +