Untitled
unknown
plain_text
3 years ago
2.7 kB
9
Indexable
pass the username prop to ChatMessage as follows:
<ChatMessage
key={message.id}
message={message}
username={username}
/>
import React, { useState } from 'react';
import { Button, Input, IconButton } from '@material-ui/core';
import ImageIcon from '@material-ui/icons/Image';
import firebase from 'firebase/app';
import 'firebase/database';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faCommentAlt } from '@fortawesome/free-solid-svg-icons'
import './ChangeUsernameAndPicture.css';
import { NavLink } from 'react-router-dom';
const ChangeUsernameAndPicture = () => {
const [username, setUsername] = useState('');
const [profilePicture, setProfilePicture] = useState(null);
const [error, setError] = useState(null);
const [success, setSuccess] = useState(false);
const auth = firebase.auth()
const handleProfilePictureChange = (e) => {
setProfilePicture(URL.createObjectURL(e.target.files[0]));
};
const handleUsernameChange = () => {
firebase.database().ref('users/' + firebase.auth().currentUser.uid + '/username').set(username)
.then(() => {
setSuccess(true);
setError(null);
})
.catch((error) => {
setSuccess(false);
setError(error.message);
});
};
return (
<div className="change-username-and-picture">
<h2>Change username and picture</h2>
<Input
type="text"
placeholder="Enter new username"
value={username}
onChange={e => setUsername(e.target.value)}
/>
<input
type="file"
accept="image/*"
onChange={handleProfilePictureChange}
style={{ display: 'none' }}
id="upload-profile-picture"
/>
<label htmlFor="upload-profile-picture">
<IconButton
color="primary"
aria-label="upload picture"
component="span"
>
<ImageIcon />
</IconButton>
</label>
{profilePicture && (
<div>
<img src={profilePicture} alt="Profile" />
</div>
)}
<Button variant="contained" color="primary" onClick={handleUsernameChange}>
Change
</Button>
{error && (
<p className="error">Error: {error}</p>
)}
{success && (
<p className="success">Username changed successfully!</p>
)}
{auth.currentUser && (<p><button className="sign-out" onClick={() => auth.signOut()}>SignOut</button></p>)}
<NavLink to="/"><button className="chat-icon">
<FontAwesomeIcon icon={faCommentAlt} />
</button></NavLink>
</div>
);
};
export default ChangeUsernameAndPicture;Editor is loading...