Skip to content

Commit 022cc48

Browse files
authored
merge: 로그인 제한 완화
refactor: 로그인 제한 완화
2 parents 7122304 + d528b1a commit 022cc48

13 files changed

Lines changed: 12179 additions & 12597 deletions

File tree

src/components/Header/index.jsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ export const Header = ({ prev, title, info, complete, onComplete }) => {
6868
<InnerRight>
6969
{info && (
7070
<>
71-
<Badge dot={isSeen}>
72-
<Icon.Link to="/user/notification" name="NOTIFICATION" size={30} />
73-
</Badge>
71+
{token && (
72+
<Badge dot={isSeen}>
73+
<Icon.Link to="/user/notification" name="NOTIFICATION" size={30} />
74+
</Badge>
75+
)}
7476
<Icon.Link to="/user/myinfo" name="MY_INFO" size={30} />
7577
</>
7678
)}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Modal } from 'components';
2+
import { useCallback } from 'react';
3+
import { useNavigate } from 'react-router-dom';
4+
import theme from 'styles/theme';
5+
6+
const MODAL_TITLE_LOGIN_REQUIRED = '로그인이 필요한 서비스입니다.';
7+
const MODAL_DESCRIPTION_LOGIN_REQUIRED = '로그인 화면으로 이동하시겠어요?';
8+
9+
const LoginRequireModal = ({ visible, onClose }) => {
10+
const navigate = useNavigate();
11+
12+
const handleClick = useCallback(() => {
13+
navigate('/login', { replace: true });
14+
}, [navigate]);
15+
16+
return (
17+
<Modal visible={visible} onClose={onClose}>
18+
<Modal.Content
19+
title={MODAL_TITLE_LOGIN_REQUIRED}
20+
description={MODAL_DESCRIPTION_LOGIN_REQUIRED}
21+
/>
22+
<Modal.Button onClick={handleClick}></Modal.Button>
23+
<Modal.Button onClick={onClose} style={modalButtonStyle}>
24+
아니오
25+
</Modal.Button>
26+
</Modal>
27+
);
28+
};
29+
30+
const modalButtonStyle = {
31+
color: theme.color.fontBlack,
32+
backgroundColor: theme.color.backgroundNormal,
33+
};
34+
35+
export default LoginRequireModal;

src/hooks/useScrollToTop.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/pages/MainPage/IconButton.jsx

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/pages/MainPage/PostBody.jsx

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
/** @jsxImportSource @emotion/react */
2-
import { jsx, css } from '@emotion/react';
1+
import { css } from '@emotion/react';
32
import { useCallback, useState, useEffect } from 'react';
43
import { useNavigate } from 'react-router-dom';
54
import styled from '@emotion/styled';
@@ -13,6 +12,9 @@ import { useUserContext } from 'contexts/UserContext';
1312
import { IMAGE_URLS } from 'utils/constants/images';
1413
import displayedAt from 'utils/functions/displayedAt';
1514
import IconButton from 'components/basic/Icon/IconButton';
15+
import { COMMENT, HEART, HEART_RED } from 'utils/constants/icons/names';
16+
import { LIKE } from 'utils/constants/notificationTypes';
17+
import LoginRequireModal from 'components/Modal/customs/LoginRequireModal';
1618

1719
const PostBody = ({ index, post, isDetailPage = false }) => {
1820
const { _id: postId, image, likes, comments, createdAt, author } = post || {};
@@ -21,7 +23,8 @@ const PostBody = ({ index, post, isDetailPage = false }) => {
2123
const [heartCount, setHeartCount] = useState(likes.length);
2224
const [likeId, setLikeId] = useState('');
2325
const [isShown, setIsShown] = useState(false);
24-
const [localToken] = useLocalToken();
26+
const [modalOn, setModalOn] = useState(false);
27+
const [token] = useLocalToken();
2528
const { currentUser } = useUserContext();
2629
const [, setCurrentPostIndex] = useScrollPosition();
2730
const navigate = useNavigate();
@@ -55,24 +58,29 @@ const PostBody = ({ index, post, isDetailPage = false }) => {
5558
}, [setCurrentPostIndex, index, postId, post, isDetailPage, navigate]);
5659

5760
const handleClickHeart = useCallback(async () => {
61+
if (!token) {
62+
setModalOn(true);
63+
return;
64+
}
65+
5866
setOnHeart(!onHeart);
5967
if (!onHeart) {
6068
setHeartCount(heartCount + 1);
61-
if (localToken && postId) {
62-
const like = await setLike(localToken, postId).then((res) => res.data);
69+
if (token && postId) {
70+
const like = await setLike(token, postId).then((res) => res.data);
6371
setLikeId(like._id);
6472
if (currentUser.id !== author._id) {
65-
await setNotification(localToken, 'LIKE', like._id, author._id, postId);
73+
await setNotification(token, LIKE, like._id, author._id, postId);
6674
}
6775
}
6876
} else {
6977
setHeartCount(heartCount - 1);
70-
if (localToken && likeId) {
71-
await setDisLike(localToken, likeId).then((res) => res.data);
78+
if (token && likeId) {
79+
await setDisLike(token, likeId).then((res) => res.data);
7280
setLikeId('');
7381
}
7482
}
75-
}, [onHeart, heartCount, postId, likeId, author._id, currentUser, localToken]);
83+
}, [onHeart, heartCount, postId, likeId, author._id, currentUser, token]);
7684

7785
const handleClickTag = useCallback(
7886
(tag) => {
@@ -86,9 +94,13 @@ const PostBody = ({ index, post, isDetailPage = false }) => {
8694
[index, setCurrentPostIndex, navigate],
8795
);
8896

89-
const handleClickMore = () => {
97+
const handleClickMore = useCallback(() => {
9098
setIsShown(true);
91-
};
99+
}, []);
100+
101+
const handleCloseModal = useCallback(() => {
102+
setModalOn(false);
103+
}, []);
92104

93105
return (
94106
<Container>
@@ -103,7 +115,7 @@ const PostBody = ({ index, post, isDetailPage = false }) => {
103115
<Contents>
104116
<IconButtons>
105117
<IconButton
106-
name={onHeart ? 'HEART_RED' : 'HEART'} // Todo: 상수화
118+
name={onHeart ? HEART_RED : HEART}
107119
size={22}
108120
style={IconButtonStyle}
109121
onClick={handleClickHeart}
@@ -112,7 +124,7 @@ const PostBody = ({ index, post, isDetailPage = false }) => {
112124
{heartCount}
113125
</Text>
114126
</IconButton>
115-
<IconButton name="COMMENT" size={22} onClick={navigateToDetailPage}>
127+
<IconButton name={COMMENT} size={22} onClick={navigateToDetailPage}>
116128
<Text fontSize={16} style={IconButtonTextStyle}>
117129
{comments.length}
118130
</Text>
@@ -135,6 +147,7 @@ const PostBody = ({ index, post, isDetailPage = false }) => {
135147
</Tags>
136148
<DateText>{displayedAt(createdAt)}</DateText>
137149
</Contents>
150+
<LoginRequireModal visible={modalOn} onClose={handleCloseModal} />
138151
</Container>
139152
);
140153
};

src/pages/MainPage/PostHeader.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useUserContext } from 'contexts/UserContext';
66
import theme from 'styles/theme';
77
import useLocalToken from 'hooks/useLocalToken';
88
import IconButton from 'components/basic/Icon/IconButton';
9+
import { MORE } from 'utils/constants/icons/names';
910

1011
const PostHeader = ({ post, isDetailPage }) => {
1112
const {
@@ -60,7 +61,7 @@ const PostHeader = ({ post, isDetailPage }) => {
6061
<Profile src={image} userName={fullName} onClick={handleClickProfile} />
6162
{isMyPost && (
6263
<>
63-
<IconButton name="MORE" size={20} onClick={handleClickMore} />
64+
<IconButton name={MORE} size={20} onClick={handleClickMore} />
6465
<Modal visible={isModal} onClose={onClose}>
6566
<Modal.Custom>
6667
<Buttons>

src/pages/MainPage/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const MainPage = () => {
2424
}).then((res) => res.data);
2525
setPosts(nextPosts);
2626
setMax(nextPosts[0].channel.posts.length);
27-
setOffset(prevPostIndex ? prevPostIndex : 5);
27+
setOffset(prevPostIndex ? prevPostIndex : LIMIT);
2828
})();
2929
}, []);
3030

src/pages/PostDetailPage/index.jsx

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import styled from '@emotion/styled';
22
import PostItem from 'pages/MainPage/PostItem';
3-
import { Avatar, Icon, Modal, PageWrapper } from 'components';
3+
import { Avatar, Modal, PageWrapper } from 'components';
44
import IconButton from 'components/basic/Icon/IconButton';
55
import theme from 'styles/theme';
66
import { useRef, useCallback, useState, useEffect } from 'react';
@@ -10,6 +10,9 @@ import { getPostData } from 'utils/apis/postApi';
1010
import { useUserContext } from 'contexts/UserContext';
1111
import { setNotification } from 'utils/apis/userApi';
1212
import displayedAt from 'utils/functions/displayedAt';
13+
import { MORE } from 'utils/constants/icons/names';
14+
import { COMMENT } from 'utils/constants/notificationTypes';
15+
import LoginRequireModal from 'components/Modal/customs/LoginRequireModal';
1316

1417
const PostDetailPage = () => {
1518
const navigate = useNavigate();
@@ -19,7 +22,8 @@ const PostDetailPage = () => {
1922
const inputRef = useRef(null);
2023
const [localToken] = useLocalToken();
2124
const { currentUser, onAddComment, onDeleteComment } = useUserContext();
22-
const [isModal, setIsModal] = useState(false);
25+
const [loginModalOn, setLoginModalOn] = useState(false);
26+
const [commentModalOn, setCommentModalOn] = useState(false);
2327
const commentIdToDelete = useRef('');
2428

2529
useEffect(() => {
@@ -33,6 +37,10 @@ const PostDetailPage = () => {
3337
const handleSubmit = useCallback(
3438
async (e) => {
3539
e.preventDefault();
40+
if (!localToken) {
41+
setLoginModalOn(true);
42+
return;
43+
}
3644
if (!inputRef.current.value) {
3745
return;
3846
}
@@ -44,7 +52,7 @@ const PostDetailPage = () => {
4452
setInputHeight('30px');
4553
inputRef.current.value = '';
4654
if (currentUser.id !== post.author._id) {
47-
await setNotification(localToken, 'COMMENT', newComment._id, post.author._id, post._id);
55+
await setNotification(localToken, COMMENT, newComment._id, post.author._id, post._id);
4856
}
4957
},
5058
[post, localToken, currentUser.id, onAddComment],
@@ -67,12 +75,12 @@ const PostDetailPage = () => {
6775
);
6876

6977
const handleMoreClick = useCallback((commentId) => {
70-
setIsModal(true);
78+
setCommentModalOn(true);
7179
commentIdToDelete.current = commentId;
7280
}, []);
7381

7482
const handleDeleteComment = useCallback(async () => {
75-
setIsModal(false);
83+
setCommentModalOn(false);
7684
if (commentIdToDelete.current) {
7785
await onDeleteComment(commentIdToDelete.current);
7886
const nextComments = post.comments.filter(
@@ -85,8 +93,9 @@ const PostDetailPage = () => {
8593
}
8694
}, [post, onDeleteComment]);
8795

88-
const onClose = useCallback(() => {
89-
setIsModal(false);
96+
const handleCloseModal = useCallback(() => {
97+
setLoginModalOn(false);
98+
setCommentModalOn(false);
9099
}, []);
91100

92101
return (
@@ -118,7 +127,7 @@ const PostDetailPage = () => {
118127
</Content>
119128
{_id === currentUser.id && (
120129
<IconButton
121-
name="MORE"
130+
name={MORE}
122131
size={20}
123132
style={MoreButtonStyle}
124133
onClick={() => handleMoreClick(commentId)}
@@ -129,11 +138,14 @@ const PostDetailPage = () => {
129138
.reverse()}
130139
</CommentList>
131140
</Container>
132-
<Modal visible={isModal} onClose={onClose}>
133-
<Modal.Custom>
134-
<DeleteCommentButton onClick={handleDeleteComment}>삭제</DeleteCommentButton>
135-
</Modal.Custom>
136-
</Modal>
141+
{commentModalOn && (
142+
<Modal visible={commentModalOn} onClose={handleCloseModal}>
143+
<Modal.Custom>
144+
<DeleteCommentButton onClick={handleDeleteComment}>삭제</DeleteCommentButton>
145+
</Modal.Custom>
146+
</Modal>
147+
)}
148+
{loginModalOn && <LoginRequireModal visible={loginModalOn} onClose={handleCloseModal} />}
137149
</PageWrapper>
138150
)}
139151
</>

src/routes/PrivateWrapper.jsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import useLocalToken from 'hooks/useLocalToken';
2+
import { useNavigate, Outlet } from 'react-router-dom';
3+
import LoginRequireModal from 'components/Modal/customs/LoginRequireModal';
4+
import { useCallback } from 'react';
5+
6+
const PrivateWrapper = () => {
7+
const [token] = useLocalToken();
8+
const navigate = useNavigate();
9+
10+
const handleCloseModal = useCallback(() => {
11+
navigate(-1);
12+
}, [navigate]);
13+
14+
return token ? (
15+
<Outlet />
16+
) : (
17+
<>
18+
<Outlet />
19+
<LoginRequireModal visible={true} onClose={handleCloseModal} />
20+
</>
21+
);
22+
};
23+
export default PrivateWrapper;

0 commit comments

Comments
 (0)