File tree 4 files changed +53
-25
lines changed
features/lobby/components
4 files changed +53
-25
lines changed Original file line number Diff line number Diff line change @@ -11,10 +11,20 @@ import {
11
11
} from '@/shad-cn-ui/components/ui/table' ;
12
12
import { useState } from 'react' ;
13
13
import { useNavigate } from 'react-router-dom' ;
14
+ import { useQuery } from '@tanstack/react-query' ;
15
+ import { fetchUserFn } from '@features/user' ;
14
16
15
17
export function Lobby ( ) {
16
18
const { chatRooms, isLoading, error } = useChatRoomList ( ) ;
17
19
const navigate = useNavigate ( ) ;
20
+ const { data } = useQuery ( {
21
+ queryKey : [ 'users' ] ,
22
+ queryFn : fetchUserFn ,
23
+ } ) ;
24
+ if ( data === null ) {
25
+ // If data is null, then the user is first logging in, redirecting to sign up page
26
+ navigate ( '/signup' ) ;
27
+ }
18
28
19
29
const [ isModalOpen , setIsModalOpen ] = useState < boolean > ( false ) ; // Modal State for Create Room
20
30
Original file line number Diff line number Diff line change 1
- import { signUpFn } from '@features/user' ;
1
+ import { fetchUserFn , signUpFn } from '@features/user' ;
2
+ import { queryClient } from '@lib/react-query' ;
2
3
import { useMutation } from '@tanstack/react-query' ;
3
4
import { useNavigate } from 'react-router-dom' ;
4
5
@@ -9,9 +10,19 @@ export const useSignUp = () => {
9
10
isLoading,
10
11
error,
11
12
} = useMutation ( signUpFn , {
12
- onSuccess : ( ) => {
13
- // Success actions
14
- navigate ( '/' ) ;
13
+ onSuccess : async ( ) => {
14
+ try {
15
+ // Fetch the latest user data after successful sign-up
16
+ const userData = await fetchUserFn ( ) ;
17
+
18
+ // Update the 'users' query data with the newly fetched user data
19
+ queryClient . setQueryData ( [ 'users' ] , userData ) ;
20
+
21
+ // Navigate to the desired route
22
+ navigate ( '/' ) ;
23
+ } catch ( error ) {
24
+ console . error ( 'Error during post-sign-up user fetch:' , error ) ;
25
+ }
15
26
} ,
16
27
onError : ( error ) => {
17
28
// Error actions
Original file line number Diff line number Diff line change 1
1
import { ChatArea , InputArea , MessageType } from '@features/chat' ;
2
- import { fetchUserFn } from '@features/user' ;
3
-
4
- import { useQuery } from '@tanstack/react-query' ;
5
2
import { useState } from 'react' ;
6
- import { useNavigate , useParams } from 'react-router-dom' ;
3
+ import { useParams } from 'react-router-dom' ;
7
4
import styles from './ChatRoom.module.scss' ;
8
5
9
6
export function ChatRoom ( ) {
10
7
const [ messages , setMessages ] = useState < MessageType [ ] > ( [ ] ) ;
11
- const { data } = useQuery ( {
12
- queryKey : [ 'users' ] ,
13
- queryFn : fetchUserFn ,
14
- } ) ;
8
+
15
9
const { id } = useParams ( ) ; // id is the chat room ID from the URL
16
10
const chatRoomId = id || '' ;
17
- const navigate = useNavigate ( ) ;
18
11
19
- if ( data === null ) {
20
- // If data is null, then the user is first logging in, redirecting to sign up page
21
- navigate ( '/signup' ) ;
22
- }
23
12
const handleNewMessage = ( text : string ) => {
24
13
const newMessage : MessageType = {
25
14
sender : 'sent' ,
Original file line number Diff line number Diff line change 1
- import { RouterProvider , createBrowserRouter } from 'react-router-dom' ;
1
+ import {
2
+ Navigate ,
3
+ Outlet ,
4
+ RouterProvider ,
5
+ createBrowserRouter ,
6
+ redirect ,
7
+ } from 'react-router-dom' ;
2
8
import { Root } from '@pages' ;
3
9
import { ChatRoom } from '@pages/ChatRoomPage' ;
4
10
import { SignUp } from '@pages/SignUpPage' ;
5
11
import { RenewLobby } from '@pages/RenewLobbyPage' ;
12
+ import { useUsers } from '@hooks' ;
13
+
14
+ const ProtectedRoutes = ( ) => {
15
+ const { data : userData , isLoading } = useUsers ( ) ;
16
+ if ( isLoading ) return null ;
17
+
18
+ return userData ? < Outlet /> : < Navigate to = "/signup" /> ;
19
+ } ;
6
20
7
21
export function Routes ( ) {
8
22
const router = createBrowserRouter ( [
@@ -11,14 +25,18 @@ export function Routes() {
11
25
element : < Root /> ,
12
26
children : [
13
27
{
14
- path : '/' ,
15
- element : < RenewLobby /> ,
28
+ element : < ProtectedRoutes /> , // Wrap the children with ProtectedRoute
29
+ children : [
30
+ {
31
+ index : true ,
32
+ element : < RenewLobby /> ,
33
+ } ,
34
+ {
35
+ path : 'chats/:id' ,
36
+ element : < ChatRoom /> ,
37
+ } ,
38
+ ] ,
16
39
} ,
17
- {
18
- path : '/chats/:id' ,
19
- element : < ChatRoom /> ,
20
- } ,
21
-
22
40
{
23
41
path : '/signup' ,
24
42
element : < SignUp /> ,
You can’t perform that action at this time.
0 commit comments