<!-- 💙 MEMBERSCRIPT #200 v0.1 💙 UPDATE DATA TABLE RECORD IN LOCAL STORAGE ON BUTTON CLICK (USE THIS SCRIPT TOGETHER WITH MEMBERSCRIPT #198 AND #199) -->
<script>
document.addEventListener("DOMContentLoaded", function() {
// Expose reusable function so other scripts (like #198) can trigger an update
window.ms200UpdateLocalStorage = async function(tableName, storageKey, delay = 0) {
const memberstack = window.$memberstackDom;
if (!memberstack) {
console.error("Memberstack not initialized");
return;
}
// Optional delay (defaults to 0 for instant updates)
if (delay > 0) {
await new Promise(resolve => setTimeout(resolve, delay));
}
try {
// Get current member
const memberResult = await memberstack.getCurrentMember();
const member = memberResult?.data || memberResult;
if (!member || !member.id) {
console.error("No member found. Please log in.");
return;
}
// Use provided table name or default
const finalTableName = tableName || "member_data_snapshots";
const finalStorageKey = storageKey || "memberDataTable";
// Query data table records for this member (with fresh query)
const queryResult = await memberstack.queryDataRecords({
table: finalTableName,
query: {
take: 100
}
});
// Normalize records array (handle different possible shapes)
const allRecords = queryResult?.data?.records || queryResult?.data || [];
// Filter records by current member
const memberRecords = allRecords.filter(function(record) {
const recordMember = record?.data?.member;
return (
recordMember === member.id ||
(recordMember && recordMember.id === member.id)
);
});
// Get the most recent record (by timestamp or creation date)
let latestRecord = null;
if (memberRecords.length > 0) {
// Sort by timestamp if available, otherwise by creation date
memberRecords.sort(function(a, b) {
const aTime = a.data?.timestamp || a.createdAt || 0;
const bTime = b.data?.timestamp || b.createdAt || 0;
return bTime - aTime; // Most recent first
});
latestRecord = memberRecords[0];
}
// Save the entire data table record to local storage
if (latestRecord) {
// Save the complete record data to local storage
localStorage.setItem(finalStorageKey, JSON.stringify(latestRecord.data));
console.log(`Data table record saved to localStorage with key: ${finalStorageKey}`);
return true;
} else {
console.warn("No data table record found for current member");
return false;
}
} catch (error) {
console.error("Error updating data table record in localStorage:", error);
return false;
}
};
// Set up button click handlers
const updateButtons = document.querySelectorAll('[data-ms-code-update="datatable"]');
updateButtons.forEach(button => {
button.addEventListener("click", async function() {
// Get table name and storage key from button attributes
const tableName = button.getAttribute("data-ms-code-table") || "member_data_snapshots";
const storageKey = button.getAttribute("data-ms-code-storage-key") || "memberDataTable";
// Call the reusable function with 0 delay for instant update
await window.ms200UpdateLocalStorage(tableName, storageKey, 0);
});
});
});
</script>
Customer Showcase
Have you used a Memberscript in your project? We’d love to highlight your work and share it with the community!
"We've been using Memberstack for a long time, and it has helped us achieve things we would have never thought possible using Webflow. It's allowed us to build platforms with great depth and functionality and the team behind it has always been super helpful and receptive to feedback"
Jamie Debnam
39 Digital
"Been building a membership site with Memberstack and Jetboost for a client. Feels like magic building with these tools. As someone who’s worked in an agency where some of these apps were coded from scratch, I finally get the hype now. This is a lot faster and a lot cheaper."
Félix Meens
Webflix Studio
"One of the best products to start a membership site - I like the ease of use of Memberstack. I was able to my membership site up and running within a day. Doesn't get easier than that. Also provides the functionality I need to make the user experience more custom."
Eric McQuesten
Health Tech Nerds
Off World Depot
"My business wouldn't be what it is without Memberstack. If you think $30/month is expensive, try hiring a developer to integrate custom recommendations into your site for that price. Incredibly flexible set of tools for those willing to put in some minimal efforts to watch their well put together documentation."
Riley Brown
Off World Depot
"The Slack community is one of the most active I've seen and fellow customers are willing to jump in to answer questions and offer solutions. I've done in-depth evaluations of alternative tools and we always come back to Memberstack - save yourself the time and give it a shot."