Files
cinny/src/app/organisms/profile-editor/ProfileEditor.jsx
T

129 lines
3.8 KiB
React
Raw Normal View History

import React, { useState, useEffect, useRef } from 'react';
2021-09-09 00:47:26 -05:00
import PropTypes from 'prop-types';
2022-03-21 09:46:11 +05:30
import { twemojify } from '../../../util/twemojify';
2021-09-09 00:47:26 -05:00
import initMatrix from '../../../client/initMatrix';
import colorMXID from '../../../util/colorMXID';
2022-03-21 09:46:11 +05:30
import Text from '../../atoms/text/Text';
import IconButton from '../../atoms/button/IconButton';
2021-09-09 00:47:26 -05:00
import Button from '../../atoms/button/Button';
2021-09-12 22:25:58 -05:00
import ImageUpload from '../../molecules/image-upload/ImageUpload';
2021-09-09 00:47:26 -05:00
import Input from '../../atoms/input/Input';
2022-03-21 09:46:11 +05:30
import PencilIC from '../../../../public/res/ic/outlined/pencil.svg';
2021-09-09 00:47:26 -05:00
import './ProfileEditor.scss';
// TODO Fix bug that prevents 'Save' button from enabling up until second changed.
2022-03-21 09:46:11 +05:30
function ProfileEditor({ userId }) {
const [isEditing, setIsEditing] = useState(false);
2021-09-09 00:47:26 -05:00
const mx = initMatrix.matrixClient;
2022-03-21 09:46:11 +05:30
const user = mx.getUser(mx.getUserId());
2021-09-09 00:47:26 -05:00
const displayNameRef = useRef(null);
2022-03-21 09:46:11 +05:30
const [avatarSrc, setAvatarSrc] = useState(user.avatarUrl ? mx.mxcUrlToHttp(user.avatarUrl, 80, 80, 'crop') : null);
const [username, setUsername] = useState(user.displayName);
2021-09-09 00:47:26 -05:00
const [disabled, setDisabled] = useState(true);
useEffect(() => {
2022-03-21 09:46:11 +05:30
let isMounted = true;
mx.getProfileInfo(mx.getUserId()).then((info) => {
2022-03-21 09:46:11 +05:30
if (!isMounted) return;
setAvatarSrc(info.avatar_url ? mx.mxcUrlToHttp(info.avatar_url, 80, 80, 'crop') : null);
2022-03-21 09:46:11 +05:30
setUsername(info.displayname);
});
2022-03-21 09:46:11 +05:30
return () => {
isMounted = false;
};
}, [userId]);
2022-03-21 09:46:11 +05:30
const handleAvatarUpload = (url) => {
2021-09-13 12:27:55 +05:30
if (url === null) {
2022-04-18 08:55:16 +05:30
if (confirm('Are you sure that you want to remove avatar?')) {
2021-09-13 12:27:55 +05:30
mx.setAvatarUrl('');
setAvatarSrc(null);
}
return;
}
mx.setAvatarUrl(url);
setAvatarSrc(mx.mxcUrlToHttp(url, 80, 80, 'crop'));
2022-03-21 09:46:11 +05:30
};
2021-09-09 00:47:26 -05:00
2022-03-21 09:46:11 +05:30
const saveDisplayName = () => {
const newDisplayName = displayNameRef.current.value;
if (newDisplayName !== null && newDisplayName !== username) {
mx.setDisplayName(newDisplayName);
2022-03-21 09:46:11 +05:30
setUsername(newDisplayName);
2021-09-09 00:47:26 -05:00
setDisabled(true);
2022-03-21 09:46:11 +05:30
setIsEditing(false);
2021-09-09 00:47:26 -05:00
}
2022-03-21 09:46:11 +05:30
};
2021-09-09 00:47:26 -05:00
2022-03-21 09:46:11 +05:30
const onDisplayNameInputChange = () => {
setDisabled(username === displayNameRef.current.value || displayNameRef.current.value == null);
2022-03-21 09:46:11 +05:30
};
const cancelDisplayNameChanges = () => {
displayNameRef.current.value = username;
onDisplayNameInputChange();
2022-03-21 09:46:11 +05:30
setIsEditing(false);
};
2021-09-09 00:47:26 -05:00
2022-03-21 09:46:11 +05:30
const renderForm = () => (
<form
2022-03-21 09:46:11 +05:30
className="profile-editor__form"
style={{ marginBottom: avatarSrc ? '24px' : '0' }}
onSubmit={(e) => { e.preventDefault(); saveDisplayName(); }}
>
2022-03-21 09:46:11 +05:30
<Input
label={`Display name of ${mx.getUserId()}`}
onChange={onDisplayNameInputChange}
value={mx.getUser(mx.getUserId()).displayName}
forwardRef={displayNameRef}
/>
<Button variant="primary" type="submit" disabled={disabled}>Save</Button>
<Button onClick={cancelDisplayNameChanges}>Cancel</Button>
</form>
);
const renderInfo = () => (
<div className="profile-editor__info" style={{ marginBottom: avatarSrc ? '24px' : '0' }}>
<div>
<Text variant="h2" primary weight="medium">{twemojify(username)}</Text>
<IconButton
src={PencilIC}
size="extra-small"
tooltip="Edit"
onClick={() => setIsEditing(true)}
/>
</div>
<Text variant="b2">{mx.getUserId()}</Text>
</div>
);
return (
<div className="profile-editor">
2021-09-13 12:27:55 +05:30
<ImageUpload
text={username}
2022-03-21 09:46:11 +05:30
bgColor={colorMXID(userId)}
2021-09-13 12:27:55 +05:30
imageSrc={avatarSrc}
onUpload={handleAvatarUpload}
onRequestRemove={() => handleAvatarUpload(null)}
/>
2022-03-21 09:46:11 +05:30
{
isEditing ? renderForm() : renderInfo()
}
</div>
2021-09-09 00:47:26 -05:00
);
}
ProfileEditor.defaultProps = {
userId: null,
};
ProfileEditor.propTypes = {
userId: PropTypes.string,
};
export default ProfileEditor;