Skip to content

Commit

Permalink
Add filters and handleFilterClick
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilles-Lucien committed Dec 27, 2023
1 parent 4b4d92f commit 0735e7f
Showing 1 changed file with 59 additions and 22 deletions.
81 changes: 59 additions & 22 deletions FrontEnd/script.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,76 @@
// async function to get the works from the API
async function getWorks() {
const response = await fetch("http://localhost:5678/api/works");
const works = await response.json();
const gallery = document.querySelector(".gallery");
gallery.innerHTML = "";
try {
const response = await fetch("http://localhost:5678/api/works");
const works = await response.json();
const gallery = document.querySelector(".gallery");
gallery.innerHTML = "";

console.log(works);
console.log(works);

works.forEach(workCard => {
const workElement = document.createElement("figure");
workElement.dataset.id = workCard.id;
works.forEach((workCard) => {
const workElement = document.createElement("figure");
workElement.dataset.id = workCard.id;

const imageElement = document.createElement("img");
imageElement.src = workCard.imageUrl;
imageElement.alt = workCard.title;
const imageElement = document.createElement("img");
imageElement.src = workCard.imageUrl;
imageElement.alt = workCard.title;

const titleElement = document.createElement("figcaption");
titleElement.innerText = workCard.title ?? "(aucun titre)";
const titleElement = document.createElement("figcaption");
titleElement.innerText = workCard.title ?? "(aucun titre)";

gallery.appendChild(workElement);
workElement.appendChild(imageElement);
workElement.appendChild(titleElement);
});
gallery.appendChild(workElement);
workElement.appendChild(imageElement);
workElement.appendChild(titleElement);
});
} catch (error) {
console.error("Error fetching works:", error);
}
}

// async function to get the categories from the API
async function getCategories() {
const response = await fetch("http://localhost:5678/api/categories");
const categories = await response.json();
try {
const response = await fetch("http://localhost:5678/api/categories");
const categories = await response.json();

console.log(categories);
console.log(categories);

const filtersDiv = document.querySelector(".filters");
// create a unique button "all"
const firstButtonElement = document.createElement("button");
firstButtonElement.innerText = "Tous";
firstButtonElement.dataset.id = categories
.map((category) => category.id)
.join(",");
firstButtonElement.classList.add("filter", "filter--active");
filtersDiv.appendChild(firstButtonElement);
// create a button for each category
categories.forEach((category) => {
const buttonElement = document.createElement("button");
buttonElement.innerText = category.name;
buttonElement.dataset.id = category.id;
buttonElement.classList.add("filter");

filtersDiv.appendChild(buttonElement);
});

// add event listener to filter buttons
const filterButtons = document.querySelectorAll(".filter");
filterButtons.forEach((button) => {
button.addEventListener("click", handleFilterClick);
});
} catch (error) {
console.error("Error fetching categories:", error);
}
}

// function to handle the click on a filter button
function handleFilterClick(event) {
const categoryId = event.target.dataset.id;
console.log("Filter button clicked:", categoryId);

// Add your code here to handle the categories data
}

getWorks();
getCategories();

0 comments on commit 0735e7f

Please sign in to comment.