@@ -17,6 +17,7 @@ import { setHeadingId } from '../../lib/heading';
17
17
import { useHistory } from 'react-router' ;
18
18
import { toast } from 'react-toastify' ;
19
19
import { useUncachedApolloClient } from '../../lib/graphql/UncachedApolloContext' ;
20
+ import useTurnstile from '../../lib/hooks/useTurnstile' ;
20
21
21
22
type PublishActionButtonsContainerProps = { } ;
22
23
@@ -25,6 +26,10 @@ const PublishActionButtonsContainer: React.FC<
25
26
> = ( ) => {
26
27
const history = useHistory ( ) ;
27
28
const client = useApolloClient ( ) ;
29
+ const user = useSelector ( ( state : RootState ) => state . core . user ) ;
30
+
31
+ const isTurnstileEnabled = ! ! user && ! user . is_trusted ;
32
+ const { isLoading, token } = useTurnstile ( isTurnstileEnabled ) ;
28
33
29
34
const options = useSelector ( ( state : RootState ) =>
30
35
pick (
@@ -54,12 +59,16 @@ const PublishActionButtonsContainer: React.FC<
54
59
55
60
const uncachedClient = useUncachedApolloClient ( ) ;
56
61
57
- const [ writePost ] = useMutation < WritePostResponse > ( WRITE_POST , {
58
- client : uncachedClient ,
59
- } ) ;
60
- const [ editPost ] = useMutation < EditPostResult > ( EDIT_POST , {
61
- client : uncachedClient ,
62
- } ) ;
62
+ const [ writePost , { loading : writePostLoading } ] =
63
+ useMutation < WritePostResponse > ( WRITE_POST , {
64
+ client : uncachedClient ,
65
+ } ) ;
66
+ const [ editPost , { loading : editPostLoading } ] = useMutation < EditPostResult > (
67
+ EDIT_POST ,
68
+ {
69
+ client : uncachedClient ,
70
+ } ,
71
+ ) ;
63
72
64
73
const variables = {
65
74
title : options . title ,
@@ -77,44 +86,78 @@ const PublishActionButtonsContainer: React.FC<
77
86
short_description : options . description ,
78
87
} ,
79
88
series_id : safe ( ( ) => options . selectedSeries ! . id ) ,
89
+ token,
80
90
} ;
81
91
82
92
const onPublish = async ( ) => {
93
+ if ( writePostLoading ) {
94
+ toast . info ( '포스트 작성 중입니다.' ) ;
95
+ return ;
96
+ }
97
+
83
98
if ( options . title . trim ( ) === '' ) {
84
99
toast . error ( '제목이 비어있습니다.' ) ;
85
100
return ;
86
101
}
102
+
87
103
try {
88
104
const response = await writePost ( {
89
105
variables : variables ,
90
106
} ) ;
91
- if ( ! response || ! response . data ) return ;
107
+
108
+ if ( ! response . data ?. writePost ) {
109
+ toast . error ( '포스트 작성 실패' ) ;
110
+ return ;
111
+ }
112
+
92
113
const { user, url_slug } = response . data . writePost ;
93
114
await client . resetStore ( ) ;
94
115
history . push ( `/@${ user . username } /${ url_slug } ` ) ;
95
- } catch ( e ) {
116
+ } catch ( error ) {
117
+ console . log ( 'write post failed' , error ) ;
96
118
toast . error ( '포스트 작성 실패' ) ;
97
119
}
98
120
} ;
99
121
100
122
const onEdit = async ( ) => {
101
- const response = await editPost ( {
102
- variables : {
103
- id : options . postId ,
104
- ...variables ,
105
- } ,
106
- } ) ;
107
- if ( ! response || ! response . data ) return ;
108
- const { user, url_slug } = response . data . editPost ;
109
- await client . resetStore ( ) ;
110
- history . push ( `/@${ user . username } /${ url_slug } ` ) ;
123
+ if ( editPostLoading ) {
124
+ toast . info ( '포스트 수정 중입니다.' ) ;
125
+ return ;
126
+ }
127
+
128
+ if ( options . title . trim ( ) === '' ) {
129
+ toast . error ( '제목이 비어있습니다.' ) ;
130
+ return ;
131
+ }
132
+
133
+ try {
134
+ const response = await editPost ( {
135
+ variables : {
136
+ id : options . postId ,
137
+ ...variables ,
138
+ } ,
139
+ } ) ;
140
+
141
+ if ( ! response . data ?. editPost ) {
142
+ toast . error ( '포스트 수정 실패' ) ;
143
+ return ;
144
+ }
145
+
146
+ const { user, url_slug } = response . data . editPost ;
147
+ await client . resetStore ( ) ;
148
+ history . push ( `/@${ user . username } /${ url_slug } ` ) ;
149
+ } catch ( error ) {
150
+ console . log ( 'edit post failed' , error ) ;
151
+ toast . error ( '포스트 수정 실패' ) ;
152
+ }
111
153
} ;
112
154
113
155
return (
114
156
< PublishActionButtons
115
157
onCancel = { onCancel }
116
158
onPublish = { options . postId ? onEdit : onPublish }
117
159
edit = { ! ! options . postId && ! options . isTemp }
160
+ isLoading = { isLoading }
118
161
/>
119
162
) ;
120
163
} ;
0 commit comments