Guest User

Untitled

a guest
May 22nd, 2024
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.39 KB | None | 0 0
  1. page.tsx
  2.  
  3. /* eslint-disable @next/next/no-sync-scripts */
  4. "use client";
  5. import { useEffect, useState } from "react";
  6. import { useRouter } from "next/navigation";
  7. import { SpinnerLoading } from "./../../../../components/spinner";
  8.  
  9. import {
  10. getForumThread,
  11. deleteForumThread,
  12. getAllForumThreadComments,
  13. getUser,
  14. likeForumPost,
  15. dislikeForumPost,
  16. getForumThreadStats,
  17. getForumPostLikeStatus,
  18. pinForumThread,
  19. } from "./../../../../server/utils/auth";
  20. import {
  21. Input,
  22. Button,
  23. BreadcrumbItem,
  24. Breadcrumbs,
  25. Divider,
  26. Pagination,
  27. Dropdown,
  28. DropdownTrigger,
  29. DropdownMenu,
  30. DropdownItem,
  31. } from "@nextui-org/react";
  32. import { ScrollArea } from "@/components/ui/scroll-area";
  33.  
  34. import parse from "html-react-parser";
  35. import { ForumCommentCreator } from "./../../../../components/forumCommentCreator";
  36. import { formatDistanceToNow } from "date-fns";
  37. import { useCommentsStore } from "./../../../../server/utils/store";
  38. import { useToast } from "@/components/ui/use-toast";
  39. import "./../../../globals.css";
  40.  
  41. const Category = ({ params: { forum } }: { params: { forum: string } }) => {
  42. const router = useRouter();
  43. const [loading, setLoading] = useState(true);
  44. const [thread, setThread] = useState(null);
  45. const [threadStats, setThreadStats] = useState(null);
  46. const [liked, setLiked] = useState(false);
  47. const [disliked, setDisliked] = useState(false);
  48. const [currentPage, setCurrentPage] = useState(1);
  49. const [totalPages, setTotalPages] = useState(1);
  50. const [pinned, setPinned] = useState(false);
  51. const [selected, setSelected] = useState("recent");
  52.  
  53. const [user, setUser] = useState(null);
  54. const [animationDelays, setAnimationDelays] = useState<{
  55. [key: number]: string;
  56. }>({});
  57.  
  58. const { toast } = useToast();
  59.  
  60. const comments = useCommentsStore((state: any) => state.comments);
  61.  
  62. useEffect(() => {
  63. const fetchInfo = async () => {
  64. try {
  65. const thread = await getForumThread(forum);
  66. const pinned = thread.data.pinned;
  67.  
  68. const comments = await getAllForumThreadComments(forum);
  69. const threadStats = await getForumThreadStats(forum);
  70. const totalPages = Math.ceil(comments.data.length / 5);
  71. setTotalPages(totalPages);
  72. setPinned(pinned);
  73. const likeStatus = await getForumPostLikeStatus({
  74. userId: "velvet",
  75. thread: forum,
  76. });
  77. if (likeStatus.status == "like") {
  78. setLiked(true);
  79. setDisliked(false);
  80. } else if (likeStatus.status == "dislike") {
  81. setLiked(false);
  82. setDisliked(true);
  83. } else if (likeStatus.status == "none") {
  84. setLiked(false);
  85. setDisliked(false);
  86. }
  87. const userData = await getUser();
  88. setThread(thread);
  89. setThreadStats(threadStats);
  90.  
  91. useCommentsStore.setState({ comments: comments });
  92. setUser(userData);
  93. } finally {
  94. setLoading(false);
  95. }
  96. };
  97.  
  98. fetchInfo();
  99. }, [forum]);
  100.  
  101. --------------------------------------------------------------------------
  102. Some of auth.ts
  103.  
  104. export const createForumThread = async (data: any) => {
  105. try {
  106. const response = await fetch(
  107. "http://localhost:4000/api/website/create-forum-thread",
  108. {
  109. method: "POST",
  110. headers: {
  111. "Content-Type": "application/json",
  112. },
  113. body: JSON.stringify(data),
  114. }
  115. );
  116.  
  117. if (!response.ok) {
  118. console.error("Failed to create forum thread:", response.statusText);
  119. return null;
  120. }
  121.  
  122. const threadData = await response.json();
  123.  
  124. return threadData.data;
  125. } catch (error) {
  126. console.error("Failed to create forum thread:", error);
  127. return null;
  128. }
  129. };
  130.  
  131. export const pinForumThread = async (data: any) => {
  132. try {
  133. const response = await fetch(
  134. "http://localhost:4000/api/website/pin-forum-thread",
  135. {
  136. method: "POST",
  137. headers: {
  138. "Content-Type": "application/json",
  139. },
  140. body: JSON.stringify(data),
  141. }
  142. );
  143.  
  144. if (!response.ok) {
  145. console.error("Failed to create forum thread:", response.statusText);
  146. return null;
  147. }
  148.  
  149. const threadData = await response.json();
  150.  
  151. return { data: threadData.data };
  152. } catch (error) {
  153. console.error(error);
  154. return null;
  155. }
  156. };
  157. const createNewCategory = async (data: any) => {
  158. try {
  159. const response = await fetch(
  160. "http://localhost:4000/api/website/create-forum-category",
  161. {
  162. method: "POST",
  163. headers: {
  164. "Content-Type": "application/json",
  165. },
  166. body: JSON.stringify(data),
  167. }
  168. );
  169.  
  170. if (!response.ok) {
  171. return false;
  172. }
  173.  
  174. return true;
  175. } catch (error) {
  176. console.error("Failed to set discord value:", error);
  177. return false;
  178. }
  179. };
  180.  
  181. export const createNewComment = async (data: any) => {
  182. try {
  183. const response = await fetch(
  184. "http://localhost:4000/api/website/create-forum-comment",
  185. {
  186. method: "POST",
  187. headers: {
  188. "Content-Type": "application/json",
  189. },
  190. body: JSON.stringify(data),
  191. }
  192. );
  193.  
  194. if (!response.ok) {
  195. return false;
  196. }
  197.  
  198. return true;
  199. } catch (error) {
  200. console.error("Failed to set discord value:", error);
  201. return false;
  202. }
  203. };
  204.  
  205. export const getAllForumCategories = async () => {
  206. console.log("dsaddsaasdsad");
  207. const response = await fetch(
  208. "http://localhost:4000/api/website/get-all-forum-categories",
  209. {
  210. method: "POST",
  211. headers: {
  212. "Content-Type": "application/json",
  213. },
  214. }
  215. );
  216.  
  217. if (!response.ok) {
  218. const errorText = await response.text();
  219. console.log(errorText);
  220. return null;
  221. }
  222.  
  223. const data = await response.json();
  224.  
  225. return data;
  226. };
  227.  
  228. ------------------------------------------------------------
  229. forumRoutes.js
  230.  
  231. const express = require("express");
  232. const db = require("./../../utils/db");
  233. const database = db.connect();
  234.  
  235. const {
  236. Category,
  237. Comment,
  238. Thread,
  239. Like,
  240. Dislike,
  241. } = require("./../../schemas/forumsSchema");
  242.  
  243. const app = express.Router();
  244.  
  245.  
  246. app.post("/api/website/get-all-forum-categories", async (req, res) => {
  247. database.then(async (db) => {
  248. const collection = db.collection("website_forums_categories");
  249. try {
  250. const data = await collection.find({}).toArray();
  251.  
  252. res.json({ data });
  253. } catch (error) {
  254. console.error("Error retrieving data from MongoDB:", error);
  255. res
  256. .status(500)
  257. .json({ error: "An error occurred while retrieving data" });
  258. }
  259. });
  260. });
  261.  
  262. app.post("/api/website/get-all-forum-category-threads", async (req, res) => {
  263. database.then(async (db) => {
  264. const collection = db.collection("website_forums_threads");
  265. try {
  266. const body = req.body;
  267.  
  268. const data = await collection.find({ category: body.category }).toArray();
  269.  
  270. res.json({ data });
  271. } catch (error) {
  272. console.error("Error retrieving data from MongoDB:", error);
  273. res
  274. .status(500)
  275. .json({ error: "An error occurred while retrieving data" });
  276. }
  277. });
  278. });
  279.  
  280. app.post("/api/website/get-forum-thread", async (req, res) => {
  281. database.then(async (db) => {
  282. const collection = db.collection("website_forums_threads");
  283. try {
  284. const { id } = req.body;
  285.  
  286. const data = await collection.findOne({ id: id });
  287.  
  288. res.json({ data });
  289. } catch (error) {
  290. console.error("Error retrieving data from MongoDB:", error);
  291. res
  292. .status(500)
  293. .json({ error: "An error occurred while retrieving data" });
  294. }
  295. });
  296. });
  297.  
  298. app.post("/api/website/delete-forum-thread", async (req, res) => {
  299. database.then(async (db) => {
  300. const collection = db.collection("website_forums_threads");
  301. try {
  302. const data = req.body;
  303. const doc = await collection.findOneAndDelete({ id: data.id });
  304.  
  305. await Category.findOneAndUpdate(
  306. { id: data.thread.data.category },
  307. {
  308. $inc: { threads: -1 },
  309. $set: { latestContent: null },
  310. }
  311. );
  312.  
  313. res.json({ doc });
  314. } catch (error) {
  315. console.error("Error retrieving data from MongoDB:", error);
  316. res
  317. .status(500)
  318. .json({ error: "An error occurred while retrieving data" });
  319. }
  320. });
  321. });
  322.  
  323. module.exports = app;
Advertisement
Add Comment
Please, Sign In to add comment