44 <h1>Product list</h1>
55 <!--FIXME: fix category table, to organise products by category (those seen on the menu)-->
66
7- <div>
8- <?php foreach ($ products as $ product ) { ?>
9- <?php $ imagePath = !empty ($ product ->image ) ? $ product ->image : '/assets/images/test.jpg ' ; ?>
10- <article>
11- <a href="/product?id=<?php echo htmlspecialchars ($ product ->id ); ?> ">
12- <img
13- src="<?php echo htmlspecialchars ($ imagePath ); ?> "
14- alt="<?php echo htmlspecialchars ($ product ->name ); ?> "
15- >
16-
17- <div>
18- <h2><?php echo htmlspecialchars ($ product ->name ); ?> </h2>
19- <p><?php echo htmlspecialchars ($ product ->price ); ?> €</p>
20- </div>
21- </a>
22- </article>
23- <?php } ?>
7+ <div class="sort-filter">
8+ <input type="text" id="search" placeholder="Rechercher...">
9+ <select id="sort">
10+ <option value="">-- Trier --</option>
11+ <option value="name_asc">Nom A-Z</option>
12+ <option value="name_desc">Nom Z-A</option>
13+ </select>
14+ </div>
15+
16+ <div class="products">
17+ <?php include "partials/product_list.php " ; ?>
2418 </div>
2519</main>
2620
27- <?php include 'partials/footer.php ' ; ?>
21+ <?php include 'partials/footer.php ' ; ?>
22+
23+
24+ <script>
25+ function loadProducts() {
26+ const search = document.getElementById('search').value;
27+ const sort = document.getElementById('sort').value;
28+
29+ fetch('/products/filter', {
30+ method: 'POST',
31+ headers: { 'Content-Type': 'application/json' },
32+ body: JSON.stringify({ search, sort })
33+ })
34+ .then(res => res.text())
35+ .then(html => {
36+ document.querySelector('.products').innerHTML = html;
37+ });
38+ }
39+
40+ // events
41+ document.getElementById('search').addEventListener('input', debounce(loadProducts, 300));
42+ document.getElementById('sort').addEventListener('change', loadProducts);
43+
44+ // debounce
45+ function debounce(fn, delay) {
46+ let timeout;
47+ return (...args) => {
48+ clearTimeout(timeout);
49+ timeout = setTimeout(() => fn(...args), delay);
50+ };
51+ }
52+
53+ </script>
0 commit comments