Proyectos de Subversion LeadersLinked - SPA

Rev

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