Hi,
I would like to insert a custom like function on my Website. So i used this code to start (https://www.wix.com/corvid/example/custom-like-button). But I dont want a Blog on my Website because I’m building something like a Social Network. This is the reason why i can not use the first part of this code.
At this point right now i already created a like function with the code below but the likes per user are not saved so far. This means if a user leaves this page and rejoins he can like this post again.
To solve this problem I created a second database (‘PostLikeData’) with the columns ‘UserId’ and ‘Post’.
In ‘UserId’ I put the userId and the column ‘Post’ is a reference-field which is linked to the database with all the posts inside. But it doesn’t work like i want. Through the value of currentPostId the item in this column should automatically be linked to the Post which was liked by the user.
Is there anybody who knows how to realize this?
import wixData from ‘wix-data’ ;
import wixUsers from ‘wix-users’ ;
let currentLikeButtonState
const likeButtonStates = {
default : ‘default’ ,
active: ‘active’ ,
}
$w.onReady( () => {
$w( “#dynamicDataset” ).onReady( () => {
let numberOfLikes = $w( ‘#dynamicDataset’ ).getCurrentItem().likes;
let currentPostId = $w( ‘#dynamicDataset’ ).getCurrentItem()._id;
//____________________________________________________________________
$w( ‘#likeButtonDefault’ ).onClick( async () => {
numberOfLikes = numberOfLikes + 1 ;
$w( '#dynamicDataset' ).setFieldValue( 'likes' , (numberOfLikes));
$w( '#dynamicDataset' ).save()
console.log( "likes:" , numberOfLikes)
$w( '#likeButtonActive' ).show()
$w( '#likeButtonDefault' ).hide()
})
//____________________________________________________________________
$w( ‘#likeButtonActive’ ).onClick( async () => {
numberOfLikes = numberOfLikes - 1 ;
$w( ‘#dynamicDataset’ ).setFieldValue( ‘likes’ , (numberOfLikes));
$w( ‘#dynamicDataset’ ).save()
console.log( “likes:” , numberOfLikes);
$w( '#likeButtonActive' ).hide()
$w( '#likeButtonDefault' ).show()
})
function setNumberOfLikes() {
$w( ‘#likesCounter’ ).text = numberOfLikes.toString()
}
} );
function setLikeButtonStateUI(state) {
switch (state) {
case likeButtonStates. default :
$w( ‘#likeButtonDefault’ ).show()
$w( ‘#likeButtonActive’ ).hide()
break
case likeButtonStates.active:
$w( ‘#likeButtonActive’ ).show()
$w( ‘#likeButtonDefault’ ).hide()
// Hide active GIF after animation ends
setTimeout(() => {
$w( ‘#likeButtonActive’ ).hide()
}, 1000 )
break
}
}
})