Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16724 | Rev 16726 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
16655 stevensc 1
import React, { useEffect, useState } from 'react'
16650 stevensc 2
import { deleteFeed } from '../../redux/feed/feed.actions'
3
import DeleteModal from '../../shared/DeleteModal'
16666 stevensc 4
import CommentForm from './CommentForm'
5
import CommentsList from './CommentsList'
16650 stevensc 6
import parse from 'html-react-parser'
7
import styles from './feeds.module.scss'
16655 stevensc 8
import Options from '../../shared/options/Options'
16650 stevensc 9
 
10
const FeedTemplate = ({ feed }) => {
16666 stevensc 11
  const [comments, setComments] = useState(feed.comments)
12
  const [totalComments, setTotalComments] = useState(feed.owner_comments)
16650 stevensc 13
  const [showDeleteModal, setShowDeleteModal] = useState(false)
16655 stevensc 14
  const [options, setOptions] = useState([])
16650 stevensc 15
 
16655 stevensc 16
  const toggleModal = () => {
17
    setShowDeleteModal(!showDeleteModal)
18
  }
16650 stevensc 19
 
16666 stevensc 20
  const deleteComment = (id) => {
21
    setComments((prevComments) =>
22
      prevComments.filter((comment) => comment.unique === id)
23
    )
24
  }
25
 
16655 stevensc 26
  useEffect(() => {
27
    if (feed.feed_delete_url) {
28
      setOptions((prevOptions) =>
29
        prevOptions.concat({ label: 'Borrar', action: toggleModal })
16650 stevensc 30
      )
31
    }
16655 stevensc 32
  }, [])
16650 stevensc 33
 
34
  return (
35
    <>
36
      <article className={styles.feed}>
37
        <FeedTemplate.Header
38
          name={feed.owner_name}
39
          image={feed.owner_image}
40
          profileUrl={feed.owner_url}
41
          timeElapsed={feed.owner_time_elapse}
16655 stevensc 42
          options={options}
16650 stevensc 43
        />
16655 stevensc 44
        <FeedTemplate.Content
45
          description={feed.owner_description}
46
          image={feed.owner_file_image}
47
          previewImage={feed.owner_file_image_preview}
48
          video={feed.owner_file_video}
49
          document={feed.owner_file_document}
50
          feedShared={{
51
            owner_description: feed.shared_description,
52
            owner_file_image: feed.shared_file_image,
53
            owner_file_image_preview: feed.shared_file_image_preview,
54
            owner_file_video: feed.shared_file_video,
55
            owner_file_document: feed.shared_file_document,
16656 stevensc 56
            owner_time_elapse: feed.shared_time_elapse
16655 stevensc 57
          }}
16725 stevensc 58
          type={feed.feed_content_type}
16655 stevensc 59
        />
16666 stevensc 60
        <CommentForm
61
          image={feed.owner_image}
62
          profileUrl={feed.owner_url}
63
          sendUrl={feed.comments_link_add}
64
          onSubmit={(data) => {
65
            setTotalComments(data.total_comments)
66
            setComments(data.data)
67
          }}
16650 stevensc 68
        />
16666 stevensc 69
        <CommentsList comments={comments} onDelete={deleteComment} />
16650 stevensc 70
      </article>
71
      <DeleteModal
72
        url={feed.feed_delete_url}
73
        isOpen={showDeleteModal}
16658 stevensc 74
        closeModal={toggleModal}
16650 stevensc 75
        title="Esta seguro de borrar esta publicación?"
76
        action={() => deleteFeed(feed.feed_unique)}
77
        message="La publicación ha sido borrada"
78
      />
79
    </>
80
  )
81
}
82
 
16655 stevensc 83
const Header = ({ image, name, profileUrl, timeElapsed, options = null }) => {
16650 stevensc 84
  return (
85
    <div className={styles.feed_header}>
86
      <img src={image} alt={`${name} profile image`} />
87
 
88
      <div className={styles.feed_info}>
89
        <a href={profileUrl}>
90
          <h2>{name}</h2>
91
        </a>
92
        <span>{timeElapsed}</span>
93
      </div>
94
 
16655 stevensc 95
      {options && <Options options={options} />}
96
    </div>
97
  )
98
}
99
 
100
const Body = ({
101
  description,
102
  image,
103
  video,
104
  previewImage,
105
  document,
16724 stevensc 106
  feedShared,
107
  type
16655 stevensc 108
}) => {
109
  const [isReadMoreActive, setIsReadMoreActive] = useState(false)
110
 
111
  const readMoreHandler = () => {
112
    setIsReadMoreActive(!isReadMoreActive)
113
  }
114
 
115
  const htmlParsedText = (fullStringText) => {
116
    const fullText = parse(fullStringText)
117
 
118
    if (fullStringText.length > 500) {
119
      const shortenedString = fullStringText.substr(0, 500)
120
      const shortenedText = parse(`${shortenedString}... `)
121
 
122
      return (
123
        <>
124
          {isReadMoreActive ? fullText : shortenedText}
125
          <button className="btn py-0 px-1" onClick={readMoreHandler}>
126
            {isReadMoreActive ? ' Leer menos' : ' Leer más'}
127
          </button>
128
        </>
129
      )
130
    }
131
 
132
    return fullText
133
  }
134
 
135
  return (
136
    <div className={styles.feed_content}>
16724 stevensc 137
      {type !== 'fast-survey' ? (
138
        htmlParsedText(description)
139
      ) : (
140
        <SurveyTemplate
141
          active={description.active}
142
          question={description.question}
143
          answers={[
144
            description.answer1,
145
            description.answer2,
146
            description.answer3,
147
            description.answer4,
148
            description.answer5
149
          ]}
150
          time={description.time_remaining}
151
        />
152
      )}
16655 stevensc 153
      {image && (
16660 stevensc 154
        <div className={styles.img_container}>
16666 stevensc 155
          <img src={image} alt="Article image" />
156
          <img src={image} alt="Article image" />
16650 stevensc 157
        </div>
158
      )}
16655 stevensc 159
      {video && (
160
        <video src={video} preload="none" poster={previewImage} controls />
161
      )}
162
      {document && (
163
        <a href={document} target="_blank" rel="noreferrer">
164
          Descargar
165
        </a>
166
      )}
16659 stevensc 167
      {feedShared.owner_description && <FeedTemplate feed={feedShared} />}
16650 stevensc 168
    </div>
169
  )
170
}
171
 
16724 stevensc 172
const SurveyTemplate = ({ question, answers = [], active, time }) => {
173
  const [isActive, setIsActive] = useState(true)
174
 
175
  useEffect(() => {
176
    setIsActive(Boolean(active))
177
  }, [active])
178
 
179
  return (
180
    <form action="">
181
      <h3>{question}</h3>
182
      {answers.map((option, index) => (
183
        <div className="survey_input" key={index}>
184
          <label htmlFor={`option-${index + 1}`}>{option}</label>
185
          <input
186
            type="radio"
187
            name="option"
188
            id={`option-${index + 1}`}
189
            disabled={!isActive || !Boolean(time)}
190
          />
191
        </div>
192
      ))}
193
    </form>
194
  )
195
}
196
 
16650 stevensc 197
FeedTemplate.Header = Header
16655 stevensc 198
FeedTemplate.Content = Body
16650 stevensc 199
 
200
export default React.memo(FeedTemplate)