Skip to content

Commit

Permalink
Refactor loadContacts() function
Browse files Browse the repository at this point in the history
  • Loading branch information
astik-dev committed Apr 24, 2024
1 parent cebbb11 commit 1427a1a
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/js/modules/dataLoaders/loadContacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,31 @@ import { removeEmptyItems } from "./removeEmptyItems.js";
import imageCreator from "../imageCreator.js";


export function loadContacts(contacts) {
const contactsItems = dqs(".contacts__items");
function contactsItemHTML({link, title, img, imgWEBP}) {

removeEmptyItems(".contacts__item_empty", contactsItems);
const fallback = `contacts/${img}`;

contacts.forEach((contact) => {
const imgElem = imgWEBP
? imageCreator.newWebpPic("external", `contacts/${imgWEBP}`, fallback, title)
: imageCreator.newImg("external", fallback, title);

const fallback = `contacts/${contact.img}`, alt = contact.title;
return `
<a href="${link}" target="_blank" title="${title}" class="contacts__item">
${imgElem}
</a>
`;
}

const imgElem = contact.imgWEBP ?
imageCreator.newWebpPic("external", `contacts/${contact.imgWEBP}`, fallback, alt) :
imageCreator.newImg("external", fallback, alt);
export function loadContacts(contacts) {

const contactsItems = dqs(".contacts__items");

let currentContact = `<a href="${contact.link}" target="_blank" title="${contact.title}" class="contacts__item">
${imgElem}
</a>`
removeEmptyItems(".contacts__item_empty", contactsItems);

contactsItems.insertAdjacentHTML("beforeend", currentContact);
contacts.forEach((contact) => {
contactsItems.insertAdjacentHTML(
"beforeend",
contactsItemHTML(contact)
);
});
}

0 comments on commit 1427a1a

Please sign in to comment.