Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3446 | Rev 3448 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3445 stevensc 1
import React, { useState, useEffect, useCallback } from 'react';
3444 stevensc 2
import { Link, useLocation } from 'react-router-dom';
3
import { useDispatch, useSelector } from 'react-redux';
4
import { Box, Button, Typography } from '@mui/material';
1847 stevensc 5
 
3444 stevensc 6
import { axios, parse } from '@utils';
7
import { addNotification } from '@store/notification/notification.actions';
1847 stevensc 8
 
3444 stevensc 9
import Row from '@components/common/Row';
10
import Cover from '@components/UI/cover/Cover';
11
import Widget from '@components/UI/Widget';
12
import Avatar from '@components/common/avatar';
13
import ProfileModal from './ProfileModal';
14
import ConfirmModal from '@components/modals/ConfirmModal';
1847 stevensc 15
 
16
const ProfileCard = ({
2905 stevensc 17
  cover,
3444 stevensc 18
  avatar,
19
  name,
20
  description,
21
  address,
22
  coverUrl,
23
  avatarUrl,
24
  coverSize,
25
  avatarSize,
26
  requestConnection,
27
  linkRequest,
28
  linkCancel,
29
  linkInmail,
30
  following,
31
  totalConnections,
2905 stevensc 32
  facebook,
1847 stevensc 33
  twitter,
3446 stevensc 34
  instagram,
35
  edit
1847 stevensc 36
}) => {
3444 stevensc 37
  const [isConnecting, setIsConnecting] = useState(false);
38
  const [isAdded, setIsAdded] = useState(!requestConnection);
39
  const [connectionUrl, setConnectionUrl] = useState(linkRequest || linkCancel || '');
3446 stevensc 40
  const [showConfirmationModal, setShowConfirmationModal] = useState(false);
3432 stevensc 41
  const labels = useSelector(({ intl }) => intl.labels);
42
  const { pathname } = useLocation();
43
  const dispatch = useDispatch();
1847 stevensc 44
 
3446 stevensc 45
  const toggleConfirmationModal = () => setShowConfirmationModal(!showConfirmationModal);
46
 
3444 stevensc 47
  const fetchConnectionUrls = useCallback(async () => {
1847 stevensc 48
    try {
3432 stevensc 49
      const response = await axios.get(pathname);
50
      const { link_request, link_cancel } = response.data;
3444 stevensc 51
      setConnectionUrl(link_request || link_cancel || '');
52
    } catch (error) {
53
      dispatch(addNotification({ style: 'danger', msg: `Error: ${error.message}` }));
54
    }
55
  }, [pathname, dispatch]);
1847 stevensc 56
 
3444 stevensc 57
  useEffect(() => {
58
    if (!linkRequest && !linkCancel) {
59
      fetchConnectionUrls();
1847 stevensc 60
    }
3444 stevensc 61
  }, [fetchConnectionUrls, linkRequest, linkCancel]);
1847 stevensc 62
 
3444 stevensc 63
  const handleConnect = useCallback(async () => {
64
    setIsConnecting(true);
1847 stevensc 65
    try {
3432 stevensc 66
      const response = await axios.post(connectionUrl);
67
      const { data, success } = response.data;
1847 stevensc 68
 
69
      if (!success) {
3444 stevensc 70
        dispatch(addNotification({ style: 'danger', msg: data }));
71
      } else {
72
        dispatch(addNotification({ style: 'success', msg: data }));
73
        setIsAdded(!isAdded);
3446 stevensc 74
        if (showConfirmationModal) {
75
          toggleConfirmationModal();
3444 stevensc 76
        }
77
        fetchConnectionUrls();
1847 stevensc 78
      }
79
    } catch (error) {
3444 stevensc 80
      dispatch(addNotification({ style: 'danger', msg: `Error: ${error.message}` }));
81
    } finally {
82
      setIsConnecting(false);
1847 stevensc 83
    }
3446 stevensc 84
  }, [connectionUrl, isAdded, dispatch, showConfirmationModal, fetchConnectionUrls]);
1847 stevensc 85
 
86
  return (
2902 stevensc 87
    <Widget>
3446 stevensc 88
      <Cover cover={cover} size={coverSize} url={coverUrl} edit={edit} />
3047 stevensc 89
 
2906 stevensc 90
      <Widget.Body>
3446 stevensc 91
        <Box sx={{ alignItems: 'center', mt: '-40px' }}>
3445 stevensc 92
          <Avatar
93
            src={avatar}
94
            alt={name}
95
            size={avatarSize}
96
            uploadUrl={avatarUrl}
3447 stevensc 97
            badgeStyles={{ mt: '-75px' }}
3445 stevensc 98
            styles={{ width: 150, height: 150 }}
3446 stevensc 99
            edit={edit}
3445 stevensc 100
          />
101
        </Box>
3444 stevensc 102
        <Typography variant='h2'>{name}</Typography>
103
        <Typography>{parse(description)}</Typography>
104
        <Typography>{address}</Typography>
105
        <Typography variant='overline'>{labels.personal_info}</Typography>
2909 stevensc 106
        <Row>
3444 stevensc 107
          {totalConnections && (
108
            <Link to='/connection/my-connections'>
109
              <Typography variant='overline'>{`${totalConnections} conexiones`}</Typography>
2902 stevensc 110
            </Link>
3444 stevensc 111
          )}
112
          {following && (
2902 stevensc 113
            <Link onClick={(e) => e.preventDefault()}>
3444 stevensc 114
              <Typography variant='overline'>{`${following} siguiendo`}</Typography>
2902 stevensc 115
            </Link>
3444 stevensc 116
          )}
2909 stevensc 117
        </Row>
118
        <Row>
119
          {connectionUrl && isAdded && (
3446 stevensc 120
            <Button color='primary' onClick={toggleConfirmationModal} disabled={isConnecting}>
2909 stevensc 121
              {labels.cancel}
122
            </Button>
123
          )}
124
          {connectionUrl && !isAdded && (
3444 stevensc 125
            <Button color='primary' onClick={handleConnect} disabled={isConnecting}>
2909 stevensc 126
              {labels.connect}
127
            </Button>
128
          )}
3444 stevensc 129
          {linkInmail && (
130
            <Button to={linkInmail} LinkComponent={Link} color='secondary'>
2909 stevensc 131
              {labels.message}
132
            </Button>
133
          )}
134
        </Row>
2902 stevensc 135
      </Widget.Body>
1847 stevensc 136
 
2902 stevensc 137
      <ProfileModal
3444 stevensc 138
        fullName={name}
1847 stevensc 139
        facebook={facebook}
140
        twitter={twitter}
141
        instagram={instagram}
142
        following={following}
3444 stevensc 143
        formatted_address={address}
144
        overview={description}
1847 stevensc 145
        total_connections={totalConnections}
146
      />
147
      <ConfirmModal
3446 stevensc 148
        show={showConfirmationModal}
149
        onClose={toggleConfirmationModal}
3444 stevensc 150
        onAccept={handleConnect}
151
        isLoading={isConnecting}
1847 stevensc 152
      />
2902 stevensc 153
    </Widget>
3432 stevensc 154
  );
155
};
1847 stevensc 156
 
3445 stevensc 157
export default ProfileCard;