Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1915 | Rev 1932 | 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'
1921 stevensc 16
import { 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'>
124
            <img
125
              alt='Profile Image'
126
              className={`img ${isEdit && 'cursor-pointer'}`}
127
              src={profileImg}
128
              onClick={() => isEdit && setModalToShow('image')}
129
            />
130
            {isEdit && (
1921 stevensc 131
              <IconButton onClick={() => setModalToShow('overview')}>
1847 stevensc 132
                <EditIcon />
1921 stevensc 133
              </IconButton>
1847 stevensc 134
            )}
135
          </div>
136
          <div className='card-content'>
137
            <div className='card-info'>
138
              <h1>{fullName}</h1>
139
              {settedOverview && parse(settedOverview)}
140
              <div
141
                className='d-inline-flex align-items-center mt-2'
142
                style={{ gap: '1rem' }}
143
              >
144
                <span>{formattedAddress}</span>
145
                <Link
146
                  onClick={(e) => {
147
                    e.preventDefault()
148
                    setModalToShow('info')
149
                  }}
150
                >
151
                  {labels.personal_info}
152
                </Link>
153
              </div>
154
              <div
155
                className='d-inline-flex align-items-center mt-2'
156
                style={{ gap: '1rem' }}
157
              >
158
                {Boolean(totalConnections) && view_total_connections && (
159
                  <Link to='/connection/my-connections'>
160
                    {`${totalConnections} ${labels.connections}`}
161
                  </Link>
162
                )}
163
                {Boolean(follower) && view_following && (
164
                  <Link onClick={(e) => e.preventDefault()}>
165
                    {`${follower} ${labels.followers}`}
166
                  </Link>
167
                )}
168
                {Boolean(following) && view_following && (
169
                  <Link to='/company/following-companies'>
170
                    {`${following} ${labels.following}`}
171
                  </Link>
172
                )}
173
              </div>
174
              <div className='button-actions mt-2'>
175
                {connectionUrl && isAdded && (
176
                  <button
177
                    className='btn button btn-primary'
178
                    onClick={() => displayModal()}
179
                  >
180
                    {labels.cancel}
181
                  </button>
182
                )}
183
                {connectionUrl && !isAdded && (
184
                  <button className='btn button btn-primary' onClick={connect}>
185
                    {labels.connect}
186
                  </button>
187
                )}
188
                {showContact && (
189
                  <Link to={linkInmail} className='btn button btn-secondary'>
190
                    {labels.message}
191
                  </Link>
192
                )}
193
              </div>
194
            </div>
195
            <div className='card-experiences'>
196
              <ul>
197
                {experiences.map(
198
                  ({ company, title, industry, size }, index) => (
199
                    <li key={index}>
200
                      <span>{`${company} - ${title}`}</span>
201
                      <p>{`${industry} / ${size}`}</p>
202
                    </li>
203
                  )
204
                )}
205
              </ul>
206
            </div>
207
          </div>
208
        </section>
1915 stevensc 209
      </WidgetWrapper>
1847 stevensc 210
 
211
      {isEdit && (
212
        <>
213
          <OverviewModal
214
            isOpen={modalToShow === 'overview'}
215
            overview={settedOverview}
216
            id={profileId}
217
            closeModal={() => closeModal()}
218
            onComplete={(newOverview) => setSettedOverview(newOverview)}
219
          />
220
          <ImageModal
221
            show={modalToShow === 'image'}
222
            id={profileId}
223
            sizes={sizes?.image}
224
            onClose={() => closeModal()}
225
            onComplete={(newImage) => setProfileImg(newImage)}
226
          />
227
        </>
228
      )}
229
 
230
      <ProfileCard.Modal
231
        show={modalToShow === 'info'}
232
        closeModal={() => closeModal()}
233
        fullName={fullName}
234
        facebook={facebook}
235
        twitter={twitter}
236
        instagram={instagram}
237
        following={following}
238
        formatted_address={formattedAddress}
239
        overview={overview}
240
        total_connections={totalConnections}
241
        follower={follower}
242
      />
243
      <ConfirmModal
244
        show={isModalShow}
245
        onClose={() => setIsModalShow(false)}
246
        onAccept={() => connect()}
247
      />
248
    </>
249
  )
250
}
251
 
252
const InfoModal = ({
253
  show,
254
  closeModal,
255
  facebook,
256
  following,
257
  formatted_address,
258
  fullName,
259
  instagram,
260
  overview,
261
  total_connections,
262
  twitter,
263
  follower
264
}) => {
265
  const labels = useSelector(({ intl }) => intl.labels)
266
 
267
  return (
268
    <Modal title={labels.personal_info} show={show} onClose={closeModal}>
269
      <div className='description__label'>
270
        <label htmlFor='name'>{labels.first_name}</label>
271
        <p>{fullName}</p>
272
      </div>
273
      {overview && (
274
        <div className='description__label'>
275
          <label htmlFor='name'>{labels.description}</label>
276
          {overview && parse(overview)}
277
        </div>
278
      )}
279
      {formatted_address && (
280
        <div className='description__label'>
281
          <label htmlFor='name'>{labels.location}</label>
282
          <p>{formatted_address}</p>
283
        </div>
284
      )}
285
      {total_connections && (
286
        <div className='description__label'>
287
          <label htmlFor='name'>{labels.connections}</label>
288
          <p>{total_connections}</p>
289
        </div>
290
      )}
291
      {follower && (
292
        <div className='description__label'>
293
          <label htmlFor='name'>{labels.followers}</label>
294
          <p>{follower}</p>
295
        </div>
296
      )}
297
      {following && (
298
        <div className='description__label'>
299
          <label htmlFor='name'>{labels.following}</label>
300
          <p>{following}</p>
301
        </div>
302
      )}
303
      <div className='description__label'>
304
        <label htmlFor='name'>{labels.social_networks}</label>
305
        {facebook && (
306
          <a href={facebook} target='_blank' rel='noreferrer'>
307
            <p className='mb-1'>{facebook}</p>
308
          </a>
309
        )}
310
        {instagram && (
311
          <a href={instagram} target='_blank' rel='noreferrer'>
312
            <p className='mb-1'>{instagram}</p>
313
          </a>
314
        )}
315
        {twitter && (
316
          <a href={twitter} target='_blank' rel='noreferrer'>
317
            <p className='mb-1'>{twitter}</p>
318
          </a>
319
        )}
320
      </div>
321
    </Modal>
322
  )
323
}
324
 
325
ProfileCard.Modal = InfoModal
326
 
327
export default ProfileCard