1
1
import { useNavigate , useParams } from 'react-router-dom' ;
2
2
3
+ import { useQueryClient } from '@tanstack/react-query' ;
4
+
3
5
import { Avatar } from '@components/Avatar' ;
4
6
import { Header } from '@components/Header' ;
5
7
import { Button } from '@components/shared/Button' ;
6
8
import { Flex } from '@components/shared/Flex' ;
7
9
import { Image } from '@components/shared/Image' ;
8
10
import { Text } from '@components/shared/Text' ;
9
11
10
- import { useMatchQuery } from '@hooks/queries/useMatchQuery' ;
12
+ import { useGameParticipateCreateMutation } from '@hooks/mutations/useGameParticipateCreateMutation' ;
13
+ import { useGameDetailQuery } from '@hooks/queries/useGameDetailQuery' ;
11
14
12
15
import { theme } from '@styles/theme' ;
13
16
17
+ import { Member } from '@type/models' ;
18
+
14
19
import { PATH_NAME } from '@consts/pathName' ;
15
20
import { WEEKDAY } from '@consts/weekday' ;
16
21
@@ -30,21 +35,39 @@ import {
30
35
UserDataWrapper ,
31
36
} from './GamesDetailPage.styles' ;
32
37
38
+ const getMyInfo = ( ) : Member | null => {
39
+ const json = localStorage . getItem ( 'USER_INFO' ) ;
40
+ if ( ! json ) {
41
+ return null ;
42
+ }
43
+ return JSON . parse ( json ) ;
44
+ } ;
45
+
33
46
export const GamesDetailPage = ( ) => {
34
47
const { id } = useParams ( ) ;
35
48
if ( id === undefined ) {
36
49
throw new Error ( '"match id" is undefined' ) ;
37
50
}
51
+ const gameId = Number ( id ) ;
38
52
39
- const { data : match } = useMatchQuery ( id ) ;
53
+ const navigate = useNavigate ( ) ;
54
+ const queryClient = useQueryClient ( ) ;
55
+ const { data : match } = useGameDetailQuery ( gameId ) ;
56
+ const myInfo = getMyInfo ( ) ;
57
+ const isMyMatch = match . host . id === myInfo ?. id ;
58
+
59
+ const { mutate : participateMutate } = useGameParticipateCreateMutation ( ) ;
60
+ const onParticipateSuccess = ( ) => {
61
+ queryClient . invalidateQueries ( {
62
+ queryKey : [ 'game-detail' , gameId ] ,
63
+ } ) ;
64
+ } ;
40
65
41
66
const [ year , month , day ] = match . playDate . split ( '-' ) ;
42
67
const [ hour , min ] = match . playStartTime . split ( ':' ) ;
43
68
const date = new Date ( Number ( year ) , Number ( month ) - 1 , Number ( day ) ) ;
44
69
const weekday = WEEKDAY [ date . getDay ( ) ] ;
45
70
46
- const navigate = useNavigate ( ) ;
47
-
48
71
const handleClickMemberProfile = ( id : number | string ) =>
49
72
navigate ( PATH_NAME . GET_PROFILE_PATH ( String ( id ) ) ) ;
50
73
@@ -83,19 +106,20 @@ export const GamesDetailPage = () => {
83
106
< Text size = { 16 } > { match . host . nickname } </ Text >
84
107
</ Flex >
85
108
</ Flex >
86
- { /* TODO: 본인일 시 버튼 렌더링 안하게 */ }
87
109
{ /* TODO: 버튼 클릭 핸들러 */ }
88
- < Button
89
- fontWeight = { 500 }
90
- width = "80px"
91
- height = "40px"
92
- borderColor = { theme . PALETTE . GRAY_400 }
93
- backgroundColor = "white"
94
- textColor = { theme . PALETTE . GRAY_400 }
95
- onClick = { ( ) => { } }
96
- >
97
- 대화하기
98
- </ Button >
110
+ { myInfo && ! isMyMatch && (
111
+ < Button
112
+ fontWeight = { 500 }
113
+ width = "80px"
114
+ height = "40px"
115
+ borderColor = { theme . PALETTE . GRAY_400 }
116
+ backgroundColor = "white"
117
+ textColor = { theme . PALETTE . GRAY_400 }
118
+ onClick = { ( ) => { } }
119
+ >
120
+ 대화하기
121
+ </ Button >
122
+ ) }
99
123
</ UserDataWrapper >
100
124
< Text size = { 20 } weight = { 700 } >
101
125
경기 정보
@@ -164,13 +188,34 @@ export const GamesDetailPage = () => {
164
188
) ) }
165
189
</ Guests >
166
190
</ GuestsContainer >
167
- < Button
168
- { ...theme . BUTTON_PROPS . LARGE_RED_BUTTON_PROPS }
169
- height = "50px"
170
- // TODO: 클릭 핸들러
171
- >
172
- 참여 신청하기
173
- </ Button >
191
+ { myInfo && ! isMyMatch && (
192
+ < Button
193
+ { ...theme . BUTTON_PROPS . LARGE_RED_BUTTON_PROPS }
194
+ height = "50px"
195
+ onClick = { ( ) =>
196
+ participateMutate (
197
+ {
198
+ gameId,
199
+ payload : { memberId : myInfo . id } ,
200
+ } ,
201
+ { onSuccess : onParticipateSuccess }
202
+ )
203
+ }
204
+ >
205
+ 참여 신청하기
206
+ </ Button >
207
+ ) }
208
+ { myInfo && isMyMatch && (
209
+ < Button
210
+ { ...theme . BUTTON_PROPS . LARGE_RED_BUTTON_PROPS }
211
+ height = "50px"
212
+ onClick = { ( ) =>
213
+ navigate ( PATH_NAME . GET_GAMES_MANAGE_PATH ( String ( gameId ) ) )
214
+ }
215
+ >
216
+ 매치 관리
217
+ </ Button >
218
+ ) }
174
219
</ PageContent >
175
220
</ PageLayout >
176
221
) ;
0 commit comments