Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1902 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1847 stevensc 1
import React, { useState, useEffect } from 'react'
2
import { Link, useLocation } from 'react-router-dom'
3
import { useDispatch, useSelector } from 'react-redux'
4
import parse from 'html-react-parser'
5
import EditIcon from '@mui/icons-material/EditOutlined'
6
 
7
import { axios } from 'utils/index'
8
import { addNotification } from '../../redux/notification/notification.actions'
9
 
10
import Modal from 'components/UI/modal/Modal'
11
import ConfirmModal from 'components/modals/ConfirmModal'
12
import ImageModal from 'components/modals/ImageModal'
13
import CoverModal from 'components/cover/CoverModal'
14
import OverviewModal from 'components/overview/OverviewModal'
15
 
16
const ProfileCard = ({
17
  full_name: fullName = '',
18
  overview = '',
19
  formatted_address: formattedAddress = '',
20
  total_connections: totalConnections,
21
  show_contact: showContact = false,
22
  experiences = [],
23
  link_inmail: linkInmail = '',
24
  link_cancel: CancelConnectionUrl = '',
25
  link_request: RequestConnectionUrl = '',
26
  image = '',
27
  cover = '',
28
  user_profile_uuid: profileId,
29
  request_connection,
30
  follower,
31
  following,
32
  facebook,
33
  twitter,
34
  instagram,
35
  sizes,
36
  view_following,
37
  view_total_connections
38
}) => {
39
  const [isAdded, setIsAdded] = useState(false)
40
  const [connectionUrl, setConnectionUrl] = useState('')
41
  const [modalToShow, setModalToShow] = useState(null)
42
  const [settedOverview, setSettedOverview] = useState('')
43
  const [profileImg, setProfileImg] = useState('')
44
  const [coverImg, setCoverImg] = useState('')
45
  const [isModalShow, setIsModalShow] = useState(false)
46
  const [isEdit, setIsEdit] = useState(false)
47
  const labels = useSelector(({ intl }) => intl.labels)
48
  const { pathname } = useLocation()
49
  const dispatch = useDispatch()
50
 
51
  const displayModal = () => {
52
    setIsModalShow(!isModalShow)
53
  }
54
 
55
  const getProfileData = async () => {
56
    try {
57
      const { data: response } = await axios.get(pathname)
58
      const { link_request, link_cancel } = response
59
 
60
      if (link_request) {
61
        setConnectionUrl(link_request)
62
        return
63
      }
64
 
65
      setConnectionUrl(link_cancel)
66
    } catch (err) {
67
      dispatch(addNotification({ style: 'danger', msg: err.message }))
68
    }
69
  }
70
 
71
  const connect = async () => {
72
    try {
73
      const { data: response } = await axios.post(connectionUrl)
74
      const { data, success } = response
75
 
76
      if (!success) {
77
        return dispatch(addNotification({ style: 'danger', msg: data }))
78
      }
79
 
80
      if (success && isModalShow) {
81
        displayModal()
82
      }
83
 
84
      await getProfileData()
85
      dispatch(addNotification({ style: 'success', msg: data }))
86
      setIsAdded(!isAdded)
87
    } catch (error) {
88
      dispatch(addNotification({ style: 'danger', msg: `Error: ${error}` }))
89
    }
90
  }
91
 
92
  const closeModal = () => {
93
    setModalToShow(null)
94
  }
95
 
96
  useEffect(() => {
97
    setIsAdded(!request_connection)
98
    setSettedOverview(overview)
99
    setProfileImg(image)
100
    setCoverImg(cover)
101
  }, [request_connection, overview, image, cover])
102
 
103
  useEffect(() => {
104
    RequestConnectionUrl
105
      ? setConnectionUrl(RequestConnectionUrl)
106
      : setConnectionUrl(CancelConnectionUrl)
107
  }, [RequestConnectionUrl, CancelConnectionUrl])
108
 
109
  useEffect(() => {
110
    setIsEdit(pathname.includes('edit'))
111
  }, [pathname])
112
 
113
  return (
114
    <>
115
      <section className='profile__user-card'>
116
        <div className='cover__image-container'>
117
          <img alt='Background Image' src={coverImg} />
118
          {isEdit && (
119
            <button
120
              className='button-icon cover__edit-btn'
121
              onClick={() => setModalToShow('cover')}
122
            >
123
              <EditIcon />
124
            </button>
125
          )}
126
        </div>
127
        <section className='px-4 pb-4'>
128
          <div className='card__image-options'>
129
            <img
130
              alt='Profile Image'
131
              className={`img ${isEdit && 'cursor-pointer'}`}
132
              src={profileImg}
133
              onClick={() => isEdit && setModalToShow('image')}
134
            />
135
            {isEdit && (
136
              <button
137
                className='button-icon'
138
                onClick={() => setModalToShow('overview')}
139
              >
140
                <EditIcon />
141
              </button>
142
            )}
143
          </div>
144
          <div className='card-content'>
145
            <div className='card-info'>
146
              <h1>{fullName}</h1>
147
              {settedOverview && parse(settedOverview)}
148
              <div
149
                className='d-inline-flex align-items-center mt-2'
150
                style={{ gap: '1rem' }}
151
              >
152
                <span>{formattedAddress}</span>
153
                <Link
154
                  onClick={(e) => {
155
                    e.preventDefault()
156
                    setModalToShow('info')
157
                  }}
158
                >
159
                  {labels.personal_info}
160
                </Link>
161
              </div>
162
              <div
163
                className='d-inline-flex align-items-center mt-2'
164
                style={{ gap: '1rem' }}
165
              >
166
                {Boolean(totalConnections) && view_total_connections && (
167
                  <Link to='/connection/my-connections'>
168
                    {`${totalConnections} ${labels.connections}`}
169
                  </Link>
170
                )}
171
                {Boolean(follower) && view_following && (
172
                  <Link onClick={(e) => e.preventDefault()}>
173
                    {`${follower} ${labels.followers}`}
174
                  </Link>
175
                )}
176
                {Boolean(following) && view_following && (
177
                  <Link to='/company/following-companies'>
178
                    {`${following} ${labels.following}`}
179
                  </Link>
180
                )}
181
              </div>
182
              <div className='button-actions mt-2'>
183
                {connectionUrl && isAdded && (
184
                  <button
185
                    className='btn button btn-primary'
186
                    onClick={() => displayModal()}
187
                  >
188
                    {labels.cancel}
189
                  </button>
190
                )}
191
                {connectionUrl && !isAdded && (
192
                  <button className='btn button btn-primary' onClick={connect}>
193
                    {labels.connect}
194
                  </button>
195
                )}
196
                {showContact && (
197
                  <Link to={linkInmail} className='btn button btn-secondary'>
198
                    {labels.message}
199
                  </Link>
200
                )}
201
              </div>
202
            </div>
203
            <div className='card-experiences'>
204
              <ul>
205
                {experiences.map(
206
                  ({ company, title, industry, size }, index) => (
207
                    <li key={index}>
208
                      <span>{`${company} - ${title}`}</span>
209
                      <p>{`${industry} / ${size}`}</p>
210
                    </li>
211
                  )
212
                )}
213
              </ul>
214
            </div>
215
          </div>
216
        </section>
217
      </section>
218
 
219
      {isEdit && (
220
        <>
221
          <OverviewModal
222
            isOpen={modalToShow === 'overview'}
223
            overview={settedOverview}
224
            id={profileId}
225
            closeModal={() => closeModal()}
226
            onComplete={(newOverview) => setSettedOverview(newOverview)}
227
          />
228
          <CoverModal
229
            isOpen={modalToShow === 'cover'}
230
            sizes={sizes?.cover}
231
            onClose={() => closeModal()}
232
            onComplete={(newImage) => setCoverImg(newImage)}
233
          />
234
          <ImageModal
235
            show={modalToShow === 'image'}
236
            id={profileId}
237
            sizes={sizes?.image}
238
            onClose={() => closeModal()}
239
            onComplete={(newImage) => setProfileImg(newImage)}
240
          />
241
        </>
242
      )}
243
 
244
      <ProfileCard.Modal
245
        show={modalToShow === 'info'}
246
        closeModal={() => closeModal()}
247
        fullName={fullName}
248
        facebook={facebook}
249
        twitter={twitter}
250
        instagram={instagram}
251
        following={following}
252
        formatted_address={formattedAddress}
253
        overview={overview}
254
        total_connections={totalConnections}
255
        follower={follower}
256
      />
257
      <ConfirmModal
258
        show={isModalShow}
259
        onClose={() => setIsModalShow(false)}
260
        onAccept={() => connect()}
261
      />
262
    </>
263
  )
264
}
265
 
266
const InfoModal = ({
267
  show,
268
  closeModal,
269
  facebook,
270
  following,
271
  formatted_address,
272
  fullName,
273
  instagram,
274
  overview,
275
  total_connections,
276
  twitter,
277
  follower
278
}) => {
279
  const labels = useSelector(({ intl }) => intl.labels)
280
 
281
  return (
282
    <Modal title={labels.personal_info} show={show} onClose={closeModal}>
283
      <div className='description__label'>
284
        <label htmlFor='name'>{labels.first_name}</label>
285
        <p>{fullName}</p>
286
      </div>
287
      {overview && (
288
        <div className='description__label'>
289
          <label htmlFor='name'>{labels.description}</label>
290
          {overview && parse(overview)}
291
        </div>
292
      )}
293
      {formatted_address && (
294
        <div className='description__label'>
295
          <label htmlFor='name'>{labels.location}</label>
296
          <p>{formatted_address}</p>
297
        </div>
298
      )}
299
      {total_connections && (
300
        <div className='description__label'>
301
          <label htmlFor='name'>{labels.connections}</label>
302
          <p>{total_connections}</p>
303
        </div>
304
      )}
305
      {follower && (
306
        <div className='description__label'>
307
          <label htmlFor='name'>{labels.followers}</label>
308
          <p>{follower}</p>
309
        </div>
310
      )}
311
      {following && (
312
        <div className='description__label'>
313
          <label htmlFor='name'>{labels.following}</label>
314
          <p>{following}</p>
315
        </div>
316
      )}
317
      <div className='description__label'>
318
        <label htmlFor='name'>{labels.social_networks}</label>
319
        {facebook && (
320
          <a href={facebook} target='_blank' rel='noreferrer'>
321
            <p className='mb-1'>{facebook}</p>
322
          </a>
323
        )}
324
        {instagram && (
325
          <a href={instagram} target='_blank' rel='noreferrer'>
326
            <p className='mb-1'>{instagram}</p>
327
          </a>
328
        )}
329
        {twitter && (
330
          <a href={twitter} target='_blank' rel='noreferrer'>
331
            <p className='mb-1'>{twitter}</p>
332
          </a>
333
        )}
334
      </div>
335
    </Modal>
336
  )
337
}
338
 
339
ProfileCard.Modal = InfoModal
340
 
341
export default ProfileCard