Hi everyone,
I’m trying to create a website where only logged-in users can vote for as many items as they want, but only once. It’s the same idea as a social media site; you can ‘like’ whatever you’d like, but only once.
Here’s what works:
Only logged-in users can access the website.
Once users have clicked the #voteButton, the specific item they ‘liked’ is incremented by one and the #voteButton is hidden
What I need help with:
Once a user clicks on any vote button, it increments it by one, but it makes the button disappear for all items rather than the specific item that was clicked → I want to make it so only the #voteButton on the specific item that was clicked disappears.
Once an item is clicked, the vote button disappears. However, once the page is reloaded, the vote button appears once again. It’s absolutely mandatory for the vote button to be permanently hidden on an item that was already ‘liked’ or ‘voted’ in order for users to be able to vote once per item.
Last but not least, I need all the data on this database to be automatically deleted at the end of each day at midnight.
export function voteButton_click(event) {
let $item = $w.at(event.context);
let currentItem = $item("#dataset1").getCurrentItem();
let currentVotes = Number(currentItem.votes);
$item("#dataset1").setFieldValue("votes",currentVotes + 1);
$item("#dataset1").save();
$w("#voteButton").hide();
}
Once a user clicks on any vote button, it increments it by one, but it makes the button disappear for all items rather than the specific item that was clicked → I want to make it so only the #voteButton on the specific item that was clicked disappears.
WRONG !!!
$w("#voteButton").hide();
RIGHT !!!
$item("#voteButton").hide();
Once an item is clicked, the vote button disappears. However, once the page is reloaded, the vote button appears once again. It’s absolutely mandatory for the vote button to be permanently hidden on an item that was already ‘liked’ or ‘voted’ in order for users to be able to vote once per item.
$w.onReady(async function() {console.log("Dataset ready...");
$w('#dataset1').onReady(()=>{
//What shall happen here???
//You want to be the vote-Button hidden on start when a vote already have been done.
//So you will have to code it here when your dataset gets ready... what shall happen ???
// CODE IT...
// CODE IT...
// CODE IT...
// CODE IT...
// CODE IT...
// CODE IT...
// CODE IT...
// CODE IT...
//Your click-function here ---> only starts when clicking onto your VOTE-BUTTON
$w('#voteButton').onClick((event)=>{
let $item = $w.at(event.context);
let currentItem = $item("#dataset1").getCurrentItem();
let currentVotes = Number(currentItem.votes);
$item("#dataset1").setFieldValue("votes",currentVotes + 1);
$item("#dataset1").save();
$item("#voteButton").hide();
});
});
});
You will have to make an if-else-query and code it.
Another idea would maybe be, to generate a further data-field inside of your DATABASE (BOOLEAN-VALUE —> TRUE/FALSE) and set-up a filter inside of the PROPERTY-PANEL for identification-purposes, but since i do not use the property-panel at all (i code everything i need), so i can’t really help here, or i would have to reconstruct your situation to be able to help, this would take a lot of my time.
@russian-dima
Thank you!
The code correction worked.
Now, I’m just not sure how to code something that will identify wether the user has voted or not for each of the items.
Could you direct me in a certian direction or assist me with the code?
Version-1:
Generating a further database with the following DATA-FIELDS:
User-ID → string-field
STATE → boolean-field
QUOTE-ID → Number-Field which will hold the selected index-number inside of repeater, which also represents the index of selected row inside your MAIN-DATABASE.
In this DB you will save all the the mentioned data above each time a user clicks the VOTE-BUTTON.
So you generate a function which would run every-time when VOTE-BUTTON has been clicked and do the check if a value already exists for a specific QUOTE related to a specific User-ID, maybe the boolean-field is even uneccessary.
Version-2:
You generate inside of your MAIN-DATABASE a new ARRAY-FIELD which will hold all the User-IDs for each of the QUOTES existing inside of your DB, and always check this field, when click the VOTE-Button.
You click the VOTE-Button
Check-function starts
Filtration of ARRAY (which is full of User-IDS) runs
If found current User-ID inside the ARRAY → ???
Else —> ???
And now try to write the check-function…
function checkUser() {
//generate your code here.....
//generate your code here.....
//generate your code here.....
//generate your code here.....
//generate your code here.....
}