From 7c06ce81b6d1a2d4bdfea9a63ab99d0c90c0d600 Mon Sep 17 00:00:00 2001 From: g-pooja-03 Date: Wed, 27 Nov 2024 00:14:40 -0500 Subject: [PATCH] community questions socket, sort by newest --- .../community/communityQuestions/index.tsx | 32 +++++++++++++------ .../community/membersSideBar/index.tsx | 2 +- client/src/types.ts | 8 +++++ server/controller/community.ts | 27 ++++++++++++---- server/controller/user.ts | 2 ++ server/models/application.ts | 2 +- server/types.ts | 7 +++- 7 files changed, 62 insertions(+), 18 deletions(-) diff --git a/client/src/components/main/homePage/community/communityQuestions/index.tsx b/client/src/components/main/homePage/community/communityQuestions/index.tsx index 1a8169f..fcd099f 100644 --- a/client/src/components/main/homePage/community/communityQuestions/index.tsx +++ b/client/src/components/main/homePage/community/communityQuestions/index.tsx @@ -1,3 +1,4 @@ +/* eslint-disable no-console */ import React, { useEffect, useState } from 'react'; import { getCommunityQuestions } from '../../../../../services/communityService'; import { Question } from '../../../../../types'; @@ -11,26 +12,39 @@ import useUserContext from '../../../../../hooks/useUserContext'; const CommunityQuestions = () => { const [questions, setQuestions] = useState([]); const [loading, setLoading] = useState(true); - const { user } = useUserContext(); + const { user, socket } = useUserContext(); useEffect(() => { const fetchQuestions = async () => { try { - const fetchedQuestions = await getCommunityQuestions(user.community); - // console.log(fetchedQuestions); - setQuestions(fetchedQuestions); + if (user.community) { + const fetchedQuestions = await getCommunityQuestions(user.community); + setQuestions(fetchedQuestions); + } } catch (err) { - // eslint-disable-next-line no-console console.error('Failed to fetch questions'); } finally { setLoading(false); } }; - if (user.community) { - fetchQuestions(); - } - }, [user.community]); + fetchQuestions(); + + const handleCommunityQuestionUpdate = (community: { name: string; questions: Question[] }) => { + if (community.name === user.community) { + console.log(community.name); + console.log(community.questions); + console.log(`Received question update for community: ${community.name}`); + setQuestions(community.questions); + } + }; + + socket.on('communityQuestionUpdate', handleCommunityQuestionUpdate); + + return () => { + socket.off('communityQuestionUpdate', handleCommunityQuestionUpdate); + }; + }, [user.community, socket]); if (loading) return

Loading questions...

; diff --git a/client/src/components/main/homePage/community/membersSideBar/index.tsx b/client/src/components/main/homePage/community/membersSideBar/index.tsx index 3c66d1f..5a66a72 100644 --- a/client/src/components/main/homePage/community/membersSideBar/index.tsx +++ b/client/src/components/main/homePage/community/membersSideBar/index.tsx @@ -35,7 +35,7 @@ const MembersSidebar = () => { socket.on('communityUpdate', handleCommunityUpdate); return () => { - socket.off('communityUpdate'); + socket.off('communityUpdate', handleCommunityUpdate); }; }, [user.community, socket]); diff --git a/client/src/types.ts b/client/src/types.ts index 7c5f8a6..1c91a1c 100644 --- a/client/src/types.ts +++ b/client/src/types.ts @@ -150,6 +150,13 @@ export interface CommunityUpdatePayload { questions: Question[]; } +export interface CommunityQuestionUpdatePayload { + name: string; + tags: string[]; + users: string[]; + questions: Question[]; +} + export interface CommunityData { name: string; tags: string[]; @@ -175,6 +182,7 @@ export interface ServerToClientEvents { voteUpdate: (vote: VoteUpdatePayload) => void; commentUpdate: (update: CommentUpdatePayload) => void; communityUpdate: (update: CommunityUpdatePayload) => void; + communityQuestionUpdate: (update: CommunityQuestionUpdatePayload) => void; editQuestionUpdate: (update: EditQuestionPayload) => void; } diff --git a/server/controller/community.ts b/server/controller/community.ts index ed6013f..2958fa0 100644 --- a/server/controller/community.ts +++ b/server/controller/community.ts @@ -1,9 +1,10 @@ /* eslint-disable no-console */ import express, { Request, Response, Router } from 'express'; import CommunityModel from '../models/communities'; -import { Community, FakeSOSocket } from '../types'; +import { Community, FakeSOSocket, Question } from '../types'; import TagModel from '../models/tags'; import QuestionModel from '../models/questions'; +import { sortQuestionsByNewest } from '../models/application'; const communityController = (socket: FakeSOSocket) => { const router: Router = express.Router(); @@ -52,7 +53,7 @@ const communityController = (socket: FakeSOSocket) => { return; } - res.json(communityData.questions); + res.json(sortQuestionsByNewest(communityData.questions as Question[])); } catch (error) { res.status(500).json({ error: 'Error retrieving questions for the community' }); } @@ -77,7 +78,6 @@ const communityController = (socket: FakeSOSocket) => { socket.emit('communityUpdate', { name: comm.name, users: comm.users.filter(user => user !== username), - tags: comm.tags, }); }); res.status(200).json({ message: 'User removed from all communities successfully' }); @@ -91,7 +91,6 @@ const communityController = (socket: FakeSOSocket) => { socket.emit('communityUpdate', { name: comm.name, users: comm.users.filter(user => user !== username), - tags: comm.tags, }); }); @@ -107,7 +106,6 @@ const communityController = (socket: FakeSOSocket) => { } socket.emit('communityUpdate', { name: newCommunity.name, - tags: newCommunity.tags, users: newCommunity.users, }); console.log(`Added user: ${username} to new community: ${newCommunity}`); @@ -162,10 +160,27 @@ const communityController = (socket: FakeSOSocket) => { const updatedCommunity = await CommunityModel.findOneAndUpdate( { name: communityName }, - { $addToSet: { questions: questionId } }, + { $addToSet: { questions: existingQuestion as Question } }, { new: true, runValidators: true }, ); + const populatedCommunity = await CommunityModel.findOne({ name: communityName }).populate({ + path: 'questions', + populate: [ + { path: 'tags', model: 'Tag' }, + { path: 'answers', model: 'Answer' }, + ], + }); + + // console.log(populatedCommunity); + if (populatedCommunity) { + console.log(populatedCommunity.questions); + socket.emit('communityQuestionUpdate', { + name: communityName, + questions: populatedCommunity.questions as Question[], + }); + } + if (!updatedCommunity) { res.status(404).json({ error: 'Community not found.' }); return; diff --git a/server/controller/user.ts b/server/controller/user.ts index 7c8103f..cbbd722 100644 --- a/server/controller/user.ts +++ b/server/controller/user.ts @@ -88,12 +88,14 @@ const userController = (socket: FakeSOSocket) => { if (!username) { res.status(400).json({ error: 'Username is required' }); + return; } const user = await UserModel.findOne({ username }); if (!user) { res.status(404).json({ error: 'User not found' }); + return; } res.status(200).json(user); diff --git a/server/models/application.ts b/server/models/application.ts index 45b2c0c..aeedcda 100644 --- a/server/models/application.ts +++ b/server/models/application.ts @@ -81,7 +81,7 @@ const checkKeywordInQuestion = (q: Question, keywordlist: string[]): boolean => * * @returns {Question[]} - The sorted list of questions */ -const sortQuestionsByNewest = (qlist: Question[]): Question[] => +export const sortQuestionsByNewest = (qlist: Question[]): Question[] => qlist.sort((a, b) => { if (a.askDateTime > b.askDateTime) { return -1; diff --git a/server/types.ts b/server/types.ts index 00a995e..d231618 100644 --- a/server/types.ts +++ b/server/types.ts @@ -224,10 +224,14 @@ export interface AnswerUpdatePayload { export interface CommunityUpdatePayload { name: string; - tags: string[]; users: string[]; } +export interface CommunityQuestionUpdatePayload { + name: string; + questions: Question[]; +} + // ADD COMMENT export interface communityRequest extends Request { body: { @@ -261,5 +265,6 @@ export interface ServerToClientEvents { voteUpdate: (vote: VoteUpdatePayload) => void; commentUpdate: (comment: CommentUpdatePayload) => void; communityUpdate: (community: CommunityUpdatePayload) => void; + communityQuestionUpdate: (community: CommunityQuestionUpdatePayload) => void; editQuestionUpdate: (update: EditQuestionPayload) => void; }