Proyectos de Subversion LeadersLinked - SPA

Rev

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