Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- page.tsx
- /* eslint-disable @next/next/no-sync-scripts */
- "use client";
- import { useEffect, useState } from "react";
- import { useRouter } from "next/navigation";
- import { SpinnerLoading } from "./../../../../components/spinner";
- import {
- getForumThread,
- deleteForumThread,
- getAllForumThreadComments,
- getUser,
- likeForumPost,
- dislikeForumPost,
- getForumThreadStats,
- getForumPostLikeStatus,
- pinForumThread,
- } from "./../../../../server/utils/auth";
- import {
- Input,
- Button,
- BreadcrumbItem,
- Breadcrumbs,
- Divider,
- Pagination,
- Dropdown,
- DropdownTrigger,
- DropdownMenu,
- DropdownItem,
- } from "@nextui-org/react";
- import { ScrollArea } from "@/components/ui/scroll-area";
- import parse from "html-react-parser";
- import { ForumCommentCreator } from "./../../../../components/forumCommentCreator";
- import { formatDistanceToNow } from "date-fns";
- import { useCommentsStore } from "./../../../../server/utils/store";
- import { useToast } from "@/components/ui/use-toast";
- import "./../../../globals.css";
- const Category = ({ params: { forum } }: { params: { forum: string } }) => {
- const router = useRouter();
- const [loading, setLoading] = useState(true);
- const [thread, setThread] = useState(null);
- const [threadStats, setThreadStats] = useState(null);
- const [liked, setLiked] = useState(false);
- const [disliked, setDisliked] = useState(false);
- const [currentPage, setCurrentPage] = useState(1);
- const [totalPages, setTotalPages] = useState(1);
- const [pinned, setPinned] = useState(false);
- const [selected, setSelected] = useState("recent");
- const [user, setUser] = useState(null);
- const [animationDelays, setAnimationDelays] = useState<{
- [key: number]: string;
- }>({});
- const { toast } = useToast();
- const comments = useCommentsStore((state: any) => state.comments);
- useEffect(() => {
- const fetchInfo = async () => {
- try {
- const thread = await getForumThread(forum);
- const pinned = thread.data.pinned;
- const comments = await getAllForumThreadComments(forum);
- const threadStats = await getForumThreadStats(forum);
- const totalPages = Math.ceil(comments.data.length / 5);
- setTotalPages(totalPages);
- setPinned(pinned);
- const likeStatus = await getForumPostLikeStatus({
- userId: "velvet",
- thread: forum,
- });
- if (likeStatus.status == "like") {
- setLiked(true);
- setDisliked(false);
- } else if (likeStatus.status == "dislike") {
- setLiked(false);
- setDisliked(true);
- } else if (likeStatus.status == "none") {
- setLiked(false);
- setDisliked(false);
- }
- const userData = await getUser();
- setThread(thread);
- setThreadStats(threadStats);
- useCommentsStore.setState({ comments: comments });
- setUser(userData);
- } finally {
- setLoading(false);
- }
- };
- fetchInfo();
- }, [forum]);
- --------------------------------------------------------------------------
- Some of auth.ts
- export const createForumThread = async (data: any) => {
- try {
- const response = await fetch(
- "http://localhost:4000/api/website/create-forum-thread",
- {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(data),
- }
- );
- if (!response.ok) {
- console.error("Failed to create forum thread:", response.statusText);
- return null;
- }
- const threadData = await response.json();
- return threadData.data;
- } catch (error) {
- console.error("Failed to create forum thread:", error);
- return null;
- }
- };
- export const pinForumThread = async (data: any) => {
- try {
- const response = await fetch(
- "http://localhost:4000/api/website/pin-forum-thread",
- {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(data),
- }
- );
- if (!response.ok) {
- console.error("Failed to create forum thread:", response.statusText);
- return null;
- }
- const threadData = await response.json();
- return { data: threadData.data };
- } catch (error) {
- console.error(error);
- return null;
- }
- };
- const createNewCategory = async (data: any) => {
- try {
- const response = await fetch(
- "http://localhost:4000/api/website/create-forum-category",
- {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(data),
- }
- );
- if (!response.ok) {
- return false;
- }
- return true;
- } catch (error) {
- console.error("Failed to set discord value:", error);
- return false;
- }
- };
- export const createNewComment = async (data: any) => {
- try {
- const response = await fetch(
- "http://localhost:4000/api/website/create-forum-comment",
- {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(data),
- }
- );
- if (!response.ok) {
- return false;
- }
- return true;
- } catch (error) {
- console.error("Failed to set discord value:", error);
- return false;
- }
- };
- export const getAllForumCategories = async () => {
- console.log("dsaddsaasdsad");
- const response = await fetch(
- "http://localhost:4000/api/website/get-all-forum-categories",
- {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- }
- );
- if (!response.ok) {
- const errorText = await response.text();
- console.log(errorText);
- return null;
- }
- const data = await response.json();
- return data;
- };
- ------------------------------------------------------------
- forumRoutes.js
- const express = require("express");
- const db = require("./../../utils/db");
- const database = db.connect();
- const {
- Category,
- Comment,
- Thread,
- Like,
- Dislike,
- } = require("./../../schemas/forumsSchema");
- const app = express.Router();
- app.post("/api/website/get-all-forum-categories", async (req, res) => {
- database.then(async (db) => {
- const collection = db.collection("website_forums_categories");
- try {
- const data = await collection.find({}).toArray();
- res.json({ data });
- } catch (error) {
- console.error("Error retrieving data from MongoDB:", error);
- res
- .status(500)
- .json({ error: "An error occurred while retrieving data" });
- }
- });
- });
- app.post("/api/website/get-all-forum-category-threads", async (req, res) => {
- database.then(async (db) => {
- const collection = db.collection("website_forums_threads");
- try {
- const body = req.body;
- const data = await collection.find({ category: body.category }).toArray();
- res.json({ data });
- } catch (error) {
- console.error("Error retrieving data from MongoDB:", error);
- res
- .status(500)
- .json({ error: "An error occurred while retrieving data" });
- }
- });
- });
- app.post("/api/website/get-forum-thread", async (req, res) => {
- database.then(async (db) => {
- const collection = db.collection("website_forums_threads");
- try {
- const { id } = req.body;
- const data = await collection.findOne({ id: id });
- res.json({ data });
- } catch (error) {
- console.error("Error retrieving data from MongoDB:", error);
- res
- .status(500)
- .json({ error: "An error occurred while retrieving data" });
- }
- });
- });
- app.post("/api/website/delete-forum-thread", async (req, res) => {
- database.then(async (db) => {
- const collection = db.collection("website_forums_threads");
- try {
- const data = req.body;
- const doc = await collection.findOneAndDelete({ id: data.id });
- await Category.findOneAndUpdate(
- { id: data.thread.data.category },
- {
- $inc: { threads: -1 },
- $set: { latestContent: null },
- }
- );
- res.json({ doc });
- } catch (error) {
- console.error("Error retrieving data from MongoDB:", error);
- res
- .status(500)
- .json({ error: "An error occurred while retrieving data" });
- }
- });
- });
- module.exports = app;
Advertisement
Add Comment
Please, Sign In to add comment