Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2142 | Rev 2905 | 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'
2902 stevensc 4
import { Avatar, Box, Button, IconButton, Typography } from '@mui/material'
5
import { Edit } from '@mui/icons-material'
1847 stevensc 6
 
2902 stevensc 7
import { axios, parse } from '@utils'
8
import { addNotification } from '@store/notification/notification.actions'
1847 stevensc 9
 
2902 stevensc 10
import Widget from '@components/UI/Widget'
11
import Cover from '@components/UI/cover/Cover'
12
import ConfirmModal from '@components/modals/ConfirmModal'
13
import ProfileModal from './ProfileModal'
1847 stevensc 14
 
15
const ProfileCard = ({
2902 stevensc 16
  full_name: fullName,
17
  user_profile_id: userProfileId,
18
  image,
1915 stevensc 19
  cover,
2902 stevensc 20
  overview,
1847 stevensc 21
  facebook,
2902 stevensc 22
  instagram,
1847 stevensc 23
  twitter,
2902 stevensc 24
  formatted_address: formattedAddress,
25
  user_experiences: userExperiences,
26
  show_contact: showContact,
27
  link_inmail: linkInmail,
28
  following,
29
  view_following: viewFollowing,
30
  total_connections: totalConnections,
31
  view_total_connections: viewTotalConnections,
32
  request_connection: requestConnection,
33
  link_cancel: linkCancel,
34
  link_request: linkRequest,
35
  sizes
1847 stevensc 36
}) => {
37
  const [isAdded, setIsAdded] = useState(false)
38
  const [connectionUrl, setConnectionUrl] = useState('')
39
  const [modalToShow, setModalToShow] = useState(null)
40
  const [settedOverview, setSettedOverview] = useState('')
41
  const [profileImg, setProfileImg] = useState('')
42
  const [isModalShow, setIsModalShow] = useState(false)
43
  const [isEdit, setIsEdit] = useState(false)
44
  const labels = useSelector(({ intl }) => intl.labels)
45
  const { pathname } = useLocation()
46
  const dispatch = useDispatch()
47
 
2902 stevensc 48
  const showConnections = totalConnections && viewTotalConnections
49
  const showFollowing = following && viewFollowing
50
 
1847 stevensc 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(() => {
2902 stevensc 97
    setIsAdded(!requestConnection)
1847 stevensc 98
    setSettedOverview(overview)
99
    setProfileImg(image)
2902 stevensc 100
  }, [requestConnection, overview, image])
1847 stevensc 101
 
102
  useEffect(() => {
2902 stevensc 103
    linkRequest ? setConnectionUrl(linkRequest) : setConnectionUrl(linkCancel)
104
  }, [linkRequest, linkCancel])
1847 stevensc 105
 
106
  useEffect(() => {
107
    setIsEdit(pathname.includes('edit'))
108
  }, [pathname])
109
 
110
  return (
2902 stevensc 111
    <Widget>
112
      <Cover
113
        cover={cover}
114
        sizes={sizes?.cover}
115
        edit={isEdit}
116
        editUrl={`/profile/my-profiles/cover/${userProfileId}/operation/upload`}
117
      />
118
      <Box>
119
        <Avatar
120
          src={profileImg}
121
          alt={fullName}
122
          sx={{
123
            width: { xs: '100px', lg: '150px' },
124
            height: { xs: '100px', lg: '150px' },
125
            mt: { xs: '-50px', lg: '-75px' },
126
            border: '4px solid #fff',
127
            backgroundColor: '#c9ced4',
128
            cursor: isEdit ? 'pointer' : 'default'
129
          }}
130
          onClick={() => isEdit && setModalToShow('image')}
1902 stevensc 131
        />
2902 stevensc 132
        {isEdit && (
133
          <IconButton onClick={() => setModalToShow('overview')}>
134
            <Edit />
135
          </IconButton>
136
        )}
137
      </Box>
138
      <Widget.Body>
139
        <Typography variant='h2'>{fullName}</Typography>
140
        <Typography>{parse(settedOverview)}</Typography>
141
 
142
        <Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
143
          <Typography>{formattedAddress}</Typography>
144
          <Button onClick={() => setModalToShow('info')}>
145
            {labels.personal_info}
146
          </Button>
147
        </Box>
148
 
149
        <div
150
          className='d-inline-flex align-items-center mt-2'
151
          style={{ gap: '1rem' }}
152
        >
153
          {showConnections && (
154
            <Link to='/connection/my-connections'>
155
              {`${totalConnections} ${labels.connections}`}
156
            </Link>
157
          )}
158
          {showFollowing && (
159
            <Link onClick={(e) => e.preventDefault()}>
160
              {`${following} ${labels.following}`}
161
            </Link>
162
          )}
163
        </div>
164
        <div className='button-actions mt-2'>
165
          {connectionUrl && isAdded && (
166
            <Button variant='primary' onClick={() => displayModal()}>
167
              {labels.cancel}
168
            </Button>
169
          )}
170
          {connectionUrl && !isAdded && (
171
            <Button variant='primary' onClick={connect}>
172
              {labels.connect}
173
            </Button>
174
          )}
175
          {showContact && (
176
            <Button to={linkInmail} LinkComponent={Link} variant='secondary'>
177
              {labels.message}
178
            </Button>
179
          )}
180
        </div>
181
 
182
        <div className='card-experiences'>
183
          <ul>
184
            {userExperiences.map(
185
              ({ company, title, industry, size }, index) => (
186
                <li key={index}>
187
                  <span>{`${company} - ${title}`}</span>
188
                  <p>{`${industry} / ${size}`}</p>
189
                </li>
190
              )
1847 stevensc 191
            )}
2902 stevensc 192
          </ul>
193
        </div>
194
      </Widget.Body>
1847 stevensc 195
 
2902 stevensc 196
      <ProfileModal
1847 stevensc 197
        show={modalToShow === 'info'}
198
        closeModal={() => closeModal()}
199
        fullName={fullName}
200
        facebook={facebook}
201
        twitter={twitter}
202
        instagram={instagram}
203
        following={following}
204
        formatted_address={formattedAddress}
205
        overview={overview}
206
        total_connections={totalConnections}
2902 stevensc 207
        // follower={follower}
1847 stevensc 208
      />
209
      <ConfirmModal
210
        show={isModalShow}
211
        onClose={() => setIsModalShow(false)}
212
        onAccept={() => connect()}
213
      />
2902 stevensc 214
    </Widget>
1847 stevensc 215
  )
216
}
217
 
218
export default ProfileCard