Proyectos de Subversion LeadersLinked - SPA

Rev

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