-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathProfileComponent.jsx
More file actions
183 lines (170 loc) · 5.61 KB
/
ProfileComponent.jsx
File metadata and controls
183 lines (170 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// React Imports
import React, { useContext, useEffect, useState } from 'react';
import { Link } from 'react-router-dom';
// Custom Hooks Imports
import { useNotification, useSession } from '@hooks';
// Material UI Imports
import Box from '@mui/material/Box';
import ContentCopyIcon from '@mui/icons-material/ContentCopy';
import IconButton from '@mui/material/IconButton';
import Typography from '@mui/material/Typography';
import useMediaQuery from '@mui/material/useMediaQuery';
import { useTheme } from '@mui/material/styles';
// Context Imports
import { SignedInUserContext } from '@contexts';
// Util Imports
import { saveToClipboard } from '@utils';
// Component Inputs
import ProfileInputField from './ProfileInputField';
import ProfileEditButtonGroup from './ProfileEditButtonGroup';
import ProfileImageField from './ProfileImageField';
import CreateQRCode from './CreateQRCode';
/**
* UserProfile - Component is a component that renders the user's profile on
* PASS
*
* @memberof Profile
* @name ProfileComponent
* @param {object} Props - Props for ClientProfile component
* @param {object} [Props.contactProfile] - Contact object with data from profile
* @param {string} [Props.webId] - The webId of the contact
* or null if user profile is selected
* @returns {React.JSX.Element} The UserProfile Component
*/
const ProfileComponent = ({ contactProfile, webId }) => {
const { session } = useSession();
const { addNotification } = useNotification();
const { updateProfileInfo, setProfileData, profileData, fetchProfileInfo } =
useContext(SignedInUserContext);
// Public Profile Data
const [profileName, setProfileName] = useState(profileData?.profileInfo?.profileName);
const [nickname, setNickname] = useState(profileData?.profileInfo?.nickname);
const [edit, setEdit] = useState(false);
const loadProfileData = async () => {
const profileDataSolid = await fetchProfileInfo(session.info.webId);
setProfileData(profileDataSolid);
setProfileName(profileDataSolid.profileInfo?.profileName);
setNickname(profileDataSolid.profileInfo?.nickname);
};
const handleCancelEdit = () => {
loadProfileData();
setEdit(!edit);
};
const handleEditInput = () => {
setEdit(!edit);
};
const handleUpdateProfile = async (event) => {
event.preventDefault();
const inputValues = {
profileName,
nickname
};
await updateProfileInfo(session, profileData, inputValues);
loadProfileData();
setEdit(false);
};
useEffect(() => {
loadProfileData();
}, []);
const theme = useTheme();
const isSmallScreen = useMediaQuery(theme.breakpoints.down('sm'));
const signupLink = `${window.location.origin}/signup`;
return (
<Box
sx={{
display: 'flex',
flexDirection: isSmallScreen ? 'column' : 'row',
gap: '15px',
padding: '10px',
boxShadow: '0 6px 20px rgba(0, 0, 0, 0.25)',
borderRadius: '10px'
}}
>
<ProfileImageField loadProfileData={loadProfileData} contactProfile={contactProfile} />
<form
onSubmit={handleUpdateProfile}
style={{
display: 'flex',
flexDirection: 'column',
justifyContent: 'space-between',
padding: '10px'
}}
>
<Box
sx={{
display: 'flex',
flexDirection: 'column',
gap: '10px'
}}
>
{!contactProfile && (
<ProfileEditButtonGroup
edit={edit}
handleCancelEdit={handleCancelEdit}
handleEditInput={handleEditInput}
/>
)}
<ProfileInputField
inputName="Name"
inputValue={
contactProfile
? `${contactProfile?.givenName ?? ''} ${contactProfile?.familyName ?? ''}`
: profileName
}
setInputValue={setProfileName}
edit={edit}
/>
<ProfileInputField
inputName="Nickname"
inputValue={contactProfile ? contactProfile?.nickname : nickname}
setInputValue={setNickname}
edit={edit}
/>
<ProfileInputField
inputName="WebId"
inputValue={webId}
endAdornment={
<IconButton
aria-label="Copy WebId"
edge="end"
onClick={() => {
saveToClipboard(webId, 'webId copied to clipboard', addNotification);
}}
>
<ContentCopyIcon />
</IconButton>
}
/>
</Box>
{!contactProfile && (
<>
<Box sx={{ display: 'flex', flexDirection: 'row', gap: '10px', alignSelf: 'end' }}>
<Typography sx={{ marginTop: '8px' }}>
<Link to={`${signupLink}?webId=${encodeURIComponent(session.info.webId)}`}>
Your Invite Link
</Link>
<IconButton
aria-label="Copy Invite Link"
edge="end"
onClick={() => {
saveToClipboard(
`${signupLink}?webId=${encodeURIComponent(session.info.webId)}`,
'Invite link copied to clipboard',
addNotification
);
}}
>
<ContentCopyIcon />
</IconButton>
</Typography>
</Box>
<Box sx={{ alignSelf: 'end' }}>
<CreateQRCode webId={webId} />
</Box>
</>
)}
</form>
</Box>
);
};
export default ProfileComponent;