Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnematias/db961d0033a9d96f8deaf9e67a275f90 to your computer and use it in GitHub Desktop.
Save johnematias/db961d0033a9d96f8deaf9e67a275f90 to your computer and use it in GitHub Desktop.
This will add meta data to the member profile in Memberstack which allows us to hide or show the mark as complete form.
<script src="https://api.memberstack.io/static/memberstack.js?webflow" data-memberstack-id="GET_FROM_MEMBERSTACK"></script>
<script>
MemberStack.onReady.then(async function(member) {
// Checks if member is logged in
if(member.loggedIn){
const metadata = await member.getMetaData();
// If no metadata.liked_topics exists, create it in MemberStack.
metadata.liked_topics = metadata.liked_topics || [];
// If no metadata.completed_topics exists, create it in MemberStack.
metadata.completed_topics = metadata.completed_topics || [];
// Defines the webflow ID to a const of itemID (Pull this from the CMS)
const itemID = "GET_FROM_CMS";
// If they have the item ID in their profile, hide the form, show the appropriate button
if(metadata.liked_topics.includes(itemID)){
document.getElementById('like-topic').style.display = 'none';
document.getElementById('unlike-topic').style.display = 'block';
};
if(metadata.completed_topics.includes(itemID)){
document.getElementById('complete-topic').style.display = 'none';
document.getElementById('uncomplete-topic').style.display = 'block';
};
// When the like-topic button is clicked, if the itemID doesn't exist on their profile
// add it, then push the metadata to MemberStack.
$('#like-topic').click(function(){
if(metadata.liked_topics.indexOf(itemID) === -1){
metadata.liked_topics.push(itemID);
member.updateMetaData(metadata);
setTimeout(function(){ location.reload(); }, 3000);
}
});
// When the complete-topic button is clicked, if the itemID doesn't exist on their profile
// add it, then push the metadata to MemberStack.
$('#complete-topic').click(function(){
if(metadata.completed_topics.indexOf(itemID) === -1){
metadata.completed_topics.push(itemID);
member.updateMetaData(metadata);
setTimeout(function(){ location.reload(); }, 3000);
}
});
// When the unlike-topic button is clicked, if the itemID exists on their profile
// remove it, then push the metadata to MemberStack.
$('#unlike-topic').click(function(){
if(metadata.liked_topics.includes(itemID)){
const itemIndex = metadata.liked_topics.indexOf(itemID);
metadata.liked_topics.splice(itemIndex);
member.updateMetaData(metadata);
setTimeout(function(){ location.reload(); }, 3000);
}
});
// When the uncomplete-topic button is clicked, if the itemID exists on their profile
// remove it, then push the metadata to MemberStack.
$('#uncomplete-topic').click(function(){
if(metadata.completed_topics.includes(itemID)){
const itemIndex = metadata.completed_topics.indexOf(itemID);
metadata.completed_topics.splice(itemIndex);
member.updateMetaData(metadata);
setTimeout(function(){ location.reload(); }, 3000);
}
});
}
});
</script>
@johnematias
Copy link
Author

Setting up to add and remove items from metadata.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment