Proyectos de Subversion LeadersLinked - SPA

Rev

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