diff --git a/src/app/[id]/page.tsx b/src/app/[id]/page.tsx index e04a602d..ac1dd07d 100644 --- a/src/app/[id]/page.tsx +++ b/src/app/[id]/page.tsx @@ -1,6 +1,5 @@ 'use client' import Footer from '@/components/Footer/Footer' -import Navbar from '@/components/Timeline/Navbar' import Modal from '@/components/Timeline/Modal' import EditProfileModal from '@/components/Timeline/Modals/EditProfileModal' import ProfileCard from '@/components/Timeline/ProfileCard' @@ -30,7 +29,6 @@ const Profile = () => { setIsModalOpen(false)}> {modalContentType && modalContent(modalContentType)} -
{ return (
-
{ const [isModalOpen, setIsModalOpen] = useState(false) const [modalContentType, setModalContentType] = useState() @@ -37,7 +58,11 @@ const Page = () => { const { data: communityGroups } = useGetCommunityGroups(id, isJoined) const [currSelectedGroup, setCurrSelectedGroup] = useState(communityGroups?.groups[0]) const [isJoinedInGroup, setIsJoinedInGroup] = useState(false) - const { data: communityGroupPost } = useGetCommunityGroupPost(currSelectedGroup?._id, isJoinedInGroup) + const { + data: communityGroupPost, + isFetching: communityGroupPostLoading, + isError, + } = useGetCommunityGroupPost(currSelectedGroup?._id, isJoinedInGroup) const modalContent = (modalContentType: string) => { switch (modalContentType) { @@ -52,6 +77,10 @@ const Page = () => { } } + useEffect(() => { + setCurrSelectedGroup(communityGroups?.groups[0]) + }, [communityGroups && !!currSelectedGroup]) + useEffect(() => { const findGroupRole = (communities: any) => { return communities?.some((community: any) => { @@ -69,12 +98,50 @@ const Page = () => { findGroupRole(userData.userUnVerifiedCommunities) } }, [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)}> {modalContentType && modalContent(modalContentType)} -
@@ -113,31 +180,7 @@ const Page = () => { '' )}
- {communityGroupPost?.communityPosts.map((item: any) => ( -
- -
- ))} +
)}
diff --git a/src/app/community/loading.tsx b/src/app/community/loading.tsx index 2b019b0d..b5d9a4f0 100644 --- a/src/app/community/loading.tsx +++ b/src/app/community/loading.tsx @@ -1,4 +1,11 @@ export default function Loading() { - // You can add any UI inside Loading, including a Skeleton. - return
dsdsdsdsdsd
+ return ( +
+
+ + + +
+
+ ) } diff --git a/src/app/community/page.tsx b/src/app/community/page.tsx index 77bcd72a..08cc8fcd 100644 --- a/src/app/community/page.tsx +++ b/src/app/community/page.tsx @@ -1,24 +1,30 @@ 'use client' import React from 'react' -import Navbar from '@/components/Timeline/Navbar' import Footer from '@/components/Footer/Footer' import UniversityCard from '@/components/universityCommunity/universityCommunityCart' 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 ( <> -

Joined Communities

{SubscribedData?.community?.length ? (
- {SubscribedData?.community?.map((item: any) => ( + {SubscribedData?.community?.map((item: CommunityType) => ( + {children} diff --git a/src/app/post/[id]/page.tsx b/src/app/post/[id]/page.tsx new file mode 100644 index 00000000..8fb52a3f --- /dev/null +++ b/src/app/post/[id]/page.tsx @@ -0,0 +1,85 @@ +'use client' +import Post from '@/components/Timeline/Post' +import { useGetPost } from '@/services/community-university' +import { useParams, useSearchParams } from 'next/navigation' +import Modal from '@/components/Timeline/Modal' +import ConnectionsModal from '@/components/Timeline/Modals/ConnectionsModal' +import PollModal from '@/components/Timeline/Modals/PollModal' + +import React, { useState } from 'react' +import { ModalContentType } from '@/types/global' + +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() + const { data, isFetching, isPending } = useGetPost(id, Type) + const item = data?.post + const [cookieValue] = useCookie('uni_user_token') + const { userProfileData } = useUniStore() + const modalContent = (modalContentType: string) => { + switch (modalContentType) { + case 'ConnectionsModal': + return + case 'PollModal': + return + default: + return null + } + } + + const PostHolder = () => { + if (!cookieValue && !isPending) { + return
Login to view Post.
+ } + if (isFetching || isPending) { + return + } + + return ( + + ) + } + + return ( +
+ setIsModalOpen(false)}> + {modalContentType && modalContent(modalContentType)} + + +
+ +
+
+ ) +} + +export default UserPost diff --git a/src/app/timeline/page.tsx b/src/app/timeline/page.tsx index b4849056..2714d68e 100644 --- a/src/app/timeline/page.tsx +++ b/src/app/timeline/page.tsx @@ -1,6 +1,5 @@ 'use client' import React, { useState } from 'react' -import Navbar from '@/components/Timeline/Navbar' import ProfileCard from '@/components/Timeline/ProfileCard' import PostInput from '@/components/Timeline/PostInput' import Dropdown from '@/components/Timeline/DropDown' @@ -12,7 +11,7 @@ import PollModal from '@/components/Timeline/Modals/PollModal' import EditProfileModal from '@/components/Timeline/Modals/EditProfileModal' import ReplyModal from '@/components/Timeline/Modals/ReplyModal' import { ModalContentType } from '@/types/global' -import { PostInputType, PostType } from '@/types/constants' +import { PostInputType, PostType, singlePostEnum } from '@/types/constants' import Recommendations from '@/components/Timeline/Recommendations' import { useUniStore } from '@/store/store' import { useGetUserPosts } from '@/services/community-timeline' @@ -117,6 +116,7 @@ const Timeline = () => { setIsModalOpen={setIsModalOpen} postID={post._id} type={PostType.Timeline} + isType={'communityId' in post ? singlePostEnum.CommunityPost : singlePostEnum.userPost} /> ) }) @@ -127,7 +127,6 @@ const Timeline = () => { setIsModalOpen(false)}> {modalContentType && modalContent(modalContentType)} -
{ const [hover, setHover] = useState(false) const [activeItem, setActiveItem] = useState('') const [, , deleteCookie] = useCookie('uni_user_token') - const { userProfileData, userData, resetUserData, resetUserProfileData, resetUserFollowingData } = useUniStore() + const { userProfileData, userData, resetUserData, resetUserProfileData } = useUniStore() const router = useRouter() const { data: notificationData } = useGetNotification() @@ -62,7 +62,6 @@ const Navbar: React.FC = () => { deleteCookie() resetUserData() resetUserProfileData() - resetUserFollowingData() setIsLogin(false) router.push('/login') } diff --git a/src/components/Timeline/Navbar.tsx b/src/components/Timeline/Navbar.tsx index dc293d70..7619615b 100644 --- a/src/components/Timeline/Navbar.tsx +++ b/src/components/Timeline/Navbar.tsx @@ -3,6 +3,7 @@ import React from 'react' import { CommunityNavbarLinks } from '@/types/constants' import { useRouter, usePathname } from 'next/navigation' import { useUniStore } from '@/store/store' +import useCookie from '@/hooks/useCookie' const Navbar: React.FC = () => { // const tabs = ['Timeline', 'Profile', 'Notifications', 'Messages', 'Connections', 'University Community', 'Chatbot'] @@ -11,7 +12,11 @@ const Navbar: React.FC = () => { const id = userData?.id const path = usePathname() const activeTab = path.split('/').pop() + const [cookieValue] = useCookie('uni_user_token') + if (!cookieValue) { + return + } return (
)} - {userComments.length && showCommentSec ?
Most Relevant / Most Recent
: ''} + {userComments?.length && showCommentSec ?
Most Relevant / Most Recent
: ''} {/* Comments Section */}
- {userComments.map((comment: any) => ( + {userComments?.map((comment: any) => (
{/* Comment Info */}
@@ -401,7 +424,7 @@ const Post: React.FC = ({ ))}
- {userComments.length > 5 && showCommentSec ? : ''} + {userComments?.length > 5 && showCommentSec ? : ''}
diff --git a/src/components/Timeline/PostInput.tsx b/src/components/Timeline/PostInput.tsx index 89ea0547..d4e3607d 100644 --- a/src/components/Timeline/PostInput.tsx +++ b/src/components/Timeline/PostInput.tsx @@ -13,6 +13,7 @@ import { useCreateGroupPost } from '@/services/community-university' import { replaceImage } from '@/services/uploadImage' import { useCreateUserPost } from '@/services/community-timeline' import { CommunityPostData, PostInputData, PostInputType } from '@/types/constants' +import { Spinner } from '../spinner/Spinner' interface PostInputProps { setModalContentType: React.Dispatch> setIsModalOpen: React.Dispatch> @@ -24,8 +25,10 @@ interface PostInputProps { const PostInput: React.FC = ({ setIsModalOpen, setModalContentType, idToPost, profileDp, type }) => { const [inputValue, setInputValue] = useState('') const [ImageValue, setImageValue] = useState([]) - const { mutate: CreateGroupPost } = useCreateGroupPost() + const [isLoading, setIsLoading] = useState(false) + const { mutate: CreateGroupPost, isPending } = useCreateGroupPost() const { mutate: CreateTimelinePost } = useCreateUserPost() + const handleEmojiClick = (emojiData: any) => { setInputValue((prevValue) => prevValue + emojiData.emoji) } @@ -44,6 +47,8 @@ const PostInput: React.FC = ({ setIsModalOpen, setModalContentTy return console.log('Please type something to post!') } + setIsLoading(true) + if (ImageValue) { const imagedata = await processImages(ImageValue) const data: PostInputData = { @@ -74,6 +79,8 @@ const PostInput: React.FC = ({ setIsModalOpen, setModalContentTy CreateTimelinePost(data) } } + + setIsLoading(false) } return ( @@ -89,8 +96,12 @@ const PostInput: React.FC = ({ setIsModalOpen, setModalContentTy value={inputValue} onChange={(e) => setInputValue(e.target.value)} /> -
{ImageValue && ( diff --git a/src/components/communityProfile/CommunityProfileContainer.tsx b/src/components/communityProfile/CommunityProfileContainer.tsx index 5f75cf14..96d6da04 100644 --- a/src/components/communityProfile/CommunityProfileContainer.tsx +++ b/src/components/communityProfile/CommunityProfileContainer.tsx @@ -58,6 +58,8 @@ const CommunityProfileContainer = () => { {selectedOption == valueType.Posts ? ( { /> ) : selectedOption == valueType.Media ? ( { /> ) : selectedOption == valueType.Saved ? ( { ) : ( void } @@ -16,10 +17,11 @@ const CreateNewGroup = ({ setNewGroup }: Props) => { const [logoImage, setLogoImage] = useState() const [coverImage, setCoverImage] = useState() const [userPopUp, setUserPopUp] = useState(false) + const [isLoading, setIsLoading] = useState(false) const [selectedUsers, setSelectedUsers] = useState([]) const selectedUsersId = selectedUsers.map((item: any) => item._id) const [searchInput, setSearchInput] = useState('') - const { mutate: createGroup } = useCreateCommunityGroup() + const { mutate: createGroup, isPending } = useCreateCommunityGroup() const { register: GroupRegister, handleSubmit: handleGroupCreate, @@ -30,7 +32,7 @@ const CreateNewGroup = ({ setNewGroup }: Props) => { const onGroupSubmit = async (data: any) => { let CoverImageData let logoImageData - + setIsLoading(true) if (coverImage) { const imagedata: any = await replaceImage(coverImage, '') CoverImageData = { communityGroupLogoCoverUrl: { imageUrl: imagedata?.imageUrl, publicId: imagedata?.publicId } } @@ -47,6 +49,7 @@ const CreateNewGroup = ({ setNewGroup }: Props) => { selectedUsersId, } createGroup({ communityId: id, data: dataToPush }) + setIsLoading(false) } const { data } = useGetCommunityUsers(id, userPopUp, values.communityGroupType, searchInput) @@ -81,6 +84,9 @@ const CreateNewGroup = ({ setNewGroup }: Props) => { className="w-full pl-12 pr-3 py-2 text-gray-500 bg-transparent outline-none border border-neutral-300 rounded-2xl" />
+ {data?.user?.map((item: any) => ( ))} @@ -166,9 +172,15 @@ const CreateNewGroup = ({ setNewGroup }: Props) => {
+ -
diff --git a/src/components/spinner/Spinner.tsx b/src/components/spinner/Spinner.tsx new file mode 100644 index 00000000..4a70abbc --- /dev/null +++ b/src/components/spinner/Spinner.tsx @@ -0,0 +1,10 @@ +import React from 'react' + +export const Spinner = () => { + return ( +
+ ) +} diff --git a/src/components/universityCommunity/universityCommunityCart.tsx b/src/components/universityCommunity/universityCommunityCart.tsx index ee95ea90..380d479b 100644 --- a/src/components/universityCommunity/universityCommunityCart.tsx +++ b/src/components/universityCommunity/universityCommunityCart.tsx @@ -24,7 +24,7 @@ const UniversityCommunityCart = ({ UniversityName, UniversityAddress, University } } return ( -
+

{UniversityName}

{address}

diff --git a/src/hooks/useCookie.ts b/src/hooks/useCookie.ts index fdf64313..89bbe0e7 100644 --- a/src/hooks/useCookie.ts +++ b/src/hooks/useCookie.ts @@ -1,3 +1,4 @@ +'use client' import { useState, useEffect } from 'react' const useCookie = (cookieName: string): [string, (value: string, expirationDate: string) => void, () => void] => { diff --git a/src/services/auth.ts b/src/services/auth.ts index ac91b1e6..06cef347 100644 --- a/src/services/auth.ts +++ b/src/services/auth.ts @@ -17,7 +17,6 @@ const register = async (data: Omit): Pr export const useHandleLogin = () => { const setUserData = useUniStore((state) => state.setUserData) const setUserProfileData = useUniStore((state) => state.setUserProfileData) - const setUserFollowingData = useUniStore((state) => state.setUserFollowingData) const [, setCookieValue] = useCookie('uni_user_token') const [, setRefreshCookieValue] = useCookie('uni_user_refresh_token') @@ -27,7 +26,6 @@ export const useHandleLogin = () => { onSuccess: (response: any) => { setUserData(response.user) setUserProfileData(response.userProfile) - setUserFollowingData(response.Following) // setToken(response.tokens) setCookieValue(response.tokens.access.token, response.tokens.access.expires) setRefreshCookieValue(response.tokens.refresh.token, response.tokens.refresh.expires) diff --git a/src/services/community-timeline.ts b/src/services/community-timeline.ts index aa626cb4..10ea5724 100644 --- a/src/services/community-timeline.ts +++ b/src/services/community-timeline.ts @@ -44,14 +44,18 @@ export async function LikeUnlikeUserPostComment(UserPostCommentId: string, token //Query Functions for UserPost, UserPostComment -export const useCreateUserPostComment = () => { +export const useCreateUserPostComment = (isSinglePost: boolean) => { const [cookieValue] = useCookie('uni_user_token') const queryClient = useQueryClient() return useMutation({ mutationFn: (data: PostCommentData) => CreateUserPostComment(data, cookieValue), onSuccess: () => { - queryClient.invalidateQueries({ queryKey: ['userPosts'] }) + if (isSinglePost) { + queryClient.invalidateQueries({ queryKey: ['getPost'] }) + } else { + queryClient.invalidateQueries({ queryKey: ['userPosts'] }) + } }, onError: (res: any) => { console.log(res.response.data.message, 'res') diff --git a/src/services/community-university.ts b/src/services/community-university.ts index 83d8e215..eb5929da 100644 --- a/src/services/community-university.ts +++ b/src/services/community-university.ts @@ -73,6 +73,11 @@ 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(`/communitypost/post/${postID}?isType=${isType}`, { headers: { Authorization: `Bearer ${token}` } }) + return response +} + export async function CreateGroupPost(data: any, token: any) { const response = await client(`/communitypost`, { method: 'POST', headers: { Authorization: `Bearer ${token}` }, data }) return response @@ -91,17 +96,17 @@ export async function LikeUnilikeGroupPostCommnet(communityGroupPostCommentId: s } export function useGetCommunity(communityId: string) { - const { isLoading, data, error } = useQuery({ + const state = useQuery({ queryKey: ['community', communityId], queryFn: () => getCommunity(communityId), }) let errorMessage = null - if (axios.isAxiosError(error) && error.response) { - errorMessage = error.response.data + if (axios.isAxiosError(state.error) && state.error.response) { + errorMessage = state.error.response.data } - return { isLoading, data, error: errorMessage } + return { ...state, error: errorMessage } } export const useUpdateCommunity = () => { @@ -152,18 +157,18 @@ export const useLeaveCommunity = () => { export function useGetCommunityGroups(communityId: string, isJoined: boolean) { const [cookieValue] = useCookie('uni_user_token') - const { isLoading, data, error } = useQuery({ + const state = useQuery({ enabled: isJoined, queryKey: ['communityGroups', communityId], queryFn: () => getAllCommunityGroups(communityId, cookieValue), }) let errorMessage = null - if (axios.isAxiosError(error) && error.response) { - errorMessage = error.response.data + if (axios.isAxiosError(state.error) && state.error.response) { + errorMessage = state.error.response.data } - return { isLoading, data, error: errorMessage } + return { ...state, error: errorMessage } } export const useJoinCommunityGroup = () => { @@ -223,18 +228,18 @@ export const useUpdateCommunityGroup = () => { export function useGetCommunityGroupPost(communityId: string, isJoined: boolean) { const [cookieValue] = useCookie('uni_user_token') - const { isLoading, data, error } = useQuery({ + const state = useQuery({ queryKey: ['communityGroupsPost', communityId], queryFn: () => getAllCommunityGroupPost(communityId, cookieValue), enabled: isJoined, }) let errorMessage = null - if (axios.isAxiosError(error) && error.response) { - errorMessage = error.response.data + if (axios.isAxiosError(state.error) && state.error.response) { + errorMessage = state.error.response.data } - return { isLoading, data, error: errorMessage } + return { ...state, error: errorMessage } } export const useLikeUnilikeGroupPost = () => { @@ -269,14 +274,18 @@ export const useCreateGroupPost = () => { }) } -export const useCreateGroupPostComment = () => { +export const useCreateGroupPostComment = (isSinglePost: boolean) => { const [cookieValue] = useCookie('uni_user_token') const queryClient = useQueryClient() return useMutation({ mutationFn: (data: any) => CreateGroupPostComment(data, cookieValue), onSuccess: () => { - queryClient.invalidateQueries({ queryKey: ['communityGroupsPost'] }) + if (isSinglePost) { + queryClient.invalidateQueries({ queryKey: ['getPost'] }) + } else { + queryClient.invalidateQueries({ queryKey: ['communityGroupsPost'] }) + } }, onError: (res: any) => { console.log(res.response.data.message, 'res') @@ -301,34 +310,34 @@ export const useLikeUnlikeGroupPostComment = () => { export function useGetCommunityUsers(communityId: string, isopen: boolean, privacy: string, name: string) { const [cookieValue] = useCookie('uni_user_token') - const { isLoading, data, error } = useQuery({ + const state = useQuery({ enabled: isopen, queryKey: ['communityUsers', communityId, privacy, name], queryFn: () => getCommunityUsers(communityId, privacy, name, cookieValue), }) let errorMessage = null - if (axios.isAxiosError(error) && error.response) { - errorMessage = error.response.data + if (axios.isAxiosError(state.error) && state.error.response) { + errorMessage = state.error.response.data } - return { isLoading, data, error: errorMessage } + return { ...state, error: errorMessage } } export function useGetCommunityGroupUsers(communityGroupId: string, isopen: boolean, name: string) { const [cookieValue] = useCookie('uni_user_token') - const { isLoading, data, error } = useQuery({ + const state = useQuery({ enabled: isopen, queryKey: ['communityGroupUsers', communityGroupId, name], queryFn: () => getCommunityGroupUsers(communityGroupId, name, cookieValue), }) let errorMessage = null - if (axios.isAxiosError(error) && error.response) { - errorMessage = error.response.data + if (axios.isAxiosError(state.error) && state.error.response) { + errorMessage = state.error.response.data } - return { isLoading, data, error: errorMessage } + return { ...state, error: errorMessage } } export const useUserGroupRole = () => { @@ -347,3 +356,20 @@ export const useUserGroupRole = () => { }, }) } + +export function useGetPost(postId: string, isType: string | null) { + const [cookieValue] = useCookie('uni_user_token') + + const state = useQuery({ + queryKey: ['getPost', postId], + queryFn: () => getPost(postId, isType, cookieValue), + enabled: !!postId, + retry: 1, + }) + + let errorMessage = null + if (axios.isAxiosError(state.error) && state.error.response) { + errorMessage = state.error.response.data + } + return { ...state, error: errorMessage } +} diff --git a/src/services/connection.ts b/src/services/connection.ts index dc3ac228..5e450694 100644 --- a/src/services/connection.ts +++ b/src/services/connection.ts @@ -35,18 +35,18 @@ export async function toggleFollow(id: string, token: any) { export function useGetAllUserWithProfileData(Name: string, content: boolean) { const [cookieValue] = useCookie('uni_user_token') const debouncedSearchTerm = useDebounce(Name, 1000) - const { isLoading, data, error, isFetching } = useQuery({ + const state = useQuery({ queryKey: ['getAllUsersWithProfile', debouncedSearchTerm], queryFn: () => getUsersWithProfile(cookieValue, debouncedSearchTerm), enabled: !!cookieValue && content, }) let errorMessage = null - if (axios.isAxiosError(error) && error.response) { - errorMessage = error.response.data + if (axios.isAxiosError(state.error) && state.error.response) { + errorMessage = state.error.response.data } - return { isLoading, data, isFetching, error: errorMessage } + return { ...state, error: errorMessage } } export const useToggleFollow = (type: string) => { @@ -73,33 +73,33 @@ export const useToggleFollow = (type: string) => { export function useGetUserFollow(Name: string, content: boolean) { const [cookieValue] = useCookie('uni_user_token') const debouncedSearchTerm = useDebounce(Name, 1000) - const { isLoading, data, error, isFetching } = useQuery({ + const state = useQuery({ queryKey: ['getUserFollow', debouncedSearchTerm], queryFn: () => getUserFollow(cookieValue, debouncedSearchTerm), enabled: !!cookieValue && content, }) let errorMessage = null - if (axios.isAxiosError(error) && error.response) { - errorMessage = error.response.data + if (axios.isAxiosError(state.error) && state.error.response) { + errorMessage = state.error.response.data } - return { isLoading, data, isFetching, error: errorMessage } + return { ...state, error: errorMessage } } export function useGetUserFollowers(Name: string, content: boolean) { const [cookieValue] = useCookie('uni_user_token') const debouncedSearchTerm = useDebounce(Name, 1000) - const { isLoading, data, error, isFetching } = useQuery({ + const state = useQuery({ queryKey: ['getUserFollowers', debouncedSearchTerm], queryFn: () => getUserFollowers(cookieValue, debouncedSearchTerm), enabled: !!cookieValue && content, }) let errorMessage = null - if (axios.isAxiosError(error) && error.response) { - errorMessage = error.response.data + if (axios.isAxiosError(state.error) && state.error.response) { + errorMessage = state.error.response.data } - return { isLoading, data, isFetching, error: errorMessage } + return { ...state, error: errorMessage } } diff --git a/src/services/notification.ts b/src/services/notification.ts index f5d6bd92..dc4a22a7 100644 --- a/src/services/notification.ts +++ b/src/services/notification.ts @@ -23,18 +23,18 @@ export async function UpdateCommunityGroup(data: { id: string }, token: string) export function useGetNotification() { const [cookieValue] = useCookie('uni_user_token') - const { isLoading, data, error, refetch } = useQuery({ + const state = useQuery({ queryKey: ['notification'], queryFn: () => getUserNotification(cookieValue), enabled: !!cookieValue, }) let errorMessage = null - if (axios.isAxiosError(error) && error.response) { - errorMessage = error.response.data + if (axios.isAxiosError(state.error) && state.error.response) { + errorMessage = state.error.response.data } - return { isLoading, data, error: errorMessage, refetch } + return { ...state, error: errorMessage } } export const useJoinCommunityGroup = () => { diff --git a/src/services/university-community.ts b/src/services/university-community.ts index 0f43c8b4..71e4066d 100644 --- a/src/services/university-community.ts +++ b/src/services/university-community.ts @@ -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 state = useQuery({ queryKey: ['UserSubscribedCommunityGroups'], queryFn: () => getUserSubscribedCommunityGroups(cookieValue), - enabled: true, + enabled: !!cookieValue, }) let errorMessage = null - if (axios.isAxiosError(error) && error.response) { - errorMessage = error.response.data + if (axios.isAxiosError(state.error) && state.error.response) { + errorMessage = state.error.response.data } - return { isLoading, data, error: errorMessage } + return { ...state, error: errorMessage } } diff --git a/src/services/user.ts b/src/services/user.ts index 10bd562d..fecaf4a5 100644 --- a/src/services/user.ts +++ b/src/services/user.ts @@ -9,19 +9,19 @@ 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({ + const state = useQuery({ queryKey: ['getRefetchUserData'], queryFn: () => getUserData(cookieValue, userData.id), - enabled: !!cookieValue, + enabled: !!cookieValue && type != '', }) let errorMessage = null - if (axios.isAxiosError(error) && error.response) { - errorMessage = error.response.data + if (axios.isAxiosError(state.error) && state.error.response) { + errorMessage = state.error.response.data } - return { isLoading, data, error: errorMessage, refetch } + return { ...state, error: errorMessage } } diff --git a/src/services/userProfile.ts b/src/services/userProfile.ts index 78606b1c..c25d3bea 100644 --- a/src/services/userProfile.ts +++ b/src/services/userProfile.ts @@ -8,19 +8,19 @@ 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({ + const state = useQuery({ queryKey: ['getRefetchUserProfileData'], queryFn: () => getUserProfileData(cookieValue), - enabled: !!cookieValue, + enabled: !!cookieValue && type !== '', }) let errorMessage = null - if (axios.isAxiosError(error) && error.response) { - errorMessage = error.response.data + if (axios.isAxiosError(state.error) && state.error.response) { + errorMessage = state.error.response.data } - return { isLoading, data, error: errorMessage, refetch } + return { ...state, error: errorMessage } } diff --git a/src/store/store.ts b/src/store/store.ts index bfa78a51..167a8f63 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -2,7 +2,6 @@ import { create } from 'zustand' import { devtools, persist } from 'zustand/middleware' import { createUserSlice } from './userSlice/userSlice' import { createUserProfileSlice } from './userProfileSlice/userProfileSlice' -import { createUserFollowingSlice } from './userFollowingSlice/userFollowingSlice' import { createSocketSlice } from './socketSlice/socketSlice' import { storeType } from './storeType' @@ -12,7 +11,6 @@ export const useUniStore = create()( (...a) => ({ ...createUserSlice(...a), ...createUserProfileSlice(...a), - ...createUserFollowingSlice(...a), ...createSocketSlice(...a), }), { diff --git a/src/store/storeType.ts b/src/store/storeType.ts index 058d3a0e..0baa2b8a 100644 --- a/src/store/storeType.ts +++ b/src/store/storeType.ts @@ -1,6 +1,6 @@ import { userSlice } from './userSlice/userSlice' import { userProfileSlice } from './userProfileSlice/userProfileSlice' -import { userFollowingSlice } from './userFollowingSlice/userFollowingSlice' + import { SocketSlice } from './socketSlice/socketSlice' -export type storeType = userSlice & userProfileSlice & userFollowingSlice & SocketSlice +export type storeType = userSlice & userProfileSlice & SocketSlice diff --git a/src/store/userFollowingSlice/userFollowingSlice.ts b/src/store/userFollowingSlice/userFollowingSlice.ts deleted file mode 100644 index a3be876b..00000000 --- a/src/store/userFollowingSlice/userFollowingSlice.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { StateCreator } from 'zustand' -import { userFollowType } from './userFollowingType' - -type userFollowingState = { - userFollowingData: userFollowType -} - -type userFollowingAction = { - setUserFollowingData: (userProfileData: userFollowType) => void - resetUserFollowingData: () => void -} - -const initialState: userFollowingState = { - userFollowingData: { - followerCount: 0, - followingCount: 0, - }, -} - -export type userFollowingSlice = userFollowingState & userFollowingAction - -export const createUserFollowingSlice: StateCreator = (set) => ({ - userFollowingData: initialState.userFollowingData, - setUserFollowingData: (userFollowingData: userFollowType) => set({ userFollowingData }), - resetUserFollowingData: () => set(initialState), -}) diff --git a/src/store/userFollowingSlice/userFollowingType.ts b/src/store/userFollowingSlice/userFollowingType.ts deleted file mode 100644 index 9c2c1d60..00000000 --- a/src/store/userFollowingSlice/userFollowingType.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface userFollowType { - followerCount: number - followingCount: number -} diff --git a/src/types/constants.ts b/src/types/constants.ts index 490711d6..8be56163 100644 --- a/src/types/constants.ts +++ b/src/types/constants.ts @@ -112,3 +112,8 @@ interface ErrorResponse { } export type AxiosErrorType = AxiosError + +export enum singlePostEnum { + userPost = 'userPost', + CommunityPost = 'CommunityPost', +} 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) {