Proyectos de Subversion LeadersLinked - SPA

Rev

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