-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from vishalkondle45/feature/forum
feature: tags in forum
- Loading branch information
Showing
9 changed files
with
171 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use client'; | ||
|
||
import React, { useState } from 'react'; | ||
import { | ||
Group, | ||
rem, | ||
SegmentedControl, | ||
SimpleGrid, | ||
Stack, | ||
Text, | ||
TextInput, | ||
Title, | ||
} from '@mantine/core'; | ||
import { IconSearch } from '@tabler/icons-react'; | ||
import { useRouter } from 'next/navigation'; | ||
import { Tag } from '@/components/Forum'; | ||
import useFetchData from '@/hooks/useFetchData'; | ||
import Skelton from '@/components/Skelton/Skelton'; | ||
|
||
const TagsPage = () => { | ||
const [sort, setSort] = useState('popular'); | ||
const [search, setSearch] = useState(''); | ||
const { data, loading } = useFetchData(`/api/forums/tags?sort=${sort}`); | ||
const router = useRouter(); | ||
|
||
return ( | ||
<> | ||
<Stack> | ||
<Title>Tags</Title> | ||
<Text> | ||
A tag is a keyword or label that categorizes your question with other, similar questions. | ||
Using the right tags makes it easier for others to find and answer your question. | ||
</Text> | ||
<Group justify="space-between"> | ||
<TextInput | ||
leftSection={<IconSearch style={{ width: rem(18), height: rem(18) }} />} | ||
placeholder="Filter by tag name" | ||
w="min-width" | ||
onChange={(e) => setSearch(e.currentTarget.value)} | ||
value={search} | ||
/> | ||
<SegmentedControl | ||
value={sort} | ||
onChange={setSort} | ||
color="indigo" | ||
bg="white" | ||
data={[ | ||
{ label: 'Popular', value: 'popular' }, | ||
{ label: 'Name', value: 'name' }, | ||
{ label: 'New', value: 'new' }, | ||
]} | ||
/> | ||
</Group> | ||
<SimpleGrid cols={{ base: 1, xs: 2, sm: 3, md: 4 }}> | ||
{loading ? ( | ||
<Skelton items={8} height={70} /> | ||
) : ( | ||
data | ||
?.filter((tag) => String(tag._id).includes(search)) | ||
?.map((tag) => ( | ||
<Tag | ||
key={String(tag._id)} | ||
tag={String(tag._id)} | ||
count={tag.total} | ||
todayCount={tag.today} | ||
onClick={() => router.push(`/forum/tagged/${tag._id}`)} | ||
/> | ||
)) | ||
)} | ||
</SimpleGrid> | ||
</Stack> | ||
</> | ||
); | ||
}; | ||
|
||
export default TagsPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import dayjs from 'dayjs'; | ||
import { getServerSession } from 'next-auth'; | ||
import { NextRequest, 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(req: NextRequest) { | ||
const today = dayjs().startOf('day'); | ||
try { | ||
const session: UserDataTypes | null = await getServerSession(authOptions); | ||
if (!session?.user) { | ||
return NextResponse.json({ error: 'You are not authorized' }, { status: 401 }); | ||
} | ||
const sort = req.nextUrl.searchParams.get('sort')?.toString() ?? 'name'; | ||
await startDb(); | ||
const tags = await Forum.aggregate([ | ||
{ $unwind: '$tags' }, | ||
{ | ||
$group: { | ||
_id: '$tags', | ||
total: { $sum: 1 }, | ||
today: { $sum: { $cond: [{ $gte: ['$createdAt', today.toDate()] }, 1, 0] } }, | ||
}, | ||
}, | ||
{ $sort: sort === 'name' ? { _id: 1 } : sort === 'new' ? { createdAt: -1 } : { total: -1 } }, | ||
]); | ||
return NextResponse.json(tags, { status: 200 }); | ||
} catch (error: any) { | ||
return NextResponse.json({ error: error?.message }, { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Badge, Group, Paper, Stack, Text } from '@mantine/core'; | ||
import React from 'react'; | ||
|
||
interface Props { | ||
tag: string; | ||
onClick: () => void; | ||
count: number; | ||
todayCount?: number; | ||
} | ||
|
||
export const Tag = ({ tag, onClick, count, todayCount }: Props) => ( | ||
<Paper p="md" withBorder> | ||
<Stack> | ||
<Badge style={{ cursor: 'pointer' }} onClick={onClick} radius="xs" tt="lowercase"> | ||
{tag} | ||
</Badge> | ||
<Group wrap="nowrap" justify="space-between"> | ||
<Text size="xs" c="dimmed"> | ||
{count} questions | ||
</Text> | ||
<Text size="xs" c="dimmed"> | ||
{todayCount} asked today | ||
</Text> | ||
</Group> | ||
</Stack> | ||
</Paper> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { Skeleton } from '@mantine/core'; | ||
import React from 'react'; | ||
|
||
const Skelton = ({ items = 4 }: { items?: number }) => | ||
[...Array(items)].map((_, i) => <Skeleton key={String(i)} height={100} mt="md" animate />); | ||
const Skelton = ({ items = 4, height = 100 }: { items?: number; height?: number }) => | ||
[...Array(items)].map((_, i) => <Skeleton key={String(i)} height={height} mt="md" animate />); | ||
|
||
export default Skelton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters