My main objective is to conditionally render a "Create profile" button if the user does not have a profile in firebase, or an "Edit profile" button if the user has a profile.
I am not having issues rendering the edit profile button. I am checking if the user has a profile by comparing the auth users "uid" with the profile's "uid." If they match, then the edit button will render.
My problem is that, when the edit button renders, the create button still appears as well, but it should disappear since I am conditionally rendering the two.
What am I missing here?
EDIT 3/23/2018
I've figured out the problem but I still don't have a solution. The problem is that the map function is looping through the 'Profiles' array and is looking for a 'uid' equal to to the logged in users 'uid'.
But if the 'uid' doesn't match the logged in users 'uid,' the create button will still render since there are other profiles in the 'Profiles' array which have uid's not equal to the logged in users 'uid.'
So i guess my other question would be,
how can I check if a logged in user does not have data in an array and/or Firebase db?
Here's my code:
I have database named 'Profiles' which I am pulling information from.
"Profiles" : {
"-L7p-wZNcvgBBTkvmn7I" : {
"about" : "I'm a full stack web developer with many skills",
"email" : "email@gmail.com",
"frameworkOne" : "react",
"frameworkThree" : "bootstrap",
"frameworkTwo" : "javascript",
"name" : "Dylan Prem",
"projectInfo" : "You're on it!",
"projectLink" : "https://hiremoredevs.com",
"projectName" : "HireMoreDevs",
"uid" : "ABCDEFG1234567"
}
}
The react component:
class ProfileButtonToggle extends Component {
constructor(props){
super(props);
this.state = {
authUser:null,
Profiles:[]
}
}
componentDidMount(){
const profilesRef = firebase.database().ref('Profiles');
profilesRef.once('value', (snapshot) => {
let Profiles = snapshot.val();
let newState = [];
for (let profile in Profiles){
newState.push({
id: profile,
uid:Profiles[profile].uid,
});
}
this.setState({
Profiles: newState
});
});
firebase.auth().onAuthStateChanged((authUser) => {
if (authUser) {
this.setState({ authUser });
}
});
}
render() {
return (
<div>
{this.state.authUser ?
<div>
{this.state.Profiles.map((profile) => {
return(
<div key={profile.id}>
{profile.uid === this.state.authUser.uid ?
<Nav>
<NavItem>
<Link className='btn yellow-button' to={`/edit/${profile.id}`}>Edit Profile</Link>
</NavItem>
</Nav>
:
<Nav>
<NavItem>
<Link className='btn yellow-button' to={routes.CREATE_PROFILE}>Create Profile</Link>
</NavItem>
</Nav>
}
</div>
);
})}
</div>
:
null
}
</div>
);
}
}
export default ProfileButtonToggle;