2838 |
stevensc |
1 |
import React, { useState } from 'react'
|
|
|
2 |
import { useDispatch, useSelector } from 'react-redux'
|
|
|
3 |
import { Box, Button, styled, Typography } from '@mui/material'
|
|
|
4 |
import { ChatOutlined, SendOutlined, ShareOutlined } from '@mui/icons-material'
|
|
|
5 |
|
|
|
6 |
import { axios } from '@app/utils'
|
|
|
7 |
import { feedTypes } from '@app/redux/feed/feed.types'
|
|
|
8 |
import { addNotification } from '@app/redux/notification/notification.actions'
|
|
|
9 |
import { openShareModal } from '@app/redux/share-modal/shareModal.actions'
|
|
|
10 |
import { shareModalTypes } from '@app/redux/share-modal/shareModal.types'
|
|
|
11 |
import withReactions from '@app/hocs/withReaction'
|
|
|
12 |
import withExternalShare from '../linkedin/withExternalShare'
|
|
|
13 |
|
|
|
14 |
import Widget from '@components/UI/Widget'
|
|
|
15 |
import CommentForm from '../linkedin/comments/comment-form'
|
|
|
16 |
import CommentsList from '../linkedin/comments/comment-list'
|
|
|
17 |
import FeedReactions from '../linkedin/feed/FeedReactions'
|
|
|
18 |
|
|
|
19 |
const Row = styled(Box)(({ theme }) => ({
|
|
|
20 |
display: 'flex',
|
|
|
21 |
padding: theme.spacing(0.5),
|
|
|
22 |
justifyContent: 'space-between',
|
|
|
23 |
alignItems: 'center',
|
|
|
24 |
gap: theme.spacing(0.5)
|
|
|
25 |
}))
|
|
|
26 |
|
|
|
27 |
export default function FeedActions({ id }) {
|
|
|
28 |
const {
|
|
|
29 |
comments,
|
|
|
30 |
owner_comments: totalComments,
|
|
|
31 |
feed_reactions_url: reactionsUrl,
|
|
|
32 |
feed_content_type: contentType,
|
|
|
33 |
owner_shared: totalShared,
|
|
|
34 |
feed_share_external_url: shareExternalUrl,
|
|
|
35 |
feed_save_reaction_url: saveReactionUrl,
|
|
|
36 |
feed_delete_reaction_url: deleteReactionUrl,
|
|
|
37 |
feed_increment_external_counter_url: incrementExternalCounterUrl,
|
|
|
38 |
feed_my_reaction: feedMyReaction,
|
|
|
39 |
feed_reactions: feedReactions,
|
|
|
40 |
owner_external_shared: ownerExternalShared,
|
|
|
41 |
feed_share_url: feedShareUrl,
|
|
|
42 |
feed_unique: feedUnique
|
|
|
43 |
} = useSelector(({ feed }) => feed.feeds.byId[id])
|
|
|
44 |
const labels = useSelector(({ intl }) => intl.labels)
|
|
|
45 |
const dispatch = useDispatch()
|
|
|
46 |
|
|
|
47 |
const [reaction, setReaction] = useState(feedMyReaction)
|
|
|
48 |
const [ownerReactions, setOwnerReactions] = useState(feedReactions)
|
|
|
49 |
const [externalShare, setExternalShare] = useState(ownerExternalShared)
|
|
|
50 |
const [showComments, setShowComments] = useState(false)
|
|
|
51 |
|
|
|
52 |
const setTotalShares = (value) => setExternalShare(value)
|
|
|
53 |
|
|
|
54 |
const toggleComments = () => setShowComments((prev) => !prev)
|
|
|
55 |
|
|
|
56 |
const updateReactions = ({ reactions }, currentReaction) => {
|
|
|
57 |
setOwnerReactions(reactions)
|
|
|
58 |
setReaction(currentReaction)
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
const shareFeed = () => {
|
|
|
62 |
dispatch(
|
|
|
63 |
openShareModal(
|
|
|
64 |
feedShareUrl,
|
|
|
65 |
shareModalTypes.SHARE,
|
|
|
66 |
feedTypes.DASHBOARD,
|
|
|
67 |
feedUnique
|
|
|
68 |
)
|
|
|
69 |
)
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
const addComment = (comment, comment_add_url) => {
|
|
|
73 |
const formData = new FormData()
|
|
|
74 |
formData.append('comment', comment)
|
|
|
75 |
|
|
|
76 |
axios.post(comment_add_url, formData).then((response) => {
|
|
|
77 |
const { success, data, total_comments } = response.data
|
|
|
78 |
|
|
|
79 |
if (!success) {
|
|
|
80 |
dispatch(
|
|
|
81 |
addNotification({
|
|
|
82 |
style: 'danger',
|
|
|
83 |
msg:
|
|
|
84 |
typeof data === 'string'
|
|
|
85 |
? data
|
|
|
86 |
: 'Error interno. Intente más tarde.'
|
|
|
87 |
})
|
|
|
88 |
)
|
|
|
89 |
return
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
dispatch(
|
|
|
93 |
addComment({
|
|
|
94 |
feedId: feedUnique,
|
|
|
95 |
comment: data,
|
|
|
96 |
totalComments: total_comments
|
|
|
97 |
})
|
|
|
98 |
)
|
|
|
99 |
})
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
const ExternalShareButton = withExternalShare(Button, shareExternalUrl)
|
|
|
103 |
const ReactionButton = withReactions(Button)
|
|
|
104 |
|
|
|
105 |
return (
|
|
|
106 |
<>
|
|
|
107 |
<Row>
|
|
|
108 |
<FeedReactions reactions={ownerReactions} reactionsUrl={reactionsUrl} />
|
|
|
109 |
|
|
|
110 |
<Row>
|
|
|
111 |
<Typography
|
|
|
112 |
variant='body2'
|
|
|
113 |
sx={{ display: totalComments ? 'inline-flex' : 'none' }}
|
|
|
114 |
>
|
|
|
115 |
{`${totalComments} ${labels.comments?.toLowerCase()}`}
|
|
|
116 |
</Typography>
|
|
|
117 |
|
|
|
118 |
<Typography
|
|
|
119 |
variant='body2'
|
|
|
120 |
sx={{ display: totalShared ? 'inline-flex' : 'none' }}
|
|
|
121 |
>
|
|
|
122 |
{`${totalShared} ${labels.shared?.toLowerCase()}`}
|
|
|
123 |
</Typography>
|
|
|
124 |
|
|
|
125 |
<Typography
|
|
|
126 |
variant='body2'
|
|
|
127 |
sx={{ display: externalShare ? 'inline-flex' : 'none' }}
|
|
|
128 |
>
|
|
|
129 |
{`${externalShare} ${labels.sends?.toLowerCase()}`}
|
|
|
130 |
</Typography>
|
|
|
131 |
</Row>
|
|
|
132 |
</Row>
|
|
|
133 |
<Widget.Actions
|
|
|
134 |
styles={{ display: contentType === 'fast-survey' ? 'none' : 'flex' }}
|
|
|
135 |
>
|
|
|
136 |
<ReactionButton
|
|
|
137 |
currentReactionType={reaction}
|
|
|
138 |
saveUrl={saveReactionUrl}
|
|
|
139 |
deleteUrl={deleteReactionUrl}
|
|
|
140 |
onReaction={updateReactions}
|
|
|
141 |
/>
|
|
|
142 |
<Button onClick={toggleComments}>
|
|
|
143 |
<ChatOutlined />
|
|
|
144 |
{labels.comment}
|
|
|
145 |
</Button>
|
|
|
146 |
<Button onClick={shareFeed}>
|
|
|
147 |
<ShareOutlined />
|
|
|
148 |
{labels.share}
|
|
|
149 |
</Button>
|
|
|
150 |
<ExternalShareButton
|
|
|
151 |
shorterUrl={incrementExternalCounterUrl}
|
|
|
152 |
setValue={setTotalShares}
|
|
|
153 |
>
|
|
|
154 |
<SendOutlined />
|
|
|
155 |
{labels.send}
|
|
|
156 |
</ExternalShareButton>
|
|
|
157 |
</Widget.Actions>
|
|
|
158 |
<Box
|
|
|
159 |
sx={{
|
|
|
160 |
display: showComments ? 'block' : 'none'
|
|
|
161 |
}}
|
|
|
162 |
>
|
|
|
163 |
<CommentForm onSubmit={addComment} />
|
|
|
164 |
<CommentsList comments={comments} />
|
|
|
165 |
</Box>
|
|
|
166 |
</>
|
|
|
167 |
)
|
|
|
168 |
}
|