Enable edit buttons for comments based on current user.

I’m using a repeater to display comments made by different users, I created an edit button in the repeater so every registered user gets to edit their own comments after post if they so choose to, however every registered user is able to click the edit button every time whether they typed the comment or not. How do I enable only the buttons for the comments made by the current user and disable the others? Is that possible?

1 Like

Then you have to loop through data and make sure the button is only visible to the users that created the original comment. If you check the logged in user id and use the forEachItem method in the repeater and inside that method set visible or not on the button depending on if the right user is logged or not.

This is the first time I do this, could you please show me an example?

Hi Manny,
The easiest way to do this would be to do the following:

  1. In your page’s onReady() use currentUser() to determine the current user and save it in a variable https://www.wix.com/code/reference/wix-users.html#currentUser
  2. Set the buttons to be hidden on load
  3. In the repeater’s onItemReady() - use the itemData parameter to inspect your data and determine whether the comment belongs to the user viewing the page. Then show the button using $w('# buttonName ').show() Repeater - Velo API Reference - Wix.com
1 Like

Could you please post an example for me? I’m just learning coding, this is all very new to me.
Thank you.

how do I save the current user to a variable?

I have no idea what I’m doing here. The comments are stored in the “Comunidad” collection (that means each comment has its own ID and URL . The members’ info is stored in the “Afiliados” collection. Could you tell me what’s going on with my code please?

import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;
$w.onReady(function () {
$w(“#button9”).hide();
});
$w.onItemReady(function () {
$w(“#repeater1”).onItemReady( ($w, itemData) => {

        $w(`#button9`).value = itemData.value; 
    const currentUser = wixUsers.currentUser; 
if (currentUser.loggedIn) { 
  currentUser.getEmail().then(email => { 
    wixData.query('Afiliados').eq('email', email).find()  
    .then(() => {  
    $w("#button9").show(); 

});
});
}
});
});

HELP!

Here’s another version of the code:

import wixUsers from 'wix-users'; 
	import wixData from 'wix-data'; 

$w.onReady(function () { 
			     $w("#button9").hide(); 
			});      
	    const currentUser = wixUsers.currentUser; 
if (currentUser.loggedIn) { 
  currentUser.getEmail().then(email => { 
    wixData.query('Afiliados').eq('email', email).find()  
    .then(() => {  

$w(“#repeater1”).onItemReady( ($w, itemData) => {
if (itemData.currentUser.loggedIn) {
$w(‘#button9’).show();
}
});
});
});

Still not working. :frowning:

Anyone?

Hey
I just find that you are missing a closing } in the end. Try this

import wixUsers from 'wix-users';
import wixData from 'wix-data';

$w.onReady(function () {
  $w("#button9").hide();
});
const currentUser = wixUsers.currentUser;
if (currentUser.loggedIn) {
	currentUser.getEmail().then(email => {
  wixData.query('Afiliados').eq('email', email).find()
  .then(() => {
		$w("#repeater1").onItemReady( ($w, itemData) => {
    	if (itemData.currentUser.loggedIn) {
    		$w('#button9').show();
			}
		});
	});
});
}

You might want to run the whole code within the onReady function to make sure the page is fully loaded.
Like the below:

import wixUsers from 'wix-users';
import wixData from 'wix-data';

$w.onReady(function () {
	$w("#button9").hide();
	const currentUser = wixUsers.currentUser;
	if (currentUser.loggedIn) {
		currentUser.getEmail().then(email => {
	  wixData.query('Afiliados').eq('email', email).find()
	  .then(() => {
			$w("#repeater1").onItemReady( ($w, itemData) => {
	    	if (itemData.currentUser.loggedIn) {
	    		$w('#button9').show();
				}
			});
		});
	});
	}
});

It’s still not working. I am soooooo confused! :frowning:

What error do you get in the developer console?

I don’t get any.

It hides all the buttons and not just the ones that do not belong to the current user.

ok, try this

import wixUsers from 'wix-users';
import wixData from 'wix-data';

$w.onReady(function () {
	const currentUser = wixUsers.currentUser;
	if (currentUser.loggedIn) {
		currentUser.getEmail().then(email => {
	  wixData.query('Afiliados').eq('email', email).find()
	  .then(() => {
			$w("#repeater1").onItemReady( ($w, itemData) => {
	    	if (itemData.currentUser.loggedIn) {
	    		$w('#button9').show();
				} else {
					$w("#button9").hide();
				}
			});
		});
	});
	}
});

Also tell me what name is the field in the comments data collection that holds then userid? Or do you just store email?

Still not working… :frowning:
The userid is stored in the ‘Afiliados’ collection as ID, field key _id. The id for each individual comment is stored in the ‘Comunidad’ collection as ID, field key _id as well. These ID’s are created automatically. The userid is also inserted in the ‘Comunidad’ collection in the tittle field everytime a user makes a comment. I hope I’m explaining myself thoroughly.

Ok, but you are filtering the dataset on email ?

wixData.query(‘Afiliados’).eq(‘email’, email).find()

Maybe should be

wixData.query(‘Afiliados’).eq(‘_id’, currentUser).find()

Nope. It’s not working. This is what I did:

import wixUsers from 'wix-users';
import wixData from 'wix-data';

$w.onReady(function () {
	const currentUser = wixUsers.currentUser;
	if (currentUser.loggedIn) {
		currentUser.getEmail().then(currentUser => {
wixData.query('Afiliados').eq('_id', currentUser).find()

	  .then(() => {
			$w("#repeater1").onItemReady( ($w, itemData) => {
	    	if (itemData.currentUser.loggedIn) {
	    		$w('#button9').show();
				} else {
					$w("#button9").hide();
				}
			});
		});
	});
	}
});

This code shows all the buttons but I need it to only show the buttons for the comments the current user made:

import wixUsers from 'wix-users';
import wixData from 'wix-data';

$w.onReady(function () {
	const currentUser = wixUsers.currentUser;
	if (currentUser.loggedIn) {
		currentUser.getEmail().then(email => {
	  wixData.query('Afiliados').eq('email', email).find()
	  .then(() => {
			$w("#repeater1").onItemReady( ($w, itemData) => {
	    	if (itemData.currentUser.loggedIn) {
	    		$w('#button9').show();
				} else {
					$w("#button9").hide();
				}
			});
		});
	});
	}
});

Actually, it’s not working at all.