diff --git a/alimento-nextjs/actions/forum/Answer.tsx b/alimento-nextjs/actions/forum/Answer.tsx new file mode 100644 index 0000000..608402d --- /dev/null +++ b/alimento-nextjs/actions/forum/Answer.tsx @@ -0,0 +1,79 @@ +"use server" + +import prismadb from "@/lib/prismadb"; +import { Answer } from "@prisma/client"; +import { UUID } from "crypto"; + +type CreateAnswerResponse = { + success: boolean; + data?: Answer + error?: string; +}; + +export async function createAnswer( + questionId: string, + content: string +): Promise { + try { + + const newAnswer = await prismadb.answer.create({ + data: { + content, + questionId, + }, + }); + + await prismadb.question.update({ + where: { id: questionId }, + data: { answered: true }, + }); + + return { + success: true, + data: newAnswer + }; + } catch (error) { + console.error("Error creating answer:", error); + return { + success: false, + error: "Failed to create answer", + }; + } +} + + + + +type DeleteAnswerResponse = { + success: boolean; + data?: Answer; + error?: string; +}; + +export async function deleteSpecificAnswer(answerId: string): Promise { + try { + const deletedAnswer = await prismadb.answer.delete({ + where: { + id: answerId, + }, + }); + + if (!deletedAnswer) { + return { + success: false, + error: "Error in deleting the answer", + }; + } + + return { + success: true, + data: deletedAnswer, + }; + } catch (error) { + console.error("Error deleting specific answer:", error); + return { + success: false, + error: "Failed to delete specific answer", + }; + } +} \ No newline at end of file diff --git a/alimento-nextjs/actions/forum/Question.tsx b/alimento-nextjs/actions/forum/Question.tsx new file mode 100644 index 0000000..e3aeb90 --- /dev/null +++ b/alimento-nextjs/actions/forum/Question.tsx @@ -0,0 +1,141 @@ +'use server'; + +import prismadb from '@/lib/prismadb'; +import { Answer, Question } from '@prisma/client'; +import { UUID } from 'crypto'; + +interface QuestionWithAnswer extends Question{ + answers?:Answer[] +} + +type QuestionResponse = { + success: boolean; + data?: Question; + error?: string; +}; + +type getQuestionsResponse = { + success: boolean; + data?: QuestionWithAnswer[]; + error?: string; +}; + + +export async function createQuestion( + content: string +): Promise { + try { + const newQuestion = await prismadb.question.create({ + data: { + content, + }, + }); + + return { + success: true, + data: newQuestion, + }; + } catch (error) { + console.error('Error creating question:', error); + return { + success: false, + error: 'Failed to create question', + }; + } +} + +export async function getUnansweredQuestions( +): Promise { + try { + const unAnsweredQuestions = await prismadb.question.findMany({ + where: { + answered: false, + }, + }); + + if (!unAnsweredQuestions.length) { + return { + success: false, + error: 'No unanswered questions!', + }; + } + + return { + success: true, + data: unAnsweredQuestions, + }; + } catch (error) { + console.error('Error fetching unanswered questions', error); + return { + success: false, + error: 'Failed to fetch unanswered questions', + }; + } +} + + +export async function getAnsweredQuestions( +): Promise { + try { + const AnsweredQuestions = await prismadb.question.findMany({ + where: { + answered: true, + }, + include:{ + answers:true + } + }); + + if (!AnsweredQuestions.length) { + return { + success: false, + error: 'No unanswered questions!', + }; + } + + return { + success: true, + data: AnsweredQuestions, + }; + } catch (error) { + console.error('Error fetching answered questions', error); + return { + success: false, + error: 'Failed to fetch answered questions', + }; + } +} + +// This will be only accessible by the admin + +// deletes a question and all its related answers if present + +export async function deleteQuestion( + questionId: string, +): Promise { + try { + const deletedQuestion = await prismadb.question.delete({ + where: { + id:questionId + }, + }); + + if (!deletedQuestion) { + return { + success: false, + error: 'error in deleting the question', + }; + } + + return { + success: true, + data: deletedQuestion, + }; + } catch (error) { + console.error('Error deleting specific question', error); + return { + success: false, + error: 'Failed to delete specific question', + }; + } +} \ No newline at end of file diff --git a/alimento-nextjs/app/(PublicRoutes)/contact-us/page.tsx b/alimento-nextjs/app/(PublicRoutes)/(footerPages)/contact-us/page.tsx similarity index 100% rename from alimento-nextjs/app/(PublicRoutes)/contact-us/page.tsx rename to alimento-nextjs/app/(PublicRoutes)/(footerPages)/contact-us/page.tsx diff --git a/alimento-nextjs/app/(PublicRoutes)/contributors/page.tsx b/alimento-nextjs/app/(PublicRoutes)/(footerPages)/contributors/page.tsx similarity index 100% rename from alimento-nextjs/app/(PublicRoutes)/contributors/page.tsx rename to alimento-nextjs/app/(PublicRoutes)/(footerPages)/contributors/page.tsx diff --git a/alimento-nextjs/app/(PublicRoutes)/(footerPages)/forum/components/NewQuestionForm.tsx b/alimento-nextjs/app/(PublicRoutes)/(footerPages)/forum/components/NewQuestionForm.tsx new file mode 100644 index 0000000..27f4778 --- /dev/null +++ b/alimento-nextjs/app/(PublicRoutes)/(footerPages)/forum/components/NewQuestionForm.tsx @@ -0,0 +1,80 @@ +'use client'; + +import { createQuestion } from '@/actions/forum/Question'; +import { Button } from '@/components/ui/button'; +import { Label } from '@/components/ui/label'; +import { Spinner } from '@/components/ui/spinner'; +import { Textarea } from '@/components/ui/textarea'; +import { useSession } from 'next-auth/react'; +import { SyntheticEvent, useEffect, useState } from 'react'; + +const NewQuestionForm = () => { + const [questionContent, setQuestionContent] = useState(''); + const [submissionError, setSubmissionError] = useState(''); + + const [isMounted, setIsMounted] = useState(false); + const [isAuthenticated, setIsAuthenticated] = useState(false); + + const session = useSession(); + + useEffect(() => { + setIsMounted(true); + if (!(session.status === 'loading')) { + if (session.status === 'authenticated') { + setIsAuthenticated(true); + // console.log("hereeeeeeeeee"+isAuthenticated+session.status) + } + } + }, [session.status]); + + if (!isMounted) { + return ; + } + + const handleSubmit = async (e: SyntheticEvent) => { + // e.preventDefault(); + try { + const response = await createQuestion(questionContent); + if (!response.success && response.error) { + setSubmissionError(response.error); + } else { + setQuestionContent(''); + setSubmissionError(''); + } + } catch (error) { + setSubmissionError( + 'An error occurred while submitting your question. Please try again.' + ); + } + }; + + return ( + <> + {isAuthenticated ? ( +
+ +