Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2909 | Rev 2912 | 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'
2909 stevensc 4
import { Button, IconButton, Typography } from '@mui/material'
2902 stevensc 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'
2909 stevensc 14
import Avatar from '@components/common/Avatar'
15
import Row from '@components/common/Row'
1847 stevensc 16
 
17
const ProfileCard = ({
2905 stevensc 18
  cover,
19
  facebook,
20
  following,
21
  formatted_address: formattedAddress,
2902 stevensc 22
  full_name: fullName,
23
  image,
2905 stevensc 24
  instagram,
25
  link_cancel: linkCancel,
26
  link_inmail: linkInmail,
27
  link_request: linkRequest,
2902 stevensc 28
  overview,
2905 stevensc 29
  request_connection: requestConnection,
30
  show_contact: showContact,
31
  sizes,
32
  total_connections: totalConnections,
1847 stevensc 33
  twitter,
2905 stevensc 34
  user_experiences: userExperiences = [],
35
  user_profile_id: userProfileId,
2902 stevensc 36
  view_following: viewFollowing,
2905 stevensc 37
  view_total_connections: viewTotalConnections
1847 stevensc 38
}) => {
39
  const [isAdded, setIsAdded] = useState(false)
40
  const [connectionUrl, setConnectionUrl] = useState('')
41
  const [modalToShow, setModalToShow] = useState(null)
42
  const [settedOverview, setSettedOverview] = 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)
2909 stevensc 100
  }, [requestConnection, overview])
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
      />
2906 stevensc 118
      <Widget.Body>
2911 stevensc 119
        <Avatar
120
          src={image}
121
          alt={fullName}
122
          styles={{
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
        />
2906 stevensc 131
 
2902 stevensc 132
        <Typography variant='h2'>{fullName}</Typography>
133
        <Typography>{parse(settedOverview)}</Typography>
134
 
2909 stevensc 135
        <Row>
2902 stevensc 136
          <Typography>{formattedAddress}</Typography>
2909 stevensc 137
          <Typography variant='body2'> - </Typography>
138
          <Typography variant='body2' onClick={() => setModalToShow('info')}>
2902 stevensc 139
            {labels.personal_info}
2909 stevensc 140
          </Typography>
141
        </Row>
2902 stevensc 142
 
2909 stevensc 143
        <Row>
2902 stevensc 144
          {showConnections && (
145
            <Link to='/connection/my-connections'>
2911 stevensc 146
              <Typography variant='body2'>
147
                {`${totalConnections} ${labels.connections}`}
148
              </Typography>
2902 stevensc 149
            </Link>
150
          )}
151
          {showFollowing && (
152
            <Link onClick={(e) => e.preventDefault()}>
2911 stevensc 153
              <Typography variant='body2'>
154
                {`${following} ${labels.following}`}
155
              </Typography>
2902 stevensc 156
            </Link>
157
          )}
2909 stevensc 158
        </Row>
2902 stevensc 159
 
2909 stevensc 160
        <Row>
161
          {connectionUrl && isAdded && (
162
            <Button variant='primary' onClick={() => displayModal()}>
163
              {labels.cancel}
164
            </Button>
165
          )}
166
          {connectionUrl && !isAdded && (
167
            <Button variant='primary' onClick={connect}>
168
              {labels.connect}
169
            </Button>
170
          )}
171
          {showContact && (
172
            <Button to={linkInmail} LinkComponent={Link} variant='secondary'>
173
              {labels.message}
174
            </Button>
175
          )}
176
        </Row>
2902 stevensc 177
      </Widget.Body>
2909 stevensc 178
      {/*  {isEdit && (
179
        <>
180
          <OverviewModal
181
            isOpen={modalToShow === 'overview'}
182
            overview={settedOverview}
183
            id={profileId}
184
            closeModal={() => closeModal()}
185
            onComplete={(newOverview) => setSettedOverview(newOverview)}
186
          />
187
        </>
188
      )} */}
1847 stevensc 189
 
2902 stevensc 190
      <ProfileModal
1847 stevensc 191
        show={modalToShow === 'info'}
192
        closeModal={() => closeModal()}
193
        fullName={fullName}
194
        facebook={facebook}
195
        twitter={twitter}
196
        instagram={instagram}
197
        following={following}
198
        formatted_address={formattedAddress}
199
        overview={overview}
200
        total_connections={totalConnections}
2902 stevensc 201
        // follower={follower}
1847 stevensc 202
      />
203
      <ConfirmModal
204
        show={isModalShow}
205
        onClose={() => setIsModalShow(false)}
206
        onAccept={() => connect()}
207
      />
2902 stevensc 208
    </Widget>
1847 stevensc 209
  )
210
}
211
 
212
export default ProfileCard