From 43c8268d59c2bbee8eecc3439edd7dc108cd6d52 Mon Sep 17 00:00:00 2001 From: Aamil13 Date: Thu, 25 Jul 2024 18:20:57 +0530 Subject: [PATCH] refactored and added type --- src/app/community/[id]/page.tsx | 96 ++++++++++++++++++---------- src/app/community/page.tsx | 14 +++- src/app/post/[id]/page.tsx | 67 ++++++++++--------- src/services/community-university.ts | 12 ++-- src/services/university-community.ts | 20 ++++-- src/services/user.ts | 4 +- src/services/userProfile.ts | 4 +- src/utils/ZustandSocketProvider.tsx | 8 +-- 8 files changed, 140 insertions(+), 85 deletions(-) diff --git a/src/app/community/[id]/page.tsx b/src/app/community/[id]/page.tsx index e7e1fd44..cd4085e3 100644 --- a/src/app/community/[id]/page.tsx +++ b/src/app/community/[id]/page.tsx @@ -26,6 +26,27 @@ const roberta = { comment: 'Sorry that was a strange thing to ask.', replyingTo: 'Johnny Nitro and Kathryn Murphy', } + +interface communityPostType { + _id: string + user_id: { + firstName: string + lastName: string + _id: string + university_name: string + study_year: string + degree: string + profile_dp: { + imageUrl: string + } + } + content: string + createdAt: string + likeCount: [] + comments: [] + imageUrl: [] +} + const Page = () => { const [isModalOpen, setIsModalOpen] = useState(false) const [modalContentType, setModalContentType] = useState() @@ -40,7 +61,7 @@ const Page = () => { const [isJoinedInGroup, setIsJoinedInGroup] = useState(false) const { data: communityGroupPost, - isLoading: communityGroupPostLoading, + isFetching: communityGroupPostLoading, isError, } = useGetCommunityGroupPost(currSelectedGroup?._id, isJoinedInGroup) @@ -79,6 +100,44 @@ const Page = () => { } }, [currSelectedGroup, userData]) + const PostContainer = () => { + if (communityGroupPostLoading) { + return + } + if (isError) { + return
Something went wrong!
+ } + if (!communityGroupPost?.communityPosts.length) { + return
No post Yet!
+ } + return communityGroupPost?.communityPosts.map((item: communityPostType) => ( +
+ +
+ )) + } + return ( <> setIsModalOpen(false)}> @@ -123,40 +182,7 @@ const Page = () => { '' )} - {communityGroupPostLoading ? ( - - ) : isError ? ( -
Something went wrong!
- ) : !communityGroupPost?.communityPosts.length ? ( -
No post Yet!
- ) : ( - communityGroupPost?.communityPosts.map((item: any) => ( -
- -
- )) - )} + )} diff --git a/src/app/community/page.tsx b/src/app/community/page.tsx index 77bcd72a..7d707fc6 100644 --- a/src/app/community/page.tsx +++ b/src/app/community/page.tsx @@ -6,10 +6,18 @@ import UniversityCard from '@/components/universityCommunity/universityCommunity import { useGetUserSubscribedCommunityGroups } from '@/services/university-community' import Loading from '../loading' +interface CommunityType { + _id: string + communityLogoUrl: { + imageUrl: string + } + name: string + collegeID: string +} const Page = () => { - const { data: SubscribedData, isLoading } = useGetUserSubscribedCommunityGroups() + const { data: SubscribedData, isFetching } = useGetUserSubscribedCommunityGroups() - if (isLoading) return + if (isFetching) return return ( <> @@ -18,7 +26,7 @@ const Page = () => {

Joined Communities

{SubscribedData?.community?.length ? (
- {SubscribedData?.community?.map((item: any) => ( + {SubscribedData?.community?.map((item: CommunityType) => ( { const { id } = useParams<{ id: string }>() const searchParams = useSearchParams() const Type = searchParams.get('isType') const [isModalOpen, setIsModalOpen] = useState(false) const [modalContentType, setModalContentType] = useState() - const { data, isLoading } = useGetPost(id, Type) + const { data, isFetching } = useGetPost(id, Type) const item = data?.post - + const [cookieValue] = useCookie('uni_user_token') const { userProfileData } = useUniStore() const modalContent = (modalContentType: string) => { switch (modalContentType) { @@ -33,6 +34,40 @@ const UserPost = () => { } } + const PostHolder = () => { + if (!cookieValue) { + return
Login to view Post.
+ } + if (isFetching) { + return + } + return ( + + ) + } + return (
setIsModalOpen(false)}> @@ -41,33 +76,7 @@ const UserPost = () => {
- {isLoading ? ( - - ) : ( - - )} +
) diff --git a/src/services/community-university.ts b/src/services/community-university.ts index 4f9cc1d4..4c6b6c1e 100644 --- a/src/services/community-university.ts +++ b/src/services/community-university.ts @@ -74,7 +74,7 @@ export async function getAllCommunityGroupPost(communityId: string, token: any) //posts export async function getPost(postID: string, isType: string | null, token: any) { - const response: any = await client(`/post/singlePost/${postID}?isType=${isType}`, { headers: { Authorization: `Bearer ${token}` } }) + const response: any = await client(`/communitypost/post/${postID}?isType=${isType}`, { headers: { Authorization: `Bearer ${token}` } }) return response } @@ -228,7 +228,7 @@ export const useUpdateCommunityGroup = () => { export function useGetCommunityGroupPost(communityId: string, isJoined: boolean) { const [cookieValue] = useCookie('uni_user_token') - const { isLoading, data, error, isError } = useQuery({ + const { isFetching, data, error, isError } = useQuery({ queryKey: ['communityGroupsPost', communityId], queryFn: () => getAllCommunityGroupPost(communityId, cookieValue), enabled: isJoined, @@ -239,7 +239,7 @@ export function useGetCommunityGroupPost(communityId: string, isJoined: boolean) errorMessage = error.response.data } - return { isLoading, data, isError, error: errorMessage } + return { isFetching, data, isError, error: errorMessage } } export const useLikeUnilikeGroupPost = () => { @@ -360,10 +360,10 @@ export const useUserGroupRole = () => { export function useGetPost(postId: string, isType: string | null) { const [cookieValue] = useCookie('uni_user_token') - const { isLoading, data, error } = useQuery({ + const { isFetching, data, error } = useQuery({ queryKey: ['getPost', postId], queryFn: () => getPost(postId, isType, cookieValue), - enabled: !!postId, + enabled: !!postId && !!cookieValue, }) let errorMessage = null @@ -371,5 +371,5 @@ export function useGetPost(postId: string, isType: string | null) { errorMessage = error.response.data } - return { isLoading, data, error: errorMessage } + return { isFetching, data, error: errorMessage } } diff --git a/src/services/university-community.ts b/src/services/university-community.ts index 0f43c8b4..63ec1058 100644 --- a/src/services/university-community.ts +++ b/src/services/university-community.ts @@ -3,17 +3,29 @@ import { client } from './api-Client' import { useQuery } from '@tanstack/react-query' import axios from 'axios' +interface community { + _id: string + communityLogoUrl: { + imageUrl: string + } + name: string + collegeID: string +} +interface CommunityType { + community: community[] +} + export async function getUserSubscribedCommunityGroups(token: any) { - const response: any = await client(`/community`, { headers: { Authorization: `Bearer ${token}` } }) + const response: CommunityType = await client(`/community`, { headers: { Authorization: `Bearer ${token}` } }) return response } export function useGetUserSubscribedCommunityGroups() { const [cookieValue] = useCookie('uni_user_token') - const { isLoading, data, error } = useQuery({ + const { isFetching, data, error } = useQuery({ queryKey: ['UserSubscribedCommunityGroups'], queryFn: () => getUserSubscribedCommunityGroups(cookieValue), - enabled: true, + enabled: !!cookieValue, }) let errorMessage = null @@ -21,5 +33,5 @@ export function useGetUserSubscribedCommunityGroups() { errorMessage = error.response.data } - return { isLoading, data, error: errorMessage } + return { isFetching, data, error: errorMessage } } diff --git a/src/services/user.ts b/src/services/user.ts index 10bd562d..86b5a586 100644 --- a/src/services/user.ts +++ b/src/services/user.ts @@ -9,13 +9,13 @@ export async function getUserData(token: any, id: string) { return response } -export function useGetUserData() { +export function useGetUserData(type: string) { const [cookieValue] = useCookie('uni_user_token') const { userData } = useUniStore() const { isLoading, data, error, refetch } = useQuery({ queryKey: ['getRefetchUserData'], queryFn: () => getUserData(cookieValue, userData.id), - enabled: !!cookieValue, + enabled: !!cookieValue && type != '', }) let errorMessage = null diff --git a/src/services/userProfile.ts b/src/services/userProfile.ts index 78606b1c..4ce26e1a 100644 --- a/src/services/userProfile.ts +++ b/src/services/userProfile.ts @@ -8,13 +8,13 @@ export async function getUserProfileData(token: any) { return response } -export function useGetUserProfileData() { +export function useGetUserProfileData(type: string) { const [cookieValue] = useCookie('uni_user_token') const { isLoading, data, error, refetch } = useQuery({ queryKey: ['getRefetchUserProfileData'], queryFn: () => getUserProfileData(cookieValue), - enabled: !!cookieValue, + enabled: !!cookieValue && type !== '', }) let errorMessage = null diff --git a/src/utils/ZustandSocketProvider.tsx b/src/utils/ZustandSocketProvider.tsx index c1a2ce26..23ca1ebc 100644 --- a/src/utils/ZustandSocketProvider.tsx +++ b/src/utils/ZustandSocketProvider.tsx @@ -12,12 +12,12 @@ type ZustandSocketProviderProps = { } const ZustandSocketProvider: React.FC = ({ children }) => { - const initializeSocket = useUniStore((state: any) => state.initializeSocket) - const disconnectSocket = useUniStore((state: any) => state.disconnectSocket) + const initializeSocket = useUniStore((state) => state.initializeSocket) + const disconnectSocket = useUniStore((state) => state.disconnectSocket) const { userData, type, setUserUnVerifiedCommunities, setUserVerifiedCommunities, setUserFollowers, setIsRefetched } = useUniStore() const { refetch: refetchNotification } = useGetNotification() - const { refetch: refetchUserData, data: RefetcheduserData } = useGetUserData() - const { refetch: refetchUserProfileData, data: RefetcheduserProfileData } = useGetUserProfileData() + const { refetch: refetchUserData, data: RefetcheduserData } = useGetUserData(type) + const { refetch: refetchUserProfileData, data: RefetcheduserProfileData } = useGetUserProfileData(type) useEffect(() => { if (userData.id) {