Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed:secondary-Navbar data-reset in community-group #81

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Navbar from '../components/Navbar/Navbar'
import { ReactQueryClientProvider } from '@/utils/Provider'
import ZustandSocketProvider from '@/utils/ZustandSocketProvider'

import SecNavbar from '@/components/Timeline/Navbar'
import SecondaryNavbar from '@/components/Timeline/Navbar'
type FontClassName = string

const inter = Inter({ subsets: ['latin'] }) as { className: FontClassName }
Expand Down Expand Up @@ -39,7 +39,7 @@ export default function RootLayout({ children }: { children: React.ReactNode })
<ReactQueryClientProvider>
<ZustandSocketProvider>
<Navbar />
<SecNavbar />
<SecondaryNavbar />
{children}
</ZustandSocketProvider>
</ReactQueryClientProvider>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Connections/FindPeople.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ const FindPeople = ({ contentDivStyle }: { contentDivStyle?: string }) => {
<UserListItemSkeleton />
<UserListItemSkeleton />
</>
) : content === 'Followers' && !userFollow?.profile?.length ? (
) : content === 'Followers' && !userFollowers?.profile?.length ? (
<p className="text-center p-4">You have 0 Followers</p>
) : (
content === 'Followers' &&
userFollowers?.profile?.map((item: FollowingItemProps, index: number) => (
<UserListItem
key={index}
key={'Followers' + index}
id={item?.users_id?.id}
firstName={item?.users_id?.firstName}
lastName={item?.users_id?.lastName}
Expand Down
8 changes: 7 additions & 1 deletion src/components/Navbar/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const Navbar: React.FC = () => {
const [, , deleteCookie] = useCookie('uni_user_token')
const { userProfileData, userData, resetUserData, resetUserProfileData } = useUniStore()
const router = useRouter()

const { refetch: refetchNotification } = useGetNotification()
const { data: notificationData } = useGetNotification()

const [isNotificationOpen, setIsNotificationOpen] = useState(false)
Expand All @@ -54,6 +54,12 @@ const Navbar: React.FC = () => {
setIsLogin(!!userData?.id)
}, [userData, userData?.id])

useEffect(() => {
if (isLogin) {
refetchNotification()
}
}, [isLogin])

const handleClick = (item: string) => {
setActiveItem(item)
}
Expand Down
16 changes: 10 additions & 6 deletions src/components/Timeline/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
'use client'
import React from 'react'
import React, { useEffect, useState } 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 SecondaryNavbar: React.FC = () => {
// const tabs = ['Timeline', 'Profile', 'Notifications', 'Messages', 'Connections', 'University Community', 'Chatbot']
const router = useRouter()
const { userData } = useUniStore()
const id = userData?.id
const path = usePathname()
const activeTab = path.split('/').pop()
const [cookieValue] = useCookie('uni_user_token')

if (!cookieValue) {
const [isLogin, setIsLogin] = useState<boolean | undefined>(false)

useEffect(() => {
setIsLogin(!!userData?.id)
}, [userData, userData?.id])

if (!isLogin) {
return
}
return (
Expand All @@ -40,4 +44,4 @@ const Navbar: React.FC = () => {
)
}

export default Navbar
export default SecondaryNavbar
2 changes: 1 addition & 1 deletion src/services/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function getUsersWithProfile(token: string, Name: string) {
return response
}
export async function getUserFollow(token: string, Name: string) {
const response: FollowingItemPropss = await client(`/userprofile?name=${Name}`, { headers: { Authorization: `Bearer ${token}` } })
const response: FollowingItemPropss = await client(`/userprofile/following?name=${Name}`, { headers: { Authorization: `Bearer ${token}` } })
return response
}
export async function getUserFollowers(token: string, Name: string) {
Expand Down
11 changes: 8 additions & 3 deletions src/services/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ export async function UpdateCommunityGroup(data: { id: string }, token: string)
}

export function useGetNotification() {
const [cookieValue] = useCookie('uni_user_token')
let finalCookie: any = null

if (typeof document !== 'undefined') {
const cookieValue = document.cookie.split('; ').find((row) => row.startsWith('uni_user_token='))
finalCookie = cookieValue ? cookieValue.split('=')[1] : null
}

const state = useQuery({
queryKey: ['notification'],
queryFn: () => getUserNotification(cookieValue),
enabled: !!cookieValue,
queryFn: () => getUserNotification(finalCookie),
enabled: !!finalCookie,
})

let errorMessage = null
Expand Down
2 changes: 1 addition & 1 deletion src/store/socketSlice/socketSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const createSocketSlice: StateCreator<SocketSlice> = (set, get) => ({
...initialState,

initializeSocket: (userId, refetchUserData, refetchNotification, refetchUserProfileData) => {
const newSocket = io('http://localhost:9000')
const newSocket = io('http://localhost:8000')

newSocket.on('connect', () => {
console.log('Connected to the server')
Expand Down
36 changes: 21 additions & 15 deletions src/utils/ZustandSocketProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ const ZustandSocketProvider: React.FC<ZustandSocketProviderProps> = ({ children
const disconnectSocket = useUniStore((state) => state.disconnectSocket)
const { userData, type, setUserUnVerifiedCommunities, setUserVerifiedCommunities, setUserFollowers, setIsRefetched } = useUniStore()
const { refetch: refetchNotification } = useGetNotification()
const { refetch: refetchUserData, data: RefetcheduserData } = useGetUserData(type)
const { refetch: refetchUserProfileData, data: RefetcheduserProfileData } = useGetUserProfileData(type)

const { refetch: refetchUserData, data: RefetcheduserData, isSuccess: refectUserDataIsSuccess, isFetching } = useGetUserData(type)
const {
refetch: refetchUserProfileData,
data: RefetcheduserProfileData,
isSuccess: refectUserProfileDataIsSuccess,
isFetching: userProfileRefething,
} = useGetUserProfileData(type)
useEffect(() => {
if (userData.id) {
initializeSocket(userData.id, refetchUserData, refetchNotification, refetchUserProfileData)
Expand All @@ -30,18 +34,20 @@ const ZustandSocketProvider: React.FC<ZustandSocketProviderProps> = ({ children
}, [userData.id, initializeSocket, disconnectSocket, refetchNotification])

useEffect(() => {
switch (type) {
case notificationRoleAccess.ASSIGN:
setUserUnVerifiedCommunities(RefetcheduserData?.user?.userUnVerifiedCommunities)
setUserVerifiedCommunities(RefetcheduserData?.user?.userVerifiedCommunities)
setIsRefetched('')
break
case notificationRoleAccess.FOLLOW:
setUserFollowers(RefetcheduserProfileData?.profile?.followers)
setIsRefetched('')
break
default:
break
if ((refectUserDataIsSuccess && !isFetching) || (refectUserProfileDataIsSuccess && !userProfileRefething)) {
switch (type) {
case notificationRoleAccess.ASSIGN:
setUserUnVerifiedCommunities(RefetcheduserData?.user?.userUnVerifiedCommunities)
setUserVerifiedCommunities(RefetcheduserData?.user?.userVerifiedCommunities)
setIsRefetched('')
break
case notificationRoleAccess.FOLLOW:
setUserFollowers(RefetcheduserProfileData?.profile?.followers)
setIsRefetched('')
break
default:
break
}
}
}, [RefetcheduserData, RefetcheduserProfileData])

Expand Down
Loading