1
- import React , { useState } from 'react' ;
1
+ import React , { useState , useEffect } from 'react' ;
2
2
import PostCommentsWrite from '../../components/post/PostCommentsWrite' ;
3
3
import { WRITE_COMMENT , RELOAD_COMMENTS } from '../../lib/graphql/post' ;
4
4
import { useMutation , useQuery } from '@apollo/react-hooks' ;
5
+ import useUser from '../../lib/hooks/useUser' ;
6
+ import useRequireLogin from '../../lib/hooks/useRequireLogin' ;
7
+ import storage from '../../lib/storage' ;
5
8
6
9
export interface PostCommentsWriteContainerProps {
7
10
postId : string ;
@@ -11,6 +14,9 @@ export interface PostCommentsWriteContainerProps {
11
14
const PostCommentsWriteContainer : React . FC < PostCommentsWriteContainerProps > = ( {
12
15
postId,
13
16
} ) => {
17
+ const user = useUser ( ) ;
18
+ const requireLogin = useRequireLogin ( ) ;
19
+
14
20
const [ comment , setComment ] = useState ( '' ) ;
15
21
const onChange = ( e : React . ChangeEvent < HTMLTextAreaElement > ) => {
16
22
setComment ( e . target . value ) ;
@@ -24,7 +30,24 @@ const PostCommentsWriteContainer: React.FC<PostCommentsWriteContainerProps> = ({
24
30
} ,
25
31
} ) ;
26
32
33
+ useEffect ( ( ) => {
34
+ const key = `comment_before_login:${ postId } ` ;
35
+
36
+ const commentBeforeLogin = storage . getItem (
37
+ `comment_before_login:${ postId } ` ,
38
+ ) ;
39
+
40
+ if ( commentBeforeLogin ) {
41
+ setComment ( commentBeforeLogin ) ;
42
+ storage . removeItem ( key ) ;
43
+ }
44
+ } , [ postId ] ) ;
45
+
27
46
const onWrite = async ( ) => {
47
+ if ( ! user ) {
48
+ storage . setItem ( `comment_before_login:${ postId } ` , comment ) ;
49
+ return requireLogin ( ) ;
50
+ }
28
51
try {
29
52
await writeComment ( {
30
53
variables : {
0 commit comments