Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2913 | Rev 3032 | 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'
2912 stevensc 4
import { Button, Typography } from '@mui/material'
1847 stevensc 5
 
2902 stevensc 6
import { axios, parse } from '@utils'
7
import { addNotification } from '@store/notification/notification.actions'
1847 stevensc 8
 
2902 stevensc 9
import Widget from '@components/UI/Widget'
10
import Cover from '@components/UI/cover/Cover'
11
import ConfirmModal from '@components/modals/ConfirmModal'
12
import ProfileModal from './ProfileModal'
2909 stevensc 13
import Avatar from '@components/common/Avatar'
14
import Row from '@components/common/Row'
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 = [],
2902 stevensc 34
  view_following: viewFollowing,
2905 stevensc 35
  view_total_connections: viewTotalConnections
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 [isModalShow, setIsModalShow] = useState(false)
42
  const [isEdit, setIsEdit] = useState(false)
43
  const labels = useSelector(({ intl }) => intl.labels)
44
  const { pathname } = useLocation()
45
  const dispatch = useDispatch()
46
 
47
  const displayModal = () => {
48
    setIsModalShow(!isModalShow)
49
  }
50
 
51
  const getProfileData = async () => {
52
    try {
53
      const { data: response } = await axios.get(pathname)
54
      const { link_request, link_cancel } = response
55
 
56
      if (link_request) {
57
        setConnectionUrl(link_request)
58
        return
59
      }
60
 
61
      setConnectionUrl(link_cancel)
62
    } catch (err) {
63
      dispatch(addNotification({ style: 'danger', msg: err.message }))
64
    }
65
  }
66
 
67
  const connect = async () => {
68
    try {
69
      const { data: response } = await axios.post(connectionUrl)
70
      const { data, success } = response
71
 
72
      if (!success) {
73
        return dispatch(addNotification({ style: 'danger', msg: data }))
74
      }
75
 
76
      if (success && isModalShow) {
77
        displayModal()
78
      }
79
 
80
      await getProfileData()
81
      dispatch(addNotification({ style: 'success', msg: data }))
82
      setIsAdded(!isAdded)
83
    } catch (error) {
84
      dispatch(addNotification({ style: 'danger', msg: `Error: ${error}` }))
85
    }
86
  }
87
 
88
  const closeModal = () => {
89
    setModalToShow(null)
90
  }
91
 
92
  useEffect(() => {
2902 stevensc 93
    setIsAdded(!requestConnection)
1847 stevensc 94
    setSettedOverview(overview)
2909 stevensc 95
  }, [requestConnection, overview])
1847 stevensc 96
 
97
  useEffect(() => {
2902 stevensc 98
    linkRequest ? setConnectionUrl(linkRequest) : setConnectionUrl(linkCancel)
99
  }, [linkRequest, linkCancel])
1847 stevensc 100
 
101
  useEffect(() => {
102
    setIsEdit(pathname.includes('edit'))
103
  }, [pathname])
104
 
105
  return (
2902 stevensc 106
    <Widget>
2912 stevensc 107
      <Cover cover={cover} />
2906 stevensc 108
      <Widget.Body>
2911 stevensc 109
        <Avatar
110
          src={image}
111
          alt={fullName}
112
          styles={{
113
            width: { xs: '100px', lg: '150px' },
114
            height: { xs: '100px', lg: '150px' },
115
            mt: { xs: '-50px', lg: '-75px' },
116
            border: '4px solid #fff',
117
            backgroundColor: '#c9ced4',
118
            cursor: isEdit ? 'pointer' : 'default'
119
          }}
120
        />
2906 stevensc 121
 
2902 stevensc 122
        <Typography variant='h2'>{fullName}</Typography>
123
        <Typography>{parse(settedOverview)}</Typography>
124
 
2909 stevensc 125
        <Row>
2902 stevensc 126
          <Typography>{formattedAddress}</Typography>
2917 stevensc 127
          <Typography variant='overline'> - </Typography>
128
          <Typography variant='overline' onClick={() => setModalToShow('info')}>
2902 stevensc 129
            {labels.personal_info}
2909 stevensc 130
          </Typography>
131
        </Row>
2902 stevensc 132
 
2909 stevensc 133
        <Row>
2912 stevensc 134
          {viewTotalConnections ? (
2902 stevensc 135
            <Link to='/connection/my-connections'>
2917 stevensc 136
              <Typography variant='overline'>
2913 stevensc 137
                {`${totalConnections} conexiones`}
2911 stevensc 138
              </Typography>
2902 stevensc 139
            </Link>
2912 stevensc 140
          ) : null}
141
          {viewFollowing ? (
2902 stevensc 142
            <Link onClick={(e) => e.preventDefault()}>
2917 stevensc 143
              <Typography variant='overline'>
2913 stevensc 144
                {`${following} siguiendo`}
2911 stevensc 145
              </Typography>
2902 stevensc 146
            </Link>
2912 stevensc 147
          ) : null}
2909 stevensc 148
        </Row>
2902 stevensc 149
 
2909 stevensc 150
        <Row>
151
          {connectionUrl && isAdded && (
152
            <Button variant='primary' onClick={() => displayModal()}>
153
              {labels.cancel}
154
            </Button>
155
          )}
156
          {connectionUrl && !isAdded && (
157
            <Button variant='primary' onClick={connect}>
158
              {labels.connect}
159
            </Button>
160
          )}
161
          {showContact && (
162
            <Button to={linkInmail} LinkComponent={Link} variant='secondary'>
163
              {labels.message}
164
            </Button>
165
          )}
166
        </Row>
2902 stevensc 167
      </Widget.Body>
2909 stevensc 168
      {/*  {isEdit && (
169
        <>
170
          <OverviewModal
171
            isOpen={modalToShow === 'overview'}
172
            overview={settedOverview}
173
            id={profileId}
174
            closeModal={() => closeModal()}
175
            onComplete={(newOverview) => setSettedOverview(newOverview)}
176
          />
177
        </>
178
      )} */}
1847 stevensc 179
 
2902 stevensc 180
      <ProfileModal
1847 stevensc 181
        show={modalToShow === 'info'}
182
        closeModal={() => closeModal()}
183
        fullName={fullName}
184
        facebook={facebook}
185
        twitter={twitter}
186
        instagram={instagram}
187
        following={following}
188
        formatted_address={formattedAddress}
189
        overview={overview}
190
        total_connections={totalConnections}
2902 stevensc 191
        // follower={follower}
1847 stevensc 192
      />
193
      <ConfirmModal
194
        show={isModalShow}
195
        onClose={() => setIsModalShow(false)}
196
        onAccept={() => connect()}
197
      />
2902 stevensc 198
    </Widget>
1847 stevensc 199
  )
200
}
201
 
202
export default ProfileCard