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