Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2906 | Rev 2911 | 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>
2909 stevensc 119
        <Row>
120
          <Avatar
121
            src={image}
122
            alt={fullName}
123
            styles={{
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
            url={`/profile/my-profiles/image/${userProfileId}/operation/upload`}
132
            edit
133
          />
2906 stevensc 134
 
2909 stevensc 135
          {isEdit && (
136
            <IconButton onClick={() => setModalToShow('overview')}>
137
              <Edit />
138
            </IconButton>
139
          )}
140
        </Row>
2906 stevensc 141
 
2902 stevensc 142
        <Typography variant='h2'>{fullName}</Typography>
143
        <Typography>{parse(settedOverview)}</Typography>
144
 
2909 stevensc 145
        <Row>
2902 stevensc 146
          <Typography>{formattedAddress}</Typography>
2909 stevensc 147
          <Typography variant='body2'> - </Typography>
148
          <Typography variant='body2' onClick={() => setModalToShow('info')}>
2902 stevensc 149
            {labels.personal_info}
2909 stevensc 150
          </Typography>
151
        </Row>
2902 stevensc 152
 
2909 stevensc 153
        <Row>
2902 stevensc 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
          )}
2909 stevensc 164
        </Row>
2902 stevensc 165
 
2909 stevensc 166
        <Row>
167
          {connectionUrl && isAdded && (
168
            <Button variant='primary' onClick={() => displayModal()}>
169
              {labels.cancel}
170
            </Button>
171
          )}
172
          {connectionUrl && !isAdded && (
173
            <Button variant='primary' onClick={connect}>
174
              {labels.connect}
175
            </Button>
176
          )}
177
          {showContact && (
178
            <Button to={linkInmail} LinkComponent={Link} variant='secondary'>
179
              {labels.message}
180
            </Button>
181
          )}
182
        </Row>
2902 stevensc 183
      </Widget.Body>
2909 stevensc 184
      {/*  {isEdit && (
185
        <>
186
          <OverviewModal
187
            isOpen={modalToShow === 'overview'}
188
            overview={settedOverview}
189
            id={profileId}
190
            closeModal={() => closeModal()}
191
            onComplete={(newOverview) => setSettedOverview(newOverview)}
192
          />
193
        </>
194
      )} */}
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