Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7118 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
4821 stevensc 1
import React, { useEffect, useState } from 'react'
5812 stevensc 2
import parse from 'html-react-parser'
4817 stevensc 3
import { axios } from '../../utils'
5812 stevensc 4
import { useDispatch } from 'react-redux'
5
import { setIntlLabels } from '../../redux/intl/intl.action'
5814 stevensc 6
import withReactions from '../components/feed/withReaction'
4817 stevensc 7
import { addNotification } from '../../redux/notification/notification.actions'
4816 stevensc 8
 
4817 stevensc 9
import HomeNews from '../components/home-section/HomeNews'
10
import InputOption from '../templates/linkedin/Feed/InputOption'
11
import withExternalShare from '../templates/linkedin/Feed/withExternalShare'
5812 stevensc 12
import FeedCommentSection from '../components/feed/feed-comment/FeedCommentSection'
3361 stevensc 13
 
4816 stevensc 14
import ChatOutlinedIcon from '@mui/icons-material/ChatOutlined'
15
import SendOutlinedIcon from '@mui/icons-material/SendOutlined'
4817 stevensc 16
import AccessTimeIcon from '@mui/icons-material/AccessTime'
5812 stevensc 17
import RecommendIcon from '@mui/icons-material/Recommend'
18
import FavoriteIcon from '@mui/icons-material/FavoriteTwoTone'
19
import VolunteerActivismIcon from '@mui/icons-material/VolunteerActivism'
20
import EmojiEmotionsIcon from '@mui/icons-material/EmojiEmotions'
21
import TungstenIcon from '@mui/icons-material/Tungsten'
3361 stevensc 22
 
4817 stevensc 23
import '../templates/linkedin/Feed/Feed.scss'
24
 
5617 stevensc 25
export default function PostView({ post, labels }) {
5118 stevensc 26
  const [externalShare, setExternalShare] = useState(post.total_share_external)
5812 stevensc 27
  const [ownerReactions, setOwnerReaction] = useState(post.reactions)
5818 stevensc 28
  const [currentReaction, setCurrentReaction] = useState(post.my_reaction)
5812 stevensc 29
  const [totalReactions, setTotalReactions] = useState(0)
5118 stevensc 30
  const [comments, setComments] = useState([])
31
  const [isReadMoreActive, setIsReadMoreActive] = useState(false)
32
  const [showComments, setShowComments] = useState(false)
5617 stevensc 33
  const dispatch = useDispatch()
3810 stevensc 34
 
5812 stevensc 35
  const reactionsOptions = [
36
    {
37
      type: 'r',
38
      icon: <RecommendIcon style={{ color: '#7405f9' }} />,
39
    },
40
    {
41
      type: 's',
42
      icon: <VolunteerActivismIcon style={{ color: '#6495ED' }} />,
43
    },
44
    {
45
      type: 'l',
46
      icon: <FavoriteIcon style={{ color: '#DF704D' }} />,
47
    },
48
    {
49
      type: 'i',
50
      icon: (
51
        <TungstenIcon
52
          style={{ color: '#F5BB5C', transform: 'rotate(180deg)' }}
53
        />
54
      ),
55
    },
56
    {
57
      type: 'f',
58
      icon: <EmojiEmotionsIcon style={{ color: '#FF7F50' }} />,
59
    },
60
  ]
61
 
5118 stevensc 62
  const readMoreHandler = () => setIsReadMoreActive(!isReadMoreActive)
4816 stevensc 63
 
5118 stevensc 64
  const displayCommentSection = () => {
65
    setShowComments(!showComments)
66
  }
4821 stevensc 67
 
5118 stevensc 68
  const getComments = async () => {
5607 stevensc 69
    await axios.get(post.comments_url).then(({ data: response }) => {
70
      if (!response.success) {
71
        addNotification({ style: 'danger', msg: response.data })
72
        return
73
      }
4818 stevensc 74
 
5607 stevensc 75
      setComments(response.data)
76
    })
5118 stevensc 77
  }
4816 stevensc 78
 
5118 stevensc 79
  const handleExternalShare = (value) => setExternalShare(value)
4816 stevensc 80
 
5607 stevensc 81
  const ExternalShareButton = withExternalShare(
82
    InputOption,
83
    post.share_external_url,
84
    {
85
      Icon: SendOutlinedIcon,
86
      color: 'gray',
87
      title: 'Enviar',
88
      shareUrl: post.share_increment_external_counter_url,
89
      setValue: handleExternalShare,
6018 stevensc 90
      withTitle: true,
5607 stevensc 91
    }
92
  )
4816 stevensc 93
 
5118 stevensc 94
  const htmlParsedText = (fullStringText) => {
95
    const fullText = parse(fullStringText)
96
    if (fullStringText.length > 500) {
97
      const shortenedString = fullStringText.substr(0, 500)
98
      const shortenedText = parse(`${shortenedString}... `)
99
      return (
5607 stevensc 100
        <>
101
          {isReadMoreActive ? fullText : shortenedText}
102
          <span className="cursor-pointer" onClick={readMoreHandler}>
103
            {isReadMoreActive ? ' Leer menos' : ' Leer más'}
104
          </span>
105
        </>
5118 stevensc 106
      )
4816 stevensc 107
    }
5118 stevensc 108
    return <p>{fullText}</p>
109
  }
3361 stevensc 110
 
5818 stevensc 111
  const saveReaction = (type) => {
7119 stevensc 112
    const formData = new FormData()
113
    formData.append('reaction', type)
114
 
115
    axios.post(post.save_reaction_url, formData).then((res) => {
5812 stevensc 116
      const { success, data } = res.data
117
 
118
      if (!success) {
119
        dispatch(addNotification({ style: 'danger', msg: data }))
7119 stevensc 120
        return
5812 stevensc 121
      }
122
 
123
      setOwnerReaction(data.reactions)
5818 stevensc 124
      setCurrentReaction(type)
5812 stevensc 125
    })
126
  }
127
 
5818 stevensc 128
  const deleteReaction = () => {
129
    axios.post(post.delete_reaction_url).then((res) => {
5812 stevensc 130
      const { success, data } = res.data
131
 
132
      if (!success) {
133
        dispatch(addNotification({ style: 'danger', msg: data }))
134
        return
135
      }
136
 
137
      setOwnerReaction(data.reactions)
5818 stevensc 138
      setCurrentReaction('')
5812 stevensc 139
    })
140
  }
141
 
5814 stevensc 142
  const WithReactionIcon = withReactions(InputOption, {
143
    onSelect: saveReaction,
144
    onDelete: deleteReaction,
5818 stevensc 145
    myReaction: currentReaction,
6017 stevensc 146
    withTitle: true,
5814 stevensc 147
  })
148
 
5118 stevensc 149
  useEffect(() => {
7113 stevensc 150
    axios
151
      .get(window.location.href, {
152
        headers: {
153
          'Content-Type': 'application/json',
154
        },
155
      })
156
      .then((response) => console.log(response.data))
5617 stevensc 157
    dispatch(setIntlLabels(labels))
158
  }, [])
159
 
160
  useEffect(() => {
5118 stevensc 161
    if (showComments && !comments.length) getComments()
162
  }, [showComments])
4821 stevensc 163
 
5812 stevensc 164
  useEffect(() => {
165
    const feedReactions = ownerReactions.reduce(
166
      (acc, reaction) => acc + Number(reaction.total),
167
 
168
    )
169
    setTotalReactions(feedReactions)
170
  }, [ownerReactions])
171
 
5118 stevensc 172
  return (
5607 stevensc 173
    <div className="container">
174
      <div className="d-flex flex-column flex-md-row" style={{ gap: '1rem' }}>
175
        <div className="col-12 col-md-8 p-0">
176
          <div className="feed">
5821 stevensc 177
            <div className="feed__body">
5607 stevensc 178
              {post.image && (
179
                <img
180
                  src={`/storage/type/post/code/${post.uuid}/filename/${post.image}`}
181
                  className="Entradas"
182
                  loading="lazy"
183
                />
184
              )}
185
            </div>
5821 stevensc 186
            <div className="feed__body">
5820 stevensc 187
              <div className="feed__header">
5821 stevensc 188
                <div className="feed__info">
5607 stevensc 189
                  <h2>{post.title}</h2>
190
                  <div className="time__elapse">
191
                    <p>{post.addedOn}</p>
192
                    <AccessTimeIcon className="time__elapse-icon" />
193
                  </div>
1 www 194
                </div>
5607 stevensc 195
              </div>
196
              {post.description && htmlParsedText(post.description)}
7112 stevensc 197
              {post.file && (
198
                <a href={post.file} download>
199
                  <img
200
                    className="pdf"
201
                    src="/images/extension/pdf.png"
202
                    alt="pdf"
203
                  />
204
                </a>
205
              )}
4093 stevensc 206
            </div>
6016 stevensc 207
            <div className="d-flex justify-content-between align-items-center px-3">
5819 stevensc 208
              <div className="reactions-counter">
209
                {reactionsOptions
210
                  .filter((option) =>
211
                    ownerReactions.find(
212
                      (reaction) => reaction.reaction === option.type
213
                    )
5812 stevensc 214
                  )
5819 stevensc 215
                  .map((reaction) => reaction.icon)}
216
                <span>{totalReactions} reacciones</span>
217
              </div>
5813 stevensc 218
              {externalShare && (
6457 stevensc 219
                <span>{`${externalShare} ${labels.SENDS?.toLowerCase()}`}</span>
5813 stevensc 220
              )}
5812 stevensc 221
            </div>
5813 stevensc 222
            <div className="feed__buttons">
5814 stevensc 223
              <WithReactionIcon />
5607 stevensc 224
              <InputOption
225
                Icon={ChatOutlinedIcon}
5812 stevensc 226
                title={labels.COMMENTS}
5607 stevensc 227
                color="gray"
228
                onClick={displayCommentSection}
6017 stevensc 229
                withTitle
5607 stevensc 230
              />
231
              <ExternalShareButton />
232
            </div>
233
            <div className="px-2 pb-2">
234
              <FeedCommentSection
5608 stevensc 235
                isShow={showComments}
5607 stevensc 236
                currentComments={comments}
237
                addUrl={post.comments_add_url}
238
              />
239
            </div>
240
          </div>
5449 stevensc 241
        </div>
5607 stevensc 242
        <div className="col-12 col-md-4 p-0">
243
          <HomeNews currentPost={post.uuid} />
244
        </div>
245
      </div>
246
    </div>
5118 stevensc 247
  )
1 www 248
}