From a692a1320ad815a62ff00882cc1763e555882bf2 Mon Sep 17 00:00:00 2001 From: g-pooja-03 Date: Wed, 27 Nov 2024 17:48:17 -0400 Subject: [PATCH] all client comments (review) --- client/src/hooks/useChat.ts | 35 ++++++++++++++++++++-- client/src/hooks/useCommunityNames.ts | 3 ++ client/src/hooks/useCreateUser.ts | 22 ++++++++++++++ client/src/hooks/useQuestionPage.ts | 1 + client/src/hooks/useRelevantCommunities.ts | 5 ++++ client/src/hooks/useTagNames.ts | 6 ++-- client/src/hooks/useTagPage.ts | 3 ++ client/src/services/communityService.ts | 23 +++++++++++++- client/src/services/questionService.ts | 13 +++++++- client/src/services/tagService.ts | 4 +-- client/src/services/userService.ts | 26 ++++++++++++++++ 11 files changed, 133 insertions(+), 8 deletions(-) diff --git a/client/src/hooks/useChat.ts b/client/src/hooks/useChat.ts index 8b78b5b..6c80b9b 100644 --- a/client/src/hooks/useChat.ts +++ b/client/src/hooks/useChat.ts @@ -48,18 +48,29 @@ const useChat = () => { .filter(u => u.toLowerCase().includes(searchTerm.toLowerCase())) .filter(curr => curr.toLowerCase() !== user.username); + /** + * Function to handle changes to the search input. + * @param e - The event object. + */ const handleSearchChange = (e: React.ChangeEvent) => { setSend(e.target.value); setSearchTerm(e.target.value); setDropdownOpen(true); // Open the dropdown when typing }; + /** + * Function to handle the selection of a user from the dropdown. + * @param username - The username of the selected user. + */ const handleUserClick = (username: string) => { setSend(username); setSearchTerm(username); setDropdownOpen(false); // Close the dropdown after selection }; + /** + * Function to fetch the list of all users from the database. + */ const fetchUsers = async () => { try { const result = await getListOfAllUsers(); @@ -71,7 +82,7 @@ const useChat = () => { }; useEffect(() => { - fetchUsers(); // Fetch users on mount + fetchUsers(); }, []); useEffect(() => { @@ -81,10 +92,19 @@ const useChat = () => { } }, [community, pathname, setSend, setSearchTerm]); + /** + * Function to handle changes to the current message input. + * @param e - The event object. + */ const handleInputChange = (e: React.ChangeEvent) => { setCurrentMessage(e.target.value); }; + /** + * Function to save the current message to the user's account. + * @param message - The message to save. + * @param sendTo - The username of the user to send the message to. + */ const saveMessagesToUserAccount = async (message: string, sendTo: string) => { try { if ( @@ -108,10 +128,12 @@ const useChat = () => { useEffect(() => { if (!user?.username) return; - // Query to retrieve messages where the current user is either the sender (username) or receiver (sendTo) const q = query(collection(db, 'messages'), orderBy('timestamp', 'asc')); if (pathname.includes('community') && community) { + /** + * Function to fetch messages between the current user and the selected user. + */ const unsubscribe = onSnapshot(q, querySnapshot => { const loadedMessages = querySnapshot.docs .map(doc => ({ @@ -127,6 +149,9 @@ const useChat = () => { return () => unsubscribe(); } + /** + * Function to fetch messages between the current user and the selected user. + */ const unsubscribe = onSnapshot(q, querySnapshot => { const loadedMessages = querySnapshot.docs .map(doc => ({ @@ -146,10 +171,16 @@ const useChat = () => { return () => unsubscribe(); }, [user, send, pathname, community]); + /** + * Function to scroll up in the chat window. + */ const scrollUp = () => { messageContainer?.scrollBy(0, -100); }; + /** + * Function to scroll down in the chat window. + */ const scrollDown = () => { messageContainer?.scrollBy(0, 100); }; diff --git a/client/src/hooks/useCommunityNames.ts b/client/src/hooks/useCommunityNames.ts index 8166895..136a3bf 100644 --- a/client/src/hooks/useCommunityNames.ts +++ b/client/src/hooks/useCommunityNames.ts @@ -14,6 +14,9 @@ const useCommunityNames = () => { const [error, setError] = useState(''); useEffect(() => { + /** + * Function to fetch community names from the server and update the community list. + */ const fetchCommunityNames = async () => { setLoading(true); setError(''); diff --git a/client/src/hooks/useCreateUser.ts b/client/src/hooks/useCreateUser.ts index 4044e44..4fe4957 100644 --- a/client/src/hooks/useCreateUser.ts +++ b/client/src/hooks/useCreateUser.ts @@ -16,6 +16,8 @@ import useLoginContext from './useLoginContext'; * @returns isLoading - The current loading state. * @returns handleEmailChange - Function to handle changes in the email input field. * @returns handlePasswordChange - Function to handle changes in the password input field. + * @returns handleFirstNameChange - Function to handle changes in the first name input field. + * @returns handleLastNameChange - Function to handle changes in the last name input field. * @returns handleSubmit - Function to handle user creation submission */ const useCreateUser = () => { @@ -29,22 +31,42 @@ const useCreateUser = () => { const { setUser } = useLoginContext(); const navigate = useNavigate(); + /** + * Function to handle the email inputchange event. + * @param e - the event object. + */ const handleEmailChange = (e: ChangeEvent) => { setEmail(e.target.value); }; + /** + * Function to handle the password input change event. + * @param e - the event object. + */ const handlePasswordChange = (e: ChangeEvent) => { setPassword(e.target.value); }; + /** + * Function to handle the first name input change event. + * @param e - the event object. + */ const handleFirstNameChange = (e: ChangeEvent) => { setFirstName(e.target.value); }; + /** + * Function to handle the last name input change event. + * @param e - the event object. + */ const handleLastNameChange = (e: ChangeEvent) => { setLastName(e.target.value); }; + /** + * Function to handle the submit input change event. + * @param e - the event object. + */ const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); setIsLoading(true); diff --git a/client/src/hooks/useQuestionPage.ts b/client/src/hooks/useQuestionPage.ts index 9940a06..3a60feb 100644 --- a/client/src/hooks/useQuestionPage.ts +++ b/client/src/hooks/useQuestionPage.ts @@ -12,6 +12,7 @@ import { getUser } from '../services/userService'; * @returns titleText - The current title of the question page * @returns qlist - The list of questions to display * @returns setQuestionOrder - Function to set the sorting order of questions (e.g., newest, oldest). + * @returns userCommunity - The community of the current user. */ const useQuestionPage = () => { const { socket, user } = useUserContext(); diff --git a/client/src/hooks/useRelevantCommunities.ts b/client/src/hooks/useRelevantCommunities.ts index 0e476f8..ff9efca 100644 --- a/client/src/hooks/useRelevantCommunities.ts +++ b/client/src/hooks/useRelevantCommunities.ts @@ -8,6 +8,8 @@ import { getRelevantCommunities } from '../services/communityService'; * * @returns relevantCommunities - The current list of relevant communities. * @returns setRelevantCommunities - Setter to manually update the relevant communities state if needed. + * @returns loading - The current loading state. + * @returns error - The current error message. */ const useRelevantCommunities = (tags: string[]) => { const [relevantCommunities, setRelevantCommunities] = useState([]); @@ -15,6 +17,9 @@ const useRelevantCommunities = (tags: string[]) => { const [error, setError] = useState(''); useEffect(() => { + /** + * Function to fetch relevant communities based on the provided tags. + */ const fetchRelevantCommunities = async () => { setLoading(true); setError(''); diff --git a/client/src/hooks/useTagNames.ts b/client/src/hooks/useTagNames.ts index 523d106..35af201 100644 --- a/client/src/hooks/useTagNames.ts +++ b/client/src/hooks/useTagNames.ts @@ -7,13 +7,15 @@ import { Tag } from '../types'; * * @param t - The tag object to fetch data for * - * @returns tag - The current tag details (name and description). - * @returns setTag - Setter to manually update the tag state if needed. + * @returns tagnames - The current list of tag names. */ const useTagNames = () => { const [tagNames, setTagNames] = useState([]); useEffect(() => { + /** + * Function to fetch tag names from the server and update the tag list. + */ const fetchData = async () => { try { const res = await getTagNames(); diff --git a/client/src/hooks/useTagPage.ts b/client/src/hooks/useTagPage.ts index 7e0f41d..bbe2642 100644 --- a/client/src/hooks/useTagPage.ts +++ b/client/src/hooks/useTagPage.ts @@ -26,6 +26,9 @@ const useTagPage = () => { }; useEffect(() => { + /** + * Function to fetch tag data from the server and update the tag list. + */ const fetchData = async () => { try { const res = await getTagsWithQuestionNumber(); diff --git a/client/src/services/communityService.ts b/client/src/services/communityService.ts index 023d699..d22d47b 100644 --- a/client/src/services/communityService.ts +++ b/client/src/services/communityService.ts @@ -5,7 +5,7 @@ import { Community, Question } from '../types'; const COMMUNITY_API_URL = `${process.env.REACT_APP_SERVER_URL}/community`; /** - * ADD TESTS?????? + * Fetches the names of all communities. * @returns names of communities */ const getCommunityNames = async (): Promise => { @@ -16,6 +16,11 @@ const getCommunityNames = async (): Promise => { return res.data; }; +/** + * Fetches the questions for a specific community. + * @param community - The name of the community for which questions will be fetched. + * @returns A Promise containing the questions for the community. + */ const getCommunityQuestions = async (community: string): Promise => { const res = await api.get(`${COMMUNITY_API_URL}/getCommunityQuestions/${community}`); if (res.status !== 200) { @@ -24,6 +29,11 @@ const getCommunityQuestions = async (community: string): Promise => return res.data; }; +/** + * Fetches the names of relevant communities based on the provided tags. + * @param tags - The tags to search for relevant communities + * @returns A Promise containing the names of relevant communities. + */ const getRelevantCommunities = async (tags: string[]): Promise => { try { const res = await api.get(`${COMMUNITY_API_URL}/getRelevantCommunities`, { @@ -39,6 +49,12 @@ const getRelevantCommunities = async (tags: string[]): Promise => { } }; +/** + * Updates the user in a community. + * @param username - The username of the user to update. + * @param community - The community to update the user in. + * @returns A Promise containing the updated community. + */ const updatedUserInCommunity = async (username: string, community: string): Promise => { const res = await api.patch(`${COMMUNITY_API_URL}/addUserToCommunity`, { username, community }); if (res.status !== 200) { @@ -70,6 +86,11 @@ const updateCommunityQuestions = async ( return res.data; }; +/** + * Fetches the members of a specific community. + * @param community - The name of the community for which members will be fetched. + * @returns A Promise containing the members of the community. + */ const getCommunityMembers = async (community: string): Promise => { const res = await api.get(`${COMMUNITY_API_URL}/getCommunityMembers/${community}`); if (res.status !== 200) { diff --git a/client/src/services/questionService.ts b/client/src/services/questionService.ts index 28bd09a..266c7dd 100644 --- a/client/src/services/questionService.ts +++ b/client/src/services/questionService.ts @@ -87,7 +87,13 @@ const downvoteQuestion = async (qid: string, username: string) => { return res.data; }; -// ADD COMMENT +/** + * Function to edit a question. + * @param qid - The ID of the question to edit. + * @param newText - The new text of the question. + * @param username - The username of the person editing the question. + * @returns The updated question. + */ const editQuestion = async (qid: string, newText: string, username: string) => { const res = await api.patch(`${QUESTION_API_URL}/editQuestion/${qid}/${username}`, { newText }); if (res.status !== 200) { @@ -96,6 +102,11 @@ const editQuestion = async (qid: string, newText: string, username: string) => { return res.data; }; +/** + * Function to remove a question. + * @param qid - The ID of the question to remove. + * @returns The removed question. + */ const removeQuestion = async (qid: string) => { const res = await api.delete(`${QUESTION_API_URL}/removeQuestion/${qid}`); if (res.status !== 200) { diff --git a/client/src/services/tagService.ts b/client/src/services/tagService.ts index 97fdfbb..0f6a3de 100644 --- a/client/src/services/tagService.ts +++ b/client/src/services/tagService.ts @@ -31,8 +31,8 @@ const getTagByName = async (name: string): Promise => { }; /** - * ADD TESTS?????? - * @returns names of tags + * Function to get tag names. + * @returns A Promise containing the names of tags. */ const getTagNames = async (): Promise => { const res = await api.get(`${TAG_API_URL}/getTagNames`); diff --git a/client/src/services/userService.ts b/client/src/services/userService.ts index 23203eb..0981081 100644 --- a/client/src/services/userService.ts +++ b/client/src/services/userService.ts @@ -22,6 +22,12 @@ const addUser = async (user: User): Promise => { } }; +/** + * Updates the tags of a user. + * @param username - The username of the user to update. + * @param tags - The tags to update for the user. + * @returns - The updated user object from the server response. + */ const updateUserTags = async (username: string, tags: string[]): Promise => { const res = await api.put(`${USER_API_URL}/updateTags`, { username, tags }); if (res.status !== 200) { @@ -29,6 +35,13 @@ const updateUserTags = async (username: string, tags: string[]): Promise = } return res.data; }; + +/** + * Updates the community of a user. + * @param username - The username of the user to update. + * @param community - The community to update for the user. + * @returns - The updated user object from the server response. + */ const updateUserCommunity = async (username: string, community: string): Promise => { const res = await api.put(`${USER_API_URL}/updateCommunity`, { username, community }); if (res.status !== 200) { @@ -37,6 +50,11 @@ const updateUserCommunity = async (username: string, community: string): Promise return res.data; }; +/** + * Fetches the user object from the server. + * @param username - The username of the user to fetch. + * @returns - The user object from the server response. + */ const getUser = async (username: string): Promise => { const res = await api.get(`${USER_API_URL}/getUser`, { params: { username } }); if (res.status !== 200) { @@ -45,6 +63,10 @@ const getUser = async (username: string): Promise => { return res.data; }; +/** + * Fetches a list of all users from the server. + * @returns - A list of all users from the server response. + */ const getListOfAllUsers = async (): Promise => { const res = await api.get(`${USER_API_URL}/getListOfAllUsers`); if (Array.isArray(res.data)) { @@ -56,6 +78,10 @@ const getListOfAllUsers = async (): Promise => { return []; }; +/** + * Increases the status of a user. + * @param username - The username of the user to update. + */ const increaseUserStatus = async (username: string): Promise => { const res = await api.put(`${USER_API_URL}/increaseUserStatus`, { username }); if (res.status !== 200) {