Reputation: 61
So I've created a function that will send a query to the db to check and see if person is the admin, if they are it should open the admin page, if not it'll display just a simple welcome message. Issue I'm having is when I set the onClick onto the same button that should redirect, nothing will render, if I remove the onClick it'll show up no matter if the person set is an admin. I'm needing help to figure out the best way to get this to work or best way to check if person is an admin before rendering the fields to change users etc. Here's the check I have written out along with it's function.
<Link to="/">
<button type="button" className="thecartbtn" onClick={getAdmin} >
Main
</button>
</Link>
async function getAdmin(event) {
event.preventDefault()
const username = localStorage.getItem("user")
const response = await getAdminInfo(username)
const resp = response.data.name
console.log(resp)
resp.forEach(function(item) {
for (const [key, value] of Object.entries(item)) {
setMain(`${value}`);
}
})
console.log(main)
}
Upvotes: 0
Views: 31
Reputation: 61
Put the getAdmin function inside of useEffect and viola, issue resolved.
Upvotes: 1