From 98bf245f15ba0d88b09a1e35f2b9bcb7ced58938 Mon Sep 17 00:00:00 2001 From: Maruf Bepary Date: Sun, 7 Dec 2025 20:09:13 +0000 Subject: [PATCH 1/4] REFACTOR: Move getCommunityData function to community folder --- app/community/[communityId]/comments/[pid]/page.tsx | 2 +- app/community/[communityId]/page.tsx | 2 +- app/community/[communityId]/submit/page.tsx | 2 +- lib/{communities.ts => community/getCommunityData.ts} | 0 4 files changed, 3 insertions(+), 3 deletions(-) rename lib/{communities.ts => community/getCommunityData.ts} (100%) diff --git a/app/community/[communityId]/comments/[pid]/page.tsx b/app/community/[communityId]/comments/[pid]/page.tsx index 142b9c7..ccf70fe 100644 --- a/app/community/[communityId]/comments/[pid]/page.tsx +++ b/app/community/[communityId]/comments/[pid]/page.tsx @@ -1,4 +1,4 @@ -import { getCommunityData } from "@/lib/communities"; +import { getCommunityData } from "@/lib/community/getCommunityData"; import { getPost } from "@/lib/posts"; import PostClientPage from "./PostClientPage"; import { notFound } from "next/navigation"; diff --git a/app/community/[communityId]/page.tsx b/app/community/[communityId]/page.tsx index d764ddb..a45289a 100644 --- a/app/community/[communityId]/page.tsx +++ b/app/community/[communityId]/page.tsx @@ -1,4 +1,4 @@ -import { getCommunityData } from "@/lib/communities"; +import { getCommunityData } from "@/lib/community/getCommunityData"; import CommunityClientPage from "./CommunityClientPage"; import { notFound } from "next/navigation"; diff --git a/app/community/[communityId]/submit/page.tsx b/app/community/[communityId]/submit/page.tsx index db76cea..e4be41d 100644 --- a/app/community/[communityId]/submit/page.tsx +++ b/app/community/[communityId]/submit/page.tsx @@ -1,4 +1,4 @@ -import { getCommunityData } from "@/lib/communities"; +import { getCommunityData } from "@/lib/community/getCommunityData"; import SubmitPostClientPage from "./SubmitPostClientPage"; import { notFound } from "next/navigation"; diff --git a/lib/communities.ts b/lib/community/getCommunityData.ts similarity index 100% rename from lib/communities.ts rename to lib/community/getCommunityData.ts From 7c6bedeac27ae0b0075b3141ebc948a25754c854 Mon Sep 17 00:00:00 2001 From: Maruf Bepary Date: Sun, 7 Dec 2025 20:10:11 +0000 Subject: [PATCH 2/4] REFACTOR: Move fetchCommunityMembers function to community folder --- components/Modal/CommunityMembers/CommunityMembersModal.tsx | 2 +- hooks/community/useCommunityMembers.ts | 4 ++-- .../fetchCommunityMembers.ts} | 0 3 files changed, 3 insertions(+), 3 deletions(-) rename lib/{communityMembers.ts => community/fetchCommunityMembers.ts} (100%) diff --git a/components/Modal/CommunityMembers/CommunityMembersModal.tsx b/components/Modal/CommunityMembers/CommunityMembersModal.tsx index d4e9527..05fe917 100644 --- a/components/Modal/CommunityMembers/CommunityMembersModal.tsx +++ b/components/Modal/CommunityMembers/CommunityMembersModal.tsx @@ -16,7 +16,7 @@ import { Stack, Text, } from "@chakra-ui/react"; -import { CommunityMember } from "@/lib/communityMembers"; +import { CommunityMember } from "@/lib/community/fetchCommunityMembers"; import useCommunityMembers from "@/hooks/community/useCommunityMembers"; type CommunityMembersModalProps = { diff --git a/hooks/community/useCommunityMembers.ts b/hooks/community/useCommunityMembers.ts index 4d31f72..933f09e 100644 --- a/hooks/community/useCommunityMembers.ts +++ b/hooks/community/useCommunityMembers.ts @@ -1,10 +1,10 @@ -'use client'; +"use client"; import { useCallback, useState } from "react"; import { CommunityMember, fetchCommunityMembers, -} from "@/lib/communityMembers"; +} from "@/lib/community/fetchCommunityMembers"; const useCommunityMembers = () => { const [members, setMembers] = useState([]); diff --git a/lib/communityMembers.ts b/lib/community/fetchCommunityMembers.ts similarity index 100% rename from lib/communityMembers.ts rename to lib/community/fetchCommunityMembers.ts From 8a66e27d2993398affd3610c57fd0d204b9e7c42 Mon Sep 17 00:00:00 2001 From: Maruf Bepary Date: Sun, 7 Dec 2025 20:13:44 +0000 Subject: [PATCH 3/4] REFACTOR: Move getPost function to `post` folder --- app/community/[communityId]/comments/[pid]/page.tsx | 2 +- lib/{posts.ts => post/getPost.ts} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename lib/{posts.ts => post/getPost.ts} (100%) diff --git a/app/community/[communityId]/comments/[pid]/page.tsx b/app/community/[communityId]/comments/[pid]/page.tsx index ccf70fe..2a085fc 100644 --- a/app/community/[communityId]/comments/[pid]/page.tsx +++ b/app/community/[communityId]/comments/[pid]/page.tsx @@ -1,7 +1,7 @@ import { getCommunityData } from "@/lib/community/getCommunityData"; -import { getPost } from "@/lib/posts"; import PostClientPage from "./PostClientPage"; import { notFound } from "next/navigation"; +import { getPost } from "@/lib/post/getPost"; export default async function PostPage({ params, diff --git a/lib/posts.ts b/lib/post/getPost.ts similarity index 100% rename from lib/posts.ts rename to lib/post/getPost.ts From 2efffd88c8507d59aff28f85e4f5c4c609e5f80f Mon Sep 17 00:00:00 2001 From: Maruf Bepary Date: Sun, 7 Dec 2025 20:18:05 +0000 Subject: [PATCH 4/4] REFACTOR: Decomposed user profile lib into smaller libs --- hooks/useUserProfile.ts | 10 +-- lib/user-profile/deleteProfileImage.ts | 11 +++ lib/user-profile/updateUserCommentsName.ts | 28 +++++++ lib/user-profile/updateUserPostsName.ts | 28 +++++++ lib/user-profile/uploadProfileImage.ts | 17 ++++ lib/userProfile.ts | 91 ---------------------- 6 files changed, 88 insertions(+), 97 deletions(-) create mode 100644 lib/user-profile/deleteProfileImage.ts create mode 100644 lib/user-profile/updateUserCommentsName.ts create mode 100644 lib/user-profile/updateUserPostsName.ts create mode 100644 lib/user-profile/uploadProfileImage.ts delete mode 100644 lib/userProfile.ts diff --git a/hooks/useUserProfile.ts b/hooks/useUserProfile.ts index 1a5df5c..df2adfd 100644 --- a/hooks/useUserProfile.ts +++ b/hooks/useUserProfile.ts @@ -1,11 +1,9 @@ import { postStateAtom } from "@/atoms/postsAtom"; import { auth } from "@/firebase/clientApp"; -import { - deleteProfileImage, - updateUserCommentsName, - updateUserPostsName, - uploadProfileImage, -} from "@/lib/userProfile"; +import { deleteProfileImage } from "@/lib/user-profile/deleteProfileImage"; +import { updateUserCommentsName } from "@/lib/user-profile/updateUserCommentsName"; +import { updateUserPostsName } from "@/lib/user-profile/updateUserPostsName"; +import { uploadProfileImage } from "@/lib/user-profile/uploadProfileImage"; import { useSetAtom } from "jotai"; import { useRouter } from "next/navigation"; import { useState } from "react"; diff --git a/lib/user-profile/deleteProfileImage.ts b/lib/user-profile/deleteProfileImage.ts new file mode 100644 index 0000000..9d666ce --- /dev/null +++ b/lib/user-profile/deleteProfileImage.ts @@ -0,0 +1,11 @@ +import { storage } from "@/firebase/clientApp"; +import { deleteObject, ref } from "firebase/storage"; + +/** + * Deletes the profile image from Firebase Storage. + * @param userId - ID of the user + */ +export const deleteProfileImage = async (userId: string) => { + const imageRef = ref(storage, `users/${userId}/profileImage`); + await deleteObject(imageRef); +}; diff --git a/lib/user-profile/updateUserCommentsName.ts b/lib/user-profile/updateUserCommentsName.ts new file mode 100644 index 0000000..75b95e4 --- /dev/null +++ b/lib/user-profile/updateUserCommentsName.ts @@ -0,0 +1,28 @@ +import { firestore } from "@/firebase/clientApp"; +import { collection, doc, getDocs, query, where, writeBatch } from "firebase/firestore"; + +/** + * Updates the name of the creator of the comments. + * Finds all the comments a user has created and updates the creator name. + * @param userId - ID of the user whose comments are to be updated + * @param newUserName - New name of the user + */ +export const updateUserCommentsName = async ( + userId: string, + newUserName: string +) => { + const commentsQuery = query( + collection(firestore, "comments"), + where("creatorId", "==", userId) + ); + const commentsSnapshot = await getDocs(commentsQuery); + + const batch = writeBatch(firestore); + + commentsSnapshot.forEach((commentDoc) => { + const commentRef = doc(firestore, "comments", commentDoc.id); + batch.update(commentRef, { creatorDisplayText: newUserName }); + }); + + await batch.commit(); +}; diff --git a/lib/user-profile/updateUserPostsName.ts b/lib/user-profile/updateUserPostsName.ts new file mode 100644 index 0000000..49d8b98 --- /dev/null +++ b/lib/user-profile/updateUserPostsName.ts @@ -0,0 +1,28 @@ +import { firestore } from "@/firebase/clientApp"; +import { collection, doc, getDocs, query, where, writeBatch } from "firebase/firestore"; + +/** + * Updates the name of the creator of the posts. + * Finds all the posts a user has created and updates the creator name. + * @param userId - ID of the user whose posts are to be updated + * @param newUserName - New name of the user + */ +export const updateUserPostsName = async ( + userId: string, + newUserName: string +) => { + const postsQuery = query( + collection(firestore, "posts"), + where("creatorId", "==", userId) + ); + const postsSnapshot = await getDocs(postsQuery); + + const batch = writeBatch(firestore); + + postsSnapshot.forEach((postDoc) => { + const postRef = doc(firestore, "posts", postDoc.id); + batch.update(postRef, { creatorUsername: newUserName }); + }); + + await batch.commit(); +}; diff --git a/lib/user-profile/uploadProfileImage.ts b/lib/user-profile/uploadProfileImage.ts new file mode 100644 index 0000000..f3b197c --- /dev/null +++ b/lib/user-profile/uploadProfileImage.ts @@ -0,0 +1,17 @@ +import { storage } from "@/firebase/clientApp"; +import { getDownloadURL, ref, uploadString } from "firebase/storage"; + +/** + * Uploads a profile image to Firebase Storage. + * @param userId - ID of the user + * @param selectedFile - Base64 string of the image + * @returns Download URL of the uploaded image + */ +export const uploadProfileImage = async ( + userId: string, + selectedFile: string +) => { + const imageRef = ref(storage, `users/${userId}/profileImage`); + await uploadString(imageRef, selectedFile, "data_url"); + return await getDownloadURL(imageRef); +}; diff --git a/lib/userProfile.ts b/lib/userProfile.ts deleted file mode 100644 index bbd1b7c..0000000 --- a/lib/userProfile.ts +++ /dev/null @@ -1,91 +0,0 @@ -import { firestore, storage } from "@/firebase/clientApp"; -import { - collection, - doc, - getDocs, - query, - where, - writeBatch, -} from "firebase/firestore"; -import { - deleteObject, - getDownloadURL, - ref, - uploadString, -} from "firebase/storage"; - -/** - * Updates the name of the creator of the comments. - * Finds all the comments a user has created and updates the creator name. - * @param {string} userId - ID of the user whose comments are to be updated - * @param {string} newUserName - New name of the user - */ -export const updateUserCommentsName = async ( - userId: string, - newUserName: string -) => { - const commentsQuery = query( - collection(firestore, "comments"), - where("creatorId", "==", userId) - ); - const commentsSnapshot = await getDocs(commentsQuery); - - const batch = writeBatch(firestore); - - commentsSnapshot.forEach((commentDoc) => { - const commentRef = doc(firestore, "comments", commentDoc.id); - batch.update(commentRef, { creatorDisplayText: newUserName }); - }); - - await batch.commit(); -}; - -/** - * Updates the name of the creator of the posts. - * Finds all the posts a user has created and updates the creator name. - * @param {string} userId - ID of the user whose posts are to be updated - * @param {string} newUserName - New name of the user - */ -export const updateUserPostsName = async ( - userId: string, - newUserName: string -) => { - const postsQuery = query( - collection(firestore, "posts"), - where("creatorId", "==", userId) - ); - const postsSnapshot = await getDocs(postsQuery); - - const batch = writeBatch(firestore); - - postsSnapshot.forEach((postDoc) => { - const postRef = doc(firestore, "posts", postDoc.id); - batch.update(postRef, { creatorUsername: newUserName }); - }); - - await batch.commit(); -}; - -/** - * Uploads a profile image to Firebase Storage. - * @param {string} userId - ID of the user - * @param {string} selectedFile - Base64 string of the image - * @returns {Promise} - Download URL of the uploaded image - */ -export const uploadProfileImage = async ( - userId: string, - selectedFile: string -) => { - const imageRef = ref(storage, `users/${userId}/profileImage`); - await uploadString(imageRef, selectedFile, "data_url"); - return await getDownloadURL(imageRef); -}; - -/** - * Deletes the profile image from Firebase Storage. - * @param {string} userId - ID of the user - */ -export const deleteProfileImage = async (userId: string) => { - const imageRef = ref(storage, `users/${userId}/profileImage`); - await deleteObject(imageRef); -};