From 89dcb41e900aa1778a79bcfea0f27dd94c94e370 Mon Sep 17 00:00:00 2001 From: That-Thing Date: Fri, 8 Nov 2024 14:27:33 -0500 Subject: [PATCH 1/2] Adds tag filters --- src/views/HomeView.vue | 73 +++++++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/src/views/HomeView.vue b/src/views/HomeView.vue index e9fb2bb..ead73d3 100644 --- a/src/views/HomeView.vue +++ b/src/views/HomeView.vue @@ -4,6 +4,8 @@ import axios from 'axios'; import { useToast } from 'vue-toast-notification'; import 'vue-toast-notification/dist/theme-sugar.css'; import MenuItemComponent from '../components/menuItemComponent.vue'; +import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'; +import { faFilter } from '@fortawesome/free-solid-svg-icons'; const API_URL = import.meta.env.VITE_APP_API_URL; const $toast = useToast(); @@ -17,12 +19,15 @@ const selectedLocation = ref(localStorage.getItem('selectedLocation') || locatio const selectedTime = ref(null); // Selected time const selectedDate = ref(new Date().toLocaleDateString('en-CA', { timeZone: 'America/New_York' })); // YYYY-MM-DD + // Pagination state const currentPage = ref(1); const itemsPerPage = 10; // Search state const searchQuery = ref(''); // Search query +const selectedLabels = ref([]); // Selected labels for filtering +const showFilters = ref(false); // Toggle state for filters // Fetch locations from the API axios.get(`${API_URL}/locations`) @@ -112,53 +117,66 @@ const changeTime = (time) => { currentPage.value = 1; }; +const uniqueLabels = computed(() => { + return [...new Set(menuItems.value.flatMap(item => item.labels || []))]; +}); + +// Filter items based on selected time, search query, and labels const filteredMenuItems = computed(() => { return menuItems.value .filter(item => item.time === selectedTime.value) .filter(item => - (item.name.toLowerCase().includes(searchQuery.value.toLowerCase())) || + item.name.toLowerCase().includes(searchQuery.value.toLowerCase()) || ((item.description || '').toLowerCase().includes(searchQuery.value.toLowerCase())) + ) + .filter(item => + // Check if item.labels contains all selected labels + selectedLabels.value.length === 0 || selectedLabels.value.every(label => (item.labels || []).includes(label)) ); }); + const paginationPages = computed(() => { const pages = []; - const range = 2; // Number of pages to display before and after the current page + const range = 2; for (let i = currentPage.value - range; i <= currentPage.value + range; i++) { if (i > 0 && i <= totalPages.value) { pages.push(i); } } - return pages; }); - const paginatedMenuItems = computed(() => { const start = (currentPage.value - 1) * itemsPerPage; - const end = start + itemsPerPage; - return filteredMenuItems.value.slice(start, end); + return filteredMenuItems.value.slice(start, start + itemsPerPage); }); -// Calculate total pages const totalPages = computed(() => Math.ceil(filteredMenuItems.value.length / itemsPerPage)); -// Change page const changePage = (page) => { if (page >= 1 && page <= totalPages.value) { currentPage.value = page; } }; + +const toggleLabel = (label) => { + const index = selectedLabels.value.indexOf(label); + if (index === -1) selectedLabels.value.push(label); + else selectedLabels.value.splice(index, 1); +};