Hoping to revive this thread because I think it’s a really cool topic and task to accomplish on your own!
To add to the capabilities, I would like to be able to limit members to only one vote per dynamic page. As it stands, anyone can vote any number of times, therefore making the totals compromised.
My approach to this was to create a separate collection titled voteCheck, with two fields: userID and (in my case) classID. I have attached a data insert to the heart click function, with userID being found with current user, and classID being found with the ID field.
I am having issues making this whole process work together. onClick, I get an error that says 'Cannot read property ‘voteCount’ of undefined. And there is the issue of checking for duplicates.
If anyone has gotten anywhere further on this, or have any suggestions, I’d appreciate it! Hoping to work quick with this. All collections and datasets have correct permissions and read/write, here’s my code:
import wixWindow from ‘wix-window’;
import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;
var currentItem, ds;
$w.onReady( function () {
});
export function vectorImage16_click(event, $w) {
wixWindow.lightbox.close();
}
export function ifNull(a){ return (a===undefined) ? 0 : a; }
export function dynamicDataset_onReady(event) {
ds = $w('#dynamicDataset');
currentItem = ds.getCurrentItem();
//Increment the number of views on this item
let views = ifNull(currentItem.viewCount) + 1;
//In case the number of votes is null, set it to zero
let votes = ifNull(currentItem.voteCount);
ds.setFieldValue('viewCount', views);
ds.setFieldValue('voteCount', votes);
ds.save();
}
export function heartEmpty_click(event, $w) {
const classId = $w(‘#dynamicDataset’).getCurrentItem()._id
const userId = wixUsers.currentUser.id
wixData.insert('voteCheck', {'classId': classId, 'userId':userId})
. **catch** ((err) => {
console.log(err)
});
$w('#heartEmpty').hide();
$w('#thanksBox').show('SlideIn');
//increment the number of votes and save it
let votes = ifNull(currentItem.voteCount) + 1;
ds.setFieldValue(‘voteCount’, votes);
ds.save();
}