Skip to content

Commit fa17167

Browse files
authored
Сделал модальное окно с просьбой авторизоваться при попытке поставить лайк на комментарий без авторизации (#321)
* Если не зарегистрированный пользователь попытается лайкнуть комментарий, выведится окно, с просьбой авторизоваться * Исправил расположение и иправил баг с повторным открыванием поповеров * Исправил ошибку в тексте поповера * Провел рефакторинг кода * Переименовал стейт
1 parent 71e6f73 commit fa17167

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,28 @@ import React from 'react';
22
import { Link } from 'react-router-dom';
33
import { useAuth } from 'common/context/Auth/useAuth';
44
import styled from 'styled-components';
5+
import PropTypes from 'prop-types';
56

67
const StyledPopoverBlock = styled.div`
78
width: 250px;
89
text-align: center;
910
`;
1011

11-
const FavoritePopoverContent = () => {
12+
const FavoritePopoverContent = ({ text }) => {
1213
const { executeLoggingInProcess } = useAuth();
1314

1415
return (
1516
<StyledPopoverBlock>
1617
<Link to="/" onClick={executeLoggingInProcess}>
1718
Авторизуйся,
1819
</Link>{' '}
19-
чтобы иметь возможность сохранять вопросы
20+
{text}
2021
</StyledPopoverBlock>
2122
);
2223
};
2324

2425
export default FavoritePopoverContent;
26+
27+
FavoritePopoverContent.propTypes = {
28+
text: PropTypes.node.isRequired,
29+
};

src/features/comments/CommentView.jsx

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { Viewer } from '@toast-ui/react-editor';
22
import { useAuth } from 'common/context/Auth/useAuth';
33
import PropTypes from 'prop-types';
44
import styled from 'styled-components';
5-
import React from 'react';
5+
import React, { useState } from 'react';
66
import dayjs from 'dayjs';
77
import { HeartFilled, HeartOutlined } from '@ant-design/icons';
88
import { useDispatch, useSelector } from 'react-redux';
99
import { selectProfile } from 'features/profile/profileSlice';
10+
import FavoritePopoverContent from 'components/FavoritePopoverContent';
11+
import { Popover } from 'antd';
1012
import { CommentsActions } from './comment-actions/CommentsActions';
1113
import { likeCommentById, unlikeCommentById } from './commentsSlice';
1214

@@ -66,9 +68,20 @@ const StyledCommentActions = styled.div`
6668
display: flex;
6769
`;
6870

71+
const StyledPopoverBlock = styled.div`
72+
position: relative;
73+
`;
74+
75+
const StyledPopoverChildren = styled.div`
76+
position: absolute;
77+
right: 47px;
78+
`;
79+
6980
export const CommentView = ({ comment, lastComment }) => {
7081
const Wrapper = lastComment ? StyledWrapper : React.Fragment;
7182

83+
const [isAuthorizePopoverEnable, setIsAuthorizePopoverEnable] = useState(false);
84+
7285
const dispatch = useDispatch();
7386
const { token } = useAuth();
7487

@@ -80,13 +93,21 @@ export const CommentView = ({ comment, lastComment }) => {
8093
const userId = profile._id;
8194

8295
const handleToggleLike = () => {
83-
if (!commentLikes.includes(userId)) {
84-
dispatch(likeCommentById({ commentId, userId }));
96+
if (token) {
97+
if (!commentLikes.includes(userId)) {
98+
dispatch(likeCommentById({ commentId, userId }));
99+
} else {
100+
dispatch(unlikeCommentById({ commentId, userId }));
101+
}
85102
} else {
86-
dispatch(unlikeCommentById({ commentId, userId }));
103+
setIsAuthorizePopoverEnable(true);
87104
}
88105
};
89106

107+
const handleOpenPopover = () => {
108+
setIsAuthorizePopoverEnable(!isAuthorizePopoverEnable);
109+
};
110+
90111
return (
91112
<Wrapper>
92113
<div className="row align-items mb-4 g-1">
@@ -110,7 +131,6 @@ export const CommentView = ({ comment, lastComment }) => {
110131
className="d-flex align-items-center"
111132
type="button"
112133
onClick={handleToggleLike}
113-
disabled={!token}
114134
>
115135
{commentLikes.includes(userId) ? (
116136
<HeartFilled style={{ color: '#FF4646' }} />
@@ -119,6 +139,19 @@ export const CommentView = ({ comment, lastComment }) => {
119139
)}
120140
</button>
121141
<span>{commentLikes.length}</span>
142+
<StyledPopoverBlock>
143+
<StyledPopoverChildren>
144+
<Popover
145+
onOpenChange={handleOpenPopover}
146+
open={isAuthorizePopoverEnable}
147+
trigger="click"
148+
placement="bottomLeft"
149+
content={
150+
<FavoritePopoverContent text="чтобы иметь возможность лайкать комментарии" />
151+
}
152+
/>
153+
</StyledPopoverChildren>
154+
</StyledPopoverBlock>
122155
</StyledCommentLikes>
123156
)}
124157
</StyledCommentActions>

src/features/questions/questions-list/question-actions/FavoritePopover.jsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import { Popover } from 'antd';
22
import { useAuth } from 'common/context/Auth/useAuth';
33
import PropTypes from 'prop-types';
4-
import FavoritePopoverContent from './FavoritePopoverContent';
4+
import FavoritePopoverContent from 'components/FavoritePopoverContent';
55

66
export const FavoritePopover = ({ children }) => {
77
const { token } = useAuth();
88

99
if (!token) {
1010
return (
11-
<Popover placement="bottomLeft" trigger="click" content={FavoritePopoverContent}>
11+
<Popover
12+
placement="bottomLeft"
13+
trigger="click"
14+
content={<FavoritePopoverContent text="чтобы иметь возможность сохранять вопросы" />}
15+
>
1216
{children}
1317
</Popover>
1418
);

0 commit comments

Comments
 (0)