ProductStore, move API calls to separate file

This commit is contained in:
2024-09-18 15:59:16 +02:00
parent 2847bd940f
commit 9ee344f45f
8 changed files with 133 additions and 123 deletions

View File

@@ -1,48 +1,32 @@
<script setup lang="ts">
import { SortOrder } from '@/data/enums/sortOrderEnum';
import { CategoryModel } from '@/data/models/categoryModel';
import { FilterModel } from '@/data/models/filterModel';
import { useProductStore } from '@/data/stores/productStore';
// Parameters
defineProps({
numberOfItems: {
type: Number,
default: 0
},
categories: {
type: Array<CategoryModel>,
required: true
},
sortBy: {
type: Array<FilterModel>,
required: true
}
})
// Constants
const selectedCategory = defineModel("selectedCategory", { required: true, type: CategoryModel, default: new CategoryModel() })
const sortedBy = defineModel("sortedBy", { required: true, type: FilterModel })
const onlyDiscounts = defineModel("onlyDiscounts", { required: true, type: Boolean })
const productStore = useProductStore()
const sortOrderItems = Object.values(SortOrder)
</script>
<template>
<v-card>
<v-card-title>
<div v-if="numberOfItems == 1">
{{ numberOfItems }} {{ $t('product.product') }}
<div v-if="productStore.filteredProducts.length == 1">
{{ productStore.filteredProducts.length }} {{ $t('product.product') }}
</div>
<div v-else>
{{ numberOfItems }} {{ $t('product.products') }}
{{ productStore.filteredProducts.length }} {{ $t('product.products') }}
</div>
</v-card-title>
<v-container class="pb-0">
<v-row>
<v-spacer />
<v-col class="d-flex justify-center align-center">
<v-checkbox :label="$t('offers')" v-model="onlyDiscounts" />
<v-checkbox :label="$t('offers')" v-model="productStore.onlyDiscounts" />
</v-col>
<v-col class="d-flex justify-left align-center">
<v-select :items="categories" :label="$t('categories')" v-model="selectedCategory" >
<!-- todo <v-select :items="categories" :label="$t('categories')" v-model="selectedCategory" >
<template v-slot:item="{ props, item }">
<v-list-item v-bind="props" :prepend-icon="item.raw.icon" :title="item.raw.name" />
</template>
@@ -50,17 +34,17 @@ const onlyDiscounts = defineModel("onlyDiscounts", { required: true, type: Boole
<template v-slot:selection="{ item }">
<v-list-item :prepend-icon="item.raw.icon" :title="item.raw.name" />
</template>
</v-select>
</v-select> -->
</v-col>
<v-col class="d-flex justify-left align-center">
<v-select :label="$t('sortBy')" :items="sortBy" v-model="sortedBy" >
<v-select :label="$t('sortBy')" :items="sortOrderItems" v-model="productStore.sortOrder" >
<template v-slot:item="{ props, item }">
<v-list-item v-bind="props" :prepend-icon="item.raw.icon" :title="item.raw.name" />
<v-list-item v-bind="props" :title="item.title" />
</template>
<template v-slot:selection="{ item }">
<v-list-item :prepend-icon="item.raw.icon" :title="item.raw.name" />
<v-list-item :title="item.title" />
</template>
</v-select>
</v-col>

View File

@@ -2,18 +2,15 @@
import productCard from "./productCard.vue"
import productDetails from "./productDetails.vue"
import filterBar from "./filterBar.vue"
import axios from "axios";
import { Ref, ref, watch } from "vue";
import { ProductModel } from "@/data/models/productModel";
import { CategoryModel } from "@/data/models/categoryModel";
import { FilterModel } from "@/data/models/filterModel";
import { useProductStore } from "@/data/stores/productStore";
const productStore = useProductStore()
const products: Ref<Array<ProductModel>> = ref<Array<ProductModel>>([])
const filteredProducts: Ref<Array<ProductModel>> = ref<Array<ProductModel>>([])
const categories: Ref<Array<CategoryModel>> = ref<Array<CategoryModel>>([new CategoryModel()])
const selectedCategory: Ref<CategoryModel> = ref<CategoryModel>(new CategoryModel())
const sortedBy: Ref<FilterModel> = ref<FilterModel>()
const onlyDiscounts: Ref<boolean> = ref(false)
const showProductDetails = ref(false)
const dialogProduct = ref(new ProductModel())
@@ -24,22 +21,13 @@ const sortBy: Array<FilterModel> = [
{ 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) {
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()
})
// todo axios.get("http://127.0.0.1:3000/categories")
// .then(function (response) {
// for (let category of response.data) {
// categories.value.push(category)
// }
// })
function getCategoryById(id: number) {
return categories.value.find(category =>
@@ -47,70 +35,14 @@ function getCategoryById(id: number) {
)
}
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
)
}
if (onlyDiscounts.value) {
filteredProducts.value = filteredProducts.value.filter(product =>
product.discount > 0
)
}
}
function showProductDialog(product: ProductModel) {
dialogProduct.value = product
showProductDetails.value = true
}
watch(() => selectedCategory.value, () => { filterProducts() })
watch(() => sortedBy.value, () => { sortProducts() })
watch(() => onlyDiscounts.value, () => { filterProducts() })
watch(() => productStore.filteredCategory, async () => { productStore.filterProducts() })
watch(() => productStore.sortOrder, async () => { productStore.sortProducts() })
watch(() => productStore.onlyDiscounts, async () => { productStore.filterProducts() })
</script>
<template>
@@ -118,17 +50,18 @@ watch(() => onlyDiscounts.value, () => { filterProducts() })
<v-row>
<v-col>
<filter-bar
:number-of-items="filteredProducts.length"
:number-of-items="productStore.filteredProducts.length"
:categories="categories"
:sort-by="sortBy"
v-model:selected-category="selectedCategory"
v-model:sorted-by="sortedBy"
v-model:only-discounts="onlyDiscounts"
/>
</v-col>
</v-row>
<v-row dense>
<v-col v-if="filteredProducts.length > 0" v-for="product in filteredProducts" cols="12" sm="6" lg="4" xl="3">
<v-col
v-if="productStore.filteredProducts.length > 0"
v-for="product in productStore.filteredProducts"
cols="12" sm="6" lg="4" xl="3"
>
<product-card
:product="product"
:category="getCategoryById(product.categoryId)"

View File

@@ -7,9 +7,9 @@ defineProps({
required: true,
type: ProductModel
},
category: {
type: CategoryModel
},
// category: {
// type: CategoryModel
// },
})
</script>
@@ -25,9 +25,9 @@ defineProps({
{{ product.name }}
</v-card-title>
<v-card-subtitle class="mb-2">
<!-- todo <v-card-subtitle class="mb-2">
<div><v-icon :icon="category.icon" /> {{ category.name }}</div>
</v-card-subtitle>
</v-card-subtitle> -->
</v-img>
<v-card-text>

View File

@@ -13,11 +13,11 @@ const basketStore = useBasketStore()
const props = defineProps({
product: ProductModel,
productCategory: CategoryModel
// todo productCategory: CategoryModel
})
function addProductToBasket() {
basketStore.addItemToBasket(productToBasketItem(props.product, props.productCategory, nrOfArticles.value))
// todo basketStore.addItemToBasket(productToBasketItem(props.product, props.productCategory, nrOfArticles.value))
nrOfArticles.value = 1
showDialog.value = false
}
@@ -32,10 +32,10 @@ function addProductToBasket() {
>
<template #content>
<v-row>
<v-col>
<!-- todo <v-col>
<v-icon :icon="productCategory.icon" />
{{ productCategory.name }}
</v-col>
</v-col> -->
</v-row>
<v-row>
<v-col>