From 967f1273b9bd4185d3a930063660cf40040be1ba Mon Sep 17 00:00:00 2001 From: Eunice Shobowale Date: Thu, 31 Oct 2024 19:34:20 -0400 Subject: [PATCH] Updating club.js using Singleton Method Design Pattern --- club.js | 50 +++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/club.js b/club.js index d230216..41de1a4 100644 --- a/club.js +++ b/club.js @@ -1,28 +1,36 @@ -// Function to handle the profile picture change -function changeProfilePicture(event) { - const reader = new FileReader(); - const file = event.target.files[0]; +class CreatingProfilePicture { + constructor() { + if (CreatingProfilePicture.instance) { + return CreatingProfilePicture.instance; + } - reader.onload = function () { - const profilePic = document.getElementById("club-profile-pic"); - profilePic.src = reader.result; - - // Save the image data to localStorage - localStorage.setItem("profilePicture", reader.result); - }; + CreatingProfilePicture.instance = this; + } - if (file) { - reader.readAsDataURL(file); + loadProfilePicture() { + const savedProfilePic = localStorage.getItem("profilePicture"); + if (savedProfilePic) { + document.getElementById("club-profile-pic").src = savedProfilePic; + } } -} -// Function to load the profile picture from localStorage on page load -function loadProfilePicture() { - const savedProfilePic = localStorage.getItem("profilePicture"); - if (savedProfilePic) { - document.getElementById("club-profile-pic").src = savedProfilePic; + changeProfilePicture(event) { + const reader = new FileReader(); + const file = event.target.files[0]; + + reader.onload = () => { + const profilePic = document.getElementById("club-profile-pic"); + profilePic.src = reader.result; + + localStorage.setItem("profilePicture", reader.result); + }; + + if (file) { + reader.readAsDataURL(file); + } } } -// Load the profile picture when the page is loaded -window.onload = loadProfilePicture; +// Singleton instance +const CreatingProfilePicture = new CreatingProfilePicture(); +window.onload = () => CreatingProfilePicture.loadProfilePicture();