Skip to content

Commit

Permalink
all client comments (review)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-pooja-03 committed Nov 27, 2024
1 parent 99f9e12 commit a692a13
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 8 deletions.
35 changes: 33 additions & 2 deletions client/src/hooks/useChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLInputElement>) => {
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();
Expand All @@ -71,7 +82,7 @@ const useChat = () => {
};

useEffect(() => {
fetchUsers(); // Fetch users on mount
fetchUsers();
}, []);

useEffect(() => {
Expand All @@ -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<HTMLInputElement>) => {
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 (
Expand All @@ -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 => ({
Expand All @@ -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 => ({
Expand All @@ -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);
};
Expand Down
3 changes: 3 additions & 0 deletions client/src/hooks/useCommunityNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const useCommunityNames = () => {
const [error, setError] = useState<string>('');

useEffect(() => {
/**
* Function to fetch community names from the server and update the community list.
*/
const fetchCommunityNames = async () => {
setLoading(true);
setError('');
Expand Down
22 changes: 22 additions & 0 deletions client/src/hooks/useCreateUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -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<HTMLInputElement>) => {
setEmail(e.target.value);
};

/**
* Function to handle the password input change event.
* @param e - the event object.
*/
const handlePasswordChange = (e: ChangeEvent<HTMLInputElement>) => {
setPassword(e.target.value);
};

/**
* Function to handle the first name input change event.
* @param e - the event object.
*/
const handleFirstNameChange = (e: ChangeEvent<HTMLInputElement>) => {
setFirstName(e.target.value);
};

/**
* Function to handle the last name input change event.
* @param e - the event object.
*/
const handleLastNameChange = (e: ChangeEvent<HTMLInputElement>) => {
setLastName(e.target.value);
};

/**
* Function to handle the submit input change event.
* @param e - the event object.
*/
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
setIsLoading(true);
Expand Down
1 change: 1 addition & 0 deletions client/src/hooks/useQuestionPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions client/src/hooks/useRelevantCommunities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ 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<string[]>([]);
const [loading, setLoading] = useState<boolean>(true);
const [error, setError] = useState<string>('');

useEffect(() => {
/**
* Function to fetch relevant communities based on the provided tags.
*/
const fetchRelevantCommunities = async () => {
setLoading(true);
setError('');
Expand Down
6 changes: 4 additions & 2 deletions client/src/hooks/useTagNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tag[]>([]);

useEffect(() => {
/**
* Function to fetch tag names from the server and update the tag list.
*/
const fetchData = async () => {
try {
const res = await getTagNames();
Expand Down
3 changes: 3 additions & 0 deletions client/src/hooks/useTagPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
23 changes: 22 additions & 1 deletion client/src/services/communityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Community[]> => {
Expand All @@ -16,6 +16,11 @@ const getCommunityNames = async (): Promise<Community[]> => {
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<Question[]> => {
const res = await api.get(`${COMMUNITY_API_URL}/getCommunityQuestions/${community}`);
if (res.status !== 200) {
Expand All @@ -24,6 +29,11 @@ const getCommunityQuestions = async (community: string): Promise<Question[]> =>
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<string[]> => {
try {
const res = await api.get(`${COMMUNITY_API_URL}/getRelevantCommunities`, {
Expand All @@ -39,6 +49,12 @@ const getRelevantCommunities = async (tags: string[]): Promise<string[]> => {
}
};

/**
* 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<Community> => {
const res = await api.patch(`${COMMUNITY_API_URL}/addUserToCommunity`, { username, community });
if (res.status !== 200) {
Expand Down Expand Up @@ -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<string[]> => {
const res = await api.get(`${COMMUNITY_API_URL}/getCommunityMembers/${community}`);
if (res.status !== 200) {
Expand Down
13 changes: 12 additions & 1 deletion client/src/services/questionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions client/src/services/tagService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const getTagByName = async (name: string): Promise<Tag> => {
};

/**
* ADD TESTS??????
* @returns names of tags
* Function to get tag names.
* @returns A Promise containing the names of tags.
*/
const getTagNames = async (): Promise<Tag[]> => {
const res = await api.get(`${TAG_API_URL}/getTagNames`);
Expand Down
26 changes: 26 additions & 0 deletions client/src/services/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,26 @@ const addUser = async (user: User): Promise<User> => {
}
};

/**
* 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<User> => {
const res = await api.put(`${USER_API_URL}/updateTags`, { username, tags });
if (res.status !== 200) {
throw new Error('Failed to update user tags');
}
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<User> => {
const res = await api.put(`${USER_API_URL}/updateCommunity`, { username, community });
if (res.status !== 200) {
Expand All @@ -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<User> => {
const res = await api.get(`${USER_API_URL}/getUser`, { params: { username } });
if (res.status !== 200) {
Expand All @@ -45,6 +63,10 @@ const getUser = async (username: string): Promise<User> => {
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<User[]> => {
const res = await api.get(`${USER_API_URL}/getListOfAllUsers`);
if (Array.isArray(res.data)) {
Expand All @@ -56,6 +78,10 @@ const getListOfAllUsers = async (): Promise<User[]> => {
return [];
};

/**
* Increases the status of a user.
* @param username - The username of the user to update.
*/
const increaseUserStatus = async (username: string): Promise<void> => {
const res = await api.put(`${USER_API_URL}/increaseUserStatus`, { username });
if (res.status !== 200) {
Expand Down

0 comments on commit a692a13

Please sign in to comment.