diff --git a/software/src/pages/productsPage/filterBar.vue b/software/src/pages/productsPage/filterBar.vue index c55516e..0db0ea1 100644 --- a/software/src/pages/productsPage/filterBar.vue +++ b/software/src/pages/productsPage/filterBar.vue @@ -1,37 +1,27 @@ @@ -49,7 +39,7 @@ axios.get("http://127.0.0.1:3000/categories") - + diff --git a/software/src/pages/productsPage/index.vue b/software/src/pages/productsPage/index.vue index 4febba4..f205b31 100644 --- a/software/src/pages/productsPage/index.vue +++ b/software/src/pages/productsPage/index.vue @@ -2,22 +2,40 @@ import productCard from "./productCard.vue" import filterBar from "./filterBar.vue" import axios from "axios"; -import { Ref, ref } from "vue"; +import { Ref, ref, watch } from "vue"; import { ProductModel } from "@/data/models/productModel"; import { CategoryModel } from "@/data/models/categoryModel"; +import { FilterModel } from "@/data/models/filterModel"; const products: Ref> = ref>([]) -const categories: Ref> = ref>([]) +const filteredProducts: Ref> = ref>([]) +const categories: Ref> = ref>([new CategoryModel()]) +const selectedCategory: Ref = ref(new CategoryModel()) +const sortedBy: Ref = ref() +const onlyDiscounts: Ref = ref(false) +const sortBy: Array = [ + { icon: "mdi-sort-ascending", name: "Price: Low to high" }, + { icon: "mdi-sort-descending", name: "Price: High to low" }, + { icon: "mdi-sort-alphabetical-ascending", name: "Name: A to Z" }, + { icon: "mdi-sort-alphabetical-descending", name: "Name: Z to A" }, +] + +sortedBy.value = sortBy[0] axios.get("http://127.0.0.1:3000/categories") .then(function (response) { - categories.value = response.data + for (let category of response.data) { + categories.value.push(category) + } }) axios.get("http://127.0.0.1:3000/products") .then(function (response) { products.value = response.data + filteredProducts.value = response.data + + sortProducts() }) function getCategoryById(id: number) { @@ -25,17 +43,91 @@ function getCategoryById(id: number) { category.id === id ) } + + +function sortProducts() { + switch (sortedBy.value.name) { + case sortBy[0].name: { + filteredProducts.value.sort((a, b) => + a.price - b.price + ) + break; + } + case sortBy[1].name: { + filteredProducts.value.sort((a, b) => + b.price - a.price + ) + break; + } + case sortBy[2].name: { + filteredProducts.value.sort((a, b) => { + if (b.name > a.name) { + return -1 + } + else { + return 1 + } + }) + break; + } + case sortBy[3].name: { + filteredProducts.value.sort((a, b) => { + if (b.name < a.name) { + return -1 + } + else { + return 1 + } + }) + break; + } + } +} + +function filterProducts() { + if (selectedCategory.value.id == -1) { + filteredProducts.value = products.value + } else { + filteredProducts.value = products.value.filter(product => + product.categoryId == selectedCategory.value.id + ) + } + + console.log("1", filteredProducts.value) + + if (onlyDiscounts.value) { + filteredProducts.value = filteredProducts.value.filter(product => + product.discount > 0 + ) + } + + console.log("2", filteredProducts.value) +} + +watch(() => selectedCategory.value, () => { filterProducts() }) +watch(() => sortedBy.value, () => { sortProducts() }) +watch(() => onlyDiscounts.value, () => { filterProducts() }) - + - - + + \ No newline at end of file diff --git a/software/src/pages/productsPage/productCard.vue b/software/src/pages/productsPage/productCard.vue index b346c86..343747c 100644 --- a/software/src/pages/productsPage/productCard.vue +++ b/software/src/pages/productsPage/productCard.vue @@ -31,17 +31,22 @@ defineProps({ - + + + + {{ product.rating }}/5 + {{ product.price }} € - + {{ (product.price * ( 100 - product.discount) / 100).toFixed(2) }} €