From 0af586d87dd23bdcc69dfbc2ad7f05e1617b36b8 Mon Sep 17 00:00:00 2001 From: Dwij13 Date: Sun, 30 Jun 2024 22:37:00 +0530 Subject: [PATCH] Save link of your social handles --- src/code/index.js | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src/code/index.js b/src/code/index.js index eec9bc0..41d150d 100644 --- a/src/code/index.js +++ b/src/code/index.js @@ -62,7 +62,7 @@ function getSocialLinks() { .then(jsonData => { Object.keys(jsonData).slice(1).forEach(key => { socialLinks[key] = jsonData[key]; - createSocialLink(key, jsonData[key]); + createSocialLink(key); }); return socialLinks; }) @@ -89,13 +89,13 @@ function createImage(key) { } const socialLinksContainer = document.getElementById("socialLinks"); -function createSocialLink(key, value) { +function createSocialLink(key) { const li = document.createElement("li"); const img = createImage(key); img.onload = () => { li.appendChild(img); li.addEventListener("click", () => { - navigator.clipboard.writeText(value); // Copy the value to clipboard + navigator.clipboard.writeText(socialLinks[key]); // Copy the value to clipboard showCopyMessage(key); }); socialLinksContainer.appendChild(li); @@ -504,4 +504,37 @@ function addDnDHandlers(elem) { const saveBtn = document.getElementById("save-btn"); saveBtn.addEventListener("click", () => { const links = Array.from(document.querySelectorAll(".link-box input")); -}); \ No newline at end of file +}); +function addLinkBoxOnSave() { + const linkInputBox = document.querySelectorAll(".link-box input"); + if (linkInputBox) { + linkInputBox.forEach((linkInput) => { + let inputURL = linkInput.value; + if (isValidURL(inputURL)) { + //if the link box already exists in home then update the existing one, else create a new box + const res = getSocialName(inputURL); + const key = res.key; + const value = inputURL; + let linkExists = false; + const homeSocials = document.querySelectorAll(".social-logo"); + homeSocials.forEach((social) => { + if (key == social.alt) { + socialLinks[key] = value; + console.log(socialLinks); + linkExists = true; + return; + } + }); + //else create a link box on home page + if (!linkExists) { + socialLinks[key] = value; + createSocialLink(key); + } + //if not a valid URL + } else { + console.log("invalid URL"); + } + }); + } + } + saveBtn.addEventListener("click", addLinkBoxOnSave); \ No newline at end of file