diff --git a/app/(private)/forum/my-answers/page.tsx b/app/(private)/forum/my-answers/page.tsx new file mode 100644 index 0000000..327d695 --- /dev/null +++ b/app/(private)/forum/my-answers/page.tsx @@ -0,0 +1,36 @@ +'use client'; + +import { Button, Group, Stack, Title } from '@mantine/core'; +import { useDisclosure, useMediaQuery } from '@mantine/hooks'; +import { ForumItem, AskQuestion } from '@/components/Forum'; +import useFetchData from '@/hooks/useFetchData'; +import { ForumType } from '@/components/Forum/Forum.types'; +import Skelton from '@/components/Skelton/Skelton'; + +const ForumPage = () => { + const { data: forums, loading } = useFetchData('/api/forums/my-answers'); + const [opened, { open, close }] = useDisclosure(false); + const isMobile = useMediaQuery('(max-width: 768px)'); + + return ( + <> + + You Answered + + + + {loading ? ( + + ) : ( + Array.isArray(forums) && + forums?.map((forum: ForumType) => ( + + )) + )} + + + + ); +}; + +export default ForumPage; diff --git a/app/(private)/forum/my-questions/page.tsx b/app/(private)/forum/my-questions/page.tsx new file mode 100644 index 0000000..bbfb933 --- /dev/null +++ b/app/(private)/forum/my-questions/page.tsx @@ -0,0 +1,36 @@ +'use client'; + +import { Button, Group, Stack, Title } from '@mantine/core'; +import { useDisclosure, useMediaQuery } from '@mantine/hooks'; +import { ForumItem, AskQuestion } from '@/components/Forum'; +import useFetchData from '@/hooks/useFetchData'; +import { ForumType } from '@/components/Forum/Forum.types'; +import Skelton from '@/components/Skelton/Skelton'; + +const ForumPage = () => { + const { data: forums, loading } = useFetchData('/api/forums/my-questions'); + const [opened, { open, close }] = useDisclosure(false); + const isMobile = useMediaQuery('(max-width: 768px)'); + + return ( + <> + + You Asked + + + + {loading ? ( + + ) : ( + Array.isArray(forums) && + forums?.map((forum: ForumType) => ( + + )) + )} + + + + ); +}; + +export default ForumPage; diff --git a/app/api/forums/my-answers/route.ts b/app/api/forums/my-answers/route.ts new file mode 100644 index 0000000..6441b08 --- /dev/null +++ b/app/api/forums/my-answers/route.ts @@ -0,0 +1,36 @@ +import { getServerSession } from 'next-auth'; +import { NextResponse } from 'next/server'; +import startDb from '@/lib/db'; +import Forum from '@/models/Forum'; +import { authOptions } from '../../auth/[...nextauth]/authOptions'; +import { UserDataTypes } from '../../auth/[...nextauth]/next-auth.interfaces'; + +export const maxDuration = 60; +export const dynamic = 'force-dynamic'; + +export async function GET() { + try { + const session: UserDataTypes | null = await getServerSession(authOptions); + if (!session?.user) { + return NextResponse.json({ error: 'You are not authorized' }, { status: 401 }); + } + await startDb(); + + const forums = await Forum.find({ + question: { $exists: false }, + description: { $exists: true }, + user: session?.user._id, + }); + + const forumsIds = forums.map((forum) => forum._id); + + const answers = await Forum.find({ answers: { $in: forumsIds } }).populate({ + path: 'user', + select: 'name', + }); + + return NextResponse.json(answers, { status: 200 }); + } catch (error: any) { + return NextResponse.json({ error: error?.message }, { status: 500 }); + } +} diff --git a/app/api/forums/my-questions/route.ts b/app/api/forums/my-questions/route.ts new file mode 100644 index 0000000..6802083 --- /dev/null +++ b/app/api/forums/my-questions/route.ts @@ -0,0 +1,31 @@ +import { getServerSession } from 'next-auth'; +import { NextResponse } from 'next/server'; +import startDb from '@/lib/db'; +import Forum from '@/models/Forum'; +import { authOptions } from '../../auth/[...nextauth]/authOptions'; +import { UserDataTypes } from '../../auth/[...nextauth]/next-auth.interfaces'; + +export const maxDuration = 60; +export const dynamic = 'force-dynamic'; + +export async function GET() { + try { + const session: UserDataTypes | null = await getServerSession(authOptions); + if (!session?.user) { + return NextResponse.json({ error: 'You are not authorized' }, { status: 401 }); + } + await startDb(); + + const forums = await Forum.find({ + question: { $exists: true }, + description: { $exists: true }, + user: session?.user._id, + }) + .populate({ path: 'user', select: 'name' }) + .sort('-updatedAt'); + + return NextResponse.json(forums, { status: 200 }); + } catch (error: any) { + return NextResponse.json({ error: error?.message }, { status: 500 }); + } +} diff --git a/app/api/forums/route.ts b/app/api/forums/route.ts index c544b7c..5bad312 100644 --- a/app/api/forums/route.ts +++ b/app/api/forums/route.ts @@ -87,8 +87,9 @@ export async function DELETE(req: NextRequest) { return NextResponse.json({ error: 'You are not authorized' }, { status: 401 }); } await startDb(); + const fourm = await Forum.findById(req.nextUrl.searchParams.get('_id')); await Forum.findByIdAndDelete(req.nextUrl.searchParams.get('_id')); - await Forum.deleteMany({ parent: req.nextUrl.searchParams.get('_id') }); + await Forum.deleteMany({ _id: { $in: fourm?.answers } }); return NextResponse.json(null, { status: 200 }); } catch (error: any) { return NextResponse.json({ error: error?.message }, { status: 500 }); diff --git a/lib/constants.tsx b/lib/constants.tsx index a36be47..afc8616 100644 --- a/lib/constants.tsx +++ b/lib/constants.tsx @@ -1,7 +1,7 @@ import { HarmBlockThreshold, HarmCategory } from '@google/generative-ai'; import { IconArchive, - IconBookmarkFilled, + IconBookmark, IconCalendar, IconCalendarDown, IconCalendarUp, @@ -11,6 +11,7 @@ import { IconCode, IconLock, IconMessageQuestion, + IconMessageReply, IconMessages, IconNote, IconNumber0Small, @@ -40,9 +41,10 @@ import { IconNumber9Small, IconPlus, IconPrompt, + IconQuestionMark, IconRobot, IconStar, - IconTagsFilled, + IconTags, IconTrash, IconWallet, } from '@tabler/icons-react'; @@ -139,8 +141,10 @@ export const APPS: AppType[] = [ color: 'indigo', sidebar: [ { label: 'Home', path: '/forum', icon: }, - { label: 'Tags', path: '/forum/tags', icon: }, - { label: 'Saved', path: '/forum/saved', icon: }, + { label: 'Tags', path: '/forum/tags', icon: }, + { label: 'Saved', path: '/forum/saved', icon: }, + { label: 'You Asked', path: '/forum/my-questions', icon: }, + { label: 'You Answered', path: '/forum/my-answers', icon: }, ], }, { label: 'Passwords', path: '/passwords', icon: , color: 'red', sidebar: [] },