Skip to content

Commit cc9a8b4

Browse files
committed
Require login on comment
1 parent f932791 commit cc9a8b4

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/containers/post/PostCommentsWriteContainer.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import React, { useState } from 'react';
1+
import React, { useState, useEffect } from 'react';
22
import PostCommentsWrite from '../../components/post/PostCommentsWrite';
33
import { WRITE_COMMENT, RELOAD_COMMENTS } from '../../lib/graphql/post';
44
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';
58

69
export interface PostCommentsWriteContainerProps {
710
postId: string;
@@ -11,6 +14,9 @@ export interface PostCommentsWriteContainerProps {
1114
const PostCommentsWriteContainer: React.FC<PostCommentsWriteContainerProps> = ({
1215
postId,
1316
}) => {
17+
const user = useUser();
18+
const requireLogin = useRequireLogin();
19+
1420
const [comment, setComment] = useState('');
1521
const onChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
1622
setComment(e.target.value);
@@ -24,7 +30,24 @@ const PostCommentsWriteContainer: React.FC<PostCommentsWriteContainerProps> = ({
2430
},
2531
});
2632

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+
2746
const onWrite = async () => {
47+
if (!user) {
48+
storage.setItem(`comment_before_login:${postId}`, comment);
49+
return requireLogin();
50+
}
2851
try {
2952
await writeComment({
3053
variables: {

src/lib/hooks/useRequireLogin.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { useDispatch } from 'react-redux';
2+
import { useCallback } from 'react';
3+
import { showAuthModal } from '../../modules/core';
4+
5+
export default function useRequireLogin() {
6+
const dispatch = useDispatch();
7+
const requireLogin = useCallback(() => {
8+
dispatch(showAuthModal('LOGIN'));
9+
}, [dispatch]);
10+
return requireLogin;
11+
}

0 commit comments

Comments
 (0)