Skip to content

Commit

Permalink
refactored and added type
Browse files Browse the repository at this point in the history
  • Loading branch information
Aamil13 committed Jul 25, 2024
1 parent 3edcbf3 commit 43c8268
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 85 deletions.
96 changes: 61 additions & 35 deletions src/app/community/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<ModalContentType>()
Expand All @@ -40,7 +61,7 @@ const Page = () => {
const [isJoinedInGroup, setIsJoinedInGroup] = useState(false)
const {
data: communityGroupPost,
isLoading: communityGroupPostLoading,
isFetching: communityGroupPostLoading,
isError,
} = useGetCommunityGroupPost(currSelectedGroup?._id, isJoinedInGroup)

Expand Down Expand Up @@ -79,6 +100,44 @@ const Page = () => {
}
}, [currSelectedGroup, userData])

const PostContainer = () => {
if (communityGroupPostLoading) {
return <PostSkeleton />
}
if (isError) {
return <div>Something went wrong!</div>
}
if (!communityGroupPost?.communityPosts.length) {
return <div className="text-center font-bold mt-10">No post Yet!</div>
}
return communityGroupPost?.communityPosts.map((item: communityPostType) => (
<div key={item._id} className="border-2 border-neutral-300 rounded-md w-[73%] max-xl:w-10/12 mt-6">
<Post
isType={'communityId' in item ? 'CommunityPost' : 'userPost'}
user={item?.user_id?.firstName + ' ' + item?.user_id?.lastName}
adminId={item.user_id?._id}
university={item?.user_id?.university_name}
year={item?.user_id?.study_year + ' Yr. ' + ' ' + item?.user_id?.degree}
text={item.content}
date={item?.createdAt}
avatar={item?.user_id?.profile_dp?.imageUrl}
likes={item.likeCount}
comments={item.comments.length}
postID={item._id}
reposts={2}
shares={1}
userComments={item.comments}
setModalContentType={setModalContentType}
setIsModalOpen={setIsModalOpen}
isUniversity={true}
profileDp={userProfileData?.profile_dp?.imageUrl}
media={item?.imageUrl}
type={PostType.Community}
/>
</div>
))
}

return (
<>
<Modal isOpen={isModalOpen} onClose={() => setIsModalOpen(false)}>
Expand Down Expand Up @@ -123,40 +182,7 @@ const Page = () => {
''
)}
</div>
{communityGroupPostLoading ? (
<PostSkeleton />
) : isError ? (
<div>Something went wrong!</div>
) : !communityGroupPost?.communityPosts.length ? (
<div className="text-center font-bold mt-10">No post Yet!</div>
) : (
communityGroupPost?.communityPosts.map((item: any) => (
<div key={item._id} className="border-2 border-neutral-300 rounded-md w-[73%] max-xl:w-10/12 mt-6">
<Post
isType={'communityId' in item ? 'CommunityPost' : 'userPost'}
user={item?.user_id?.firstName + ' ' + item?.user_id?.lastName}
adminId={item.user_id?._id}
university={item?.user_id?.university_name}
year={item?.user_id?.study_year + ' Yr. ' + ' ' + item?.user_id?.degree}
text={item.content}
date={item?.createdAt}
avatar={item?.user_id?.profile_dp?.imageUrl}
likes={item.likeCount}
comments={item.comments.length}
postID={item._id}
reposts={2}
shares={1}
userComments={item.comments}
setModalContentType={setModalContentType}
setIsModalOpen={setIsModalOpen}
isUniversity={true}
profileDp={userProfileData?.profile_dp?.imageUrl}
media={item?.imageUrl}
type={PostType.Community}
/>
</div>
))
)}
<PostContainer />
</div>
)}
</div>
Expand Down
14 changes: 11 additions & 3 deletions src/app/community/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Loading />
if (isFetching) return <Loading />

return (
<>
Expand All @@ -18,7 +26,7 @@ const Page = () => {
<h3 className="py-10 text-xl font-bold text-neutral-700">Joined Communities</h3>
{SubscribedData?.community?.length ? (
<div className="flex gap-10 flex-wrap justify-center">
{SubscribedData?.community?.map((item: any) => (
{SubscribedData?.community?.map((item: CommunityType) => (
<UniversityCard
key={item?._id}
universityLogo={item?.communityLogoUrl?.imageUrl}
Expand Down
67 changes: 38 additions & 29 deletions src/app/post/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ import Navbar from '@/components/Timeline/Navbar'
import { PostType } from '@/types/constants'
import { useUniStore } from '@/store/store'
import PostSkeleton from '@/components/Timeline/PostSkeleton'
import useCookie from '@/hooks/useCookie'
const UserPost = () => {
const { id } = useParams<{ id: string }>()
const searchParams = useSearchParams()
const Type = searchParams.get('isType')
const [isModalOpen, setIsModalOpen] = useState(false)
const [modalContentType, setModalContentType] = useState<ModalContentType>()
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) {
Expand All @@ -33,6 +34,40 @@ const UserPost = () => {
}
}

const PostHolder = () => {
if (!cookieValue) {
return <div className="text-center">Login to view Post.</div>
}
if (isFetching) {
return <PostSkeleton />
}
return (
<Post
isType={String(Type)}
isSinglePost={true}
user={item?.user_id?.firstName + ' ' + item?.user_id?.lastName}
adminId={item?.user_id?._id}
university={item?.user_id?.university_name}
year={item?.user_id?.study_year + ' Yr. ' + ' ' + item?.user_id?.degree}
text={item?.content}
date={item?.createdAt}
avatar={item?.user_id?.profile_dp?.imageUrl}
likes={item?.likeCount}
comments={item?.comments.length}
postID={item?._id}
reposts={2}
shares={1}
userComments={item?.comments}
setModalContentType={setModalContentType}
setIsModalOpen={setIsModalOpen}
isUniversity={true}
profileDp={userProfileData?.profile_dp?.imageUrl}
media={item?.imageUrl}
type={String(Type) == 'userPost' ? PostType.Timeline : PostType.Community}
/>
)
}

return (
<div>
<Modal isOpen={isModalOpen} onClose={() => setIsModalOpen(false)}>
Expand All @@ -41,33 +76,7 @@ const UserPost = () => {
<Navbar />

<div className="border-2 border-neutral-300 rounded-md w-3/4 mx-auto mt-6">
{isLoading ? (
<PostSkeleton />
) : (
<Post
isType={String(Type)}
isSinglePost={true}
user={item?.user_id?.firstName + ' ' + item?.user_id?.lastName}
adminId={item?.user_id?._id}
university={item?.user_id?.university_name}
year={item?.user_id?.study_year + ' Yr. ' + ' ' + item?.user_id?.degree}
text={item?.content}
date={item?.createdAt}
avatar={item?.user_id?.profile_dp?.imageUrl}
likes={item?.likeCount}
comments={item?.comments.length}
postID={item?._id}
reposts={2}
shares={1}
userComments={item?.comments}
setModalContentType={setModalContentType}
setIsModalOpen={setIsModalOpen}
isUniversity={true}
profileDp={userProfileData?.profile_dp?.imageUrl}
media={item?.imageUrl}
type={String(Type) == 'userPost' ? PostType.Timeline : PostType.Community}
/>
)}
<PostHolder />
</div>
</div>
)
Expand Down
12 changes: 6 additions & 6 deletions src/services/community-university.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand All @@ -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 = () => {
Expand Down Expand Up @@ -360,16 +360,16 @@ 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
if (axios.isAxiosError(error) && error.response) {
errorMessage = error.response.data
}

return { isLoading, data, error: errorMessage }
return { isFetching, data, error: errorMessage }
}
20 changes: 16 additions & 4 deletions src/services/university-community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,35 @@ 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
if (axios.isAxiosError(error) && error.response) {
errorMessage = error.response.data
}

return { isLoading, data, error: errorMessage }
return { isFetching, data, error: errorMessage }
}
4 changes: 2 additions & 2 deletions src/services/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/services/userProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/utils/ZustandSocketProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ type ZustandSocketProviderProps = {
}

const ZustandSocketProvider: React.FC<ZustandSocketProviderProps> = ({ 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) {
Expand Down

0 comments on commit 43c8268

Please sign in to comment.