CategoryStore, API calls

This commit is contained in:
2024-09-18 16:21:07 +02:00
parent 9ee344f45f
commit 7b245da959
11 changed files with 83 additions and 52 deletions

View File

@@ -6,9 +6,11 @@ import { ref } from 'vue';
import vuetify from './plugins/vuetify'; import vuetify from './plugins/vuetify';
import navigationItems from './components/navigationItems.vue'; import navigationItems from './components/navigationItems.vue';
import { useProductStore } from './data/stores/productStore'; import { useProductStore } from './data/stores/productStore';
import { useCategoryStore } from './data/stores/categoryStore';
const userStore = useUserStore() const userStore = useUserStore()
const productStore = useProductStore() const productStore = useProductStore()
const categoryStore = useCategoryStore()
const theme = useTheme() const theme = useTheme()
const navRail = ref(vuetify.display.mobile) const navRail = ref(vuetify.display.mobile)
@@ -16,6 +18,7 @@ theme.global.name.value = userStore.theme
i18n.global.locale = userStore.language i18n.global.locale = userStore.language
productStore.fetchAllProducts() productStore.fetchAllProducts()
categoryStore.fetchAllCategories()
</script> </script>
<template> <template>

View File

@@ -0,0 +1,7 @@
import axios from "axios"
let BASE_URL = "http://localhost:3000/categories"
export async function getAllCategories() {
return await axios.get(BASE_URL)
}

View File

@@ -8,6 +8,4 @@ export class ProductModel {
discount: number = 0 discount: number = 0
rating: number = 1 rating: number = 1
imageUrl: string = "" imageUrl: string = ""
createdAt: string = ""
updatedAt: string = ""
} }

View File

@@ -0,0 +1,13 @@
import { CategoryModel } from "./categoryModel"
export class ProductWithCategoryModel {
id: number = -1
brand: string
name: string
description: string = ""
category: CategoryModel
price: number = 0
discount: number = 0
rating: number = 1
imageUrl: string = ""
}

View File

@@ -0,0 +1,25 @@
import { useLocalStorage } from "@vueuse/core";
import { defineStore } from "pinia";
import { CategoryModel } from "../models/categoryModel";
import { getAllCategories } from "../api/categoryApi";
export const useCategoryStore = defineStore("categoryStore", {
state: () => ({
categories: useLocalStorage<Array<CategoryModel>>("hackmycart/categoryStore/categories", [])
}),
actions: {
async fetchAllCategories() {
await getAllCategories()
.then(categories => {
this.categories = categories.data
})
},
getProductById(id: number): CategoryModel {
return this.categories.find(category =>
category.id === id
)
}
}
})

View File

@@ -1,15 +1,15 @@
import { useLocalStorage } from "@vueuse/core"; import { useLocalStorage } from "@vueuse/core";
import { defineStore } from "pinia"; import { defineStore } from "pinia";
import { ProductModel } from "../models/productModel";
import { getAllProducts } from "../api/productApi"; import { getAllProducts } from "../api/productApi";
import { SortOrder } from "../enums/sortOrderEnum"; import { SortOrder } from "../enums/sortOrderEnum";
import { CategoryModel } from "../models/categoryModel"; import { CategoryModel } from "../models/categoryModel";
import { ProductWithCategoryModel } from "../models/productWithCategoryModel";
export const useProductStore = defineStore("productStore", { export const useProductStore = defineStore("productStore", {
state: () => ({ state: () => ({
products: useLocalStorage<Array<ProductModel>>("hackmycart/productStore/products", []), products: useLocalStorage<Array<ProductWithCategoryModel>>("hackmycart/productStore/products", []),
filteredProducts: useLocalStorage<Array<ProductModel>>("hackmycart/productStore/filteredProducts", []), filteredProducts: useLocalStorage<Array<ProductWithCategoryModel>>("hackmycart/productStore/filteredProducts", []),
sortOrder: useLocalStorage<SortOrder>("hackmycart/productStore/sortOrder", SortOrder.NAMEATOZ), sortOrder: useLocalStorage<SortOrder>("hackmycart/productStore/sortOrder", SortOrder.NAMEATOZ),
filteredCategory: useLocalStorage<CategoryModel>("hackmycart/productStore/filteredCategory", new CategoryModel()), filteredCategory: useLocalStorage<CategoryModel>("hackmycart/productStore/filteredCategory", new CategoryModel()),
onlyDiscounts: useLocalStorage<Boolean>("hackmycart/productStore/onlyDiscounts", false) onlyDiscounts: useLocalStorage<Boolean>("hackmycart/productStore/onlyDiscounts", false)
@@ -28,20 +28,20 @@ export const useProductStore = defineStore("productStore", {
if (this.filteredCategory.id == -1) { if (this.filteredCategory.id == -1) {
this.filteredProducts = this.products this.filteredProducts = this.products
} else { } else {
this.filteredProducts = this.products.filter((product: ProductModel) => this.filteredProducts = this.products.filter((product: ProductWithCategoryModel) =>
product.categoryId == this.filteredCategory.id product.category.id == this.filteredCategory.id
) )
} }
if (this.onlyDiscounts) { if (this.onlyDiscounts) {
this.filteredProducts = this.filteredProducts.filter((product: ProductModel) => this.filteredProducts = this.filteredProducts.filter((product: ProductWithCategoryModel) =>
product.discount > 0 product.discount > 0
) )
} }
}, },
sortProducts() { sortProducts() {
this.filteredProducts.sort((a: ProductModel, b: ProductModel) => { this.filteredProducts.sort((a: ProductWithCategoryModel, b: ProductWithCategoryModel) => {
switch (this.sortOrder) switch (this.sortOrder)
{ {
case SortOrder.PRICELOWTOHIGH: { case SortOrder.PRICELOWTOHIGH: {

View File

@@ -1,10 +1,10 @@
<script setup lang="ts"> <script setup lang="ts">
import { SortOrder } from '@/data/enums/sortOrderEnum'; import { SortOrder } from '@/data/enums/sortOrderEnum';
import { CategoryModel } from '@/data/models/categoryModel'; import { useCategoryStore } from '@/data/stores/categoryStore';
import { FilterModel } from '@/data/models/filterModel';
import { useProductStore } from '@/data/stores/productStore'; import { useProductStore } from '@/data/stores/productStore';
const productStore = useProductStore() const productStore = useProductStore()
const categoryStore = useCategoryStore()
const sortOrderItems = Object.values(SortOrder) const sortOrderItems = Object.values(SortOrder)
</script> </script>
@@ -26,7 +26,11 @@ const sortOrderItems = Object.values(SortOrder)
</v-col> </v-col>
<v-col class="d-flex justify-left align-center"> <v-col class="d-flex justify-left align-center">
<!-- todo <v-select :items="categories" :label="$t('categories')" v-model="selectedCategory" > <v-select
:items="categoryStore.categories"
:label="$t('categories')"
v-model="productStore.filteredCategory"
>
<template v-slot:item="{ props, item }"> <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" :prepend-icon="item.raw.icon" :title="item.raw.name" />
</template> </template>
@@ -34,7 +38,7 @@ const sortOrderItems = Object.values(SortOrder)
<template v-slot:selection="{ item }"> <template v-slot:selection="{ item }">
<v-list-item :prepend-icon="item.raw.icon" :title="item.raw.name" /> <v-list-item :prepend-icon="item.raw.icon" :title="item.raw.name" />
</template> </template>
</v-select> --> </v-select>
</v-col> </v-col>
<v-col class="d-flex justify-left align-center"> <v-col class="d-flex justify-left align-center">

View File

@@ -3,16 +3,17 @@ import productCard from "./productCard.vue"
import productDetails from "./productDetails.vue" import productDetails from "./productDetails.vue"
import filterBar from "./filterBar.vue" import filterBar from "./filterBar.vue"
import { Ref, ref, watch } from "vue"; import { Ref, ref, watch } from "vue";
import { ProductModel } from "@/data/models/productModel";
import { CategoryModel } from "@/data/models/categoryModel"; import { CategoryModel } from "@/data/models/categoryModel";
import { FilterModel } from "@/data/models/filterModel"; import { FilterModel } from "@/data/models/filterModel";
import { useProductStore } from "@/data/stores/productStore"; import { useProductStore } from "@/data/stores/productStore";
import { useCategoryStore } from "@/data/stores/categoryStore";
import { ProductWithCategoryModel } from "@/data/models/productWithCategoryModel";
const productStore = useProductStore() const productStore = useProductStore()
const categoryStore = useCategoryStore()
const categories: Ref<Array<CategoryModel>> = ref<Array<CategoryModel>>([new CategoryModel()])
const showProductDetails = ref(false) const showProductDetails = ref(false)
const dialogProduct = ref(new ProductModel()) const dialogProduct = ref(new ProductWithCategoryModel())
const sortBy: Array<FilterModel> = [ const sortBy: Array<FilterModel> = [
{ icon: "mdi-sort-ascending", name: "Price: Low to high" }, { icon: "mdi-sort-ascending", name: "Price: Low to high" },
@@ -21,21 +22,7 @@ const sortBy: Array<FilterModel> = [
{ icon: "mdi-sort-alphabetical-descending", name: "Name: Z to A" }, { icon: "mdi-sort-alphabetical-descending", name: "Name: Z to A" },
] ]
function showProductDialog(product: ProductWithCategoryModel) {
// 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 =>
category.id === id
)
}
function showProductDialog(product: ProductModel) {
dialogProduct.value = product dialogProduct.value = product
showProductDetails.value = true showProductDetails.value = true
} }
@@ -49,11 +36,7 @@ watch(() => productStore.onlyDiscounts, async () => { productStore.filterProduct
<v-container> <v-container>
<v-row> <v-row>
<v-col> <v-col>
<filter-bar <filter-bar />
:number-of-items="productStore.filteredProducts.length"
:categories="categories"
:sort-by="sortBy"
/>
</v-col> </v-col>
</v-row> </v-row>
<v-row dense> <v-row dense>
@@ -64,7 +47,6 @@ watch(() => productStore.onlyDiscounts, async () => { productStore.filterProduct
> >
<product-card <product-card
:product="product" :product="product"
:category="getCategoryById(product.categoryId)"
@click="showProductDialog(product)" @click="showProductDialog(product)"
/> />
</v-col> </v-col>
@@ -80,7 +62,6 @@ watch(() => productStore.onlyDiscounts, async () => { productStore.filterProduct
<product-details <product-details
v-model="showProductDetails" v-model="showProductDetails"
:product="dialogProduct" :product="dialogProduct"
:productCategory="getCategoryById(dialogProduct.categoryId)"
/> />
</template> </template>

View File

@@ -1,15 +1,11 @@
<script setup lang="ts"> <script setup lang="ts">
import { CategoryModel } from '@/data/models/categoryModel'; import { ProductWithCategoryModel } from '@/data/models/productWithCategoryModel';
import { ProductModel } from '@/data/models/productModel';
defineProps({ defineProps({
product: { product: {
required: true, required: true,
type: ProductModel type: ProductWithCategoryModel
}, }
// category: {
// type: CategoryModel
// },
}) })
</script> </script>
@@ -25,9 +21,12 @@ defineProps({
{{ product.name }} {{ product.name }}
</v-card-title> </v-card-title>
<!-- todo <v-card-subtitle class="mb-2"> <v-card-subtitle class="mb-2">
<div><v-icon :icon="category.icon" /> {{ category.name }}</div> <div>
</v-card-subtitle> --> <v-icon :icon="product.category.icon" />
{{ product.category.name }}
</div>
</v-card-subtitle>
</v-img> </v-img>
<v-card-text> <v-card-text>

View File

@@ -6,14 +6,14 @@ import { ModelRef, ref } from 'vue';
import { useBasketStore } from '@/data/stores/basketStore'; import { useBasketStore } from '@/data/stores/basketStore';
import { calcProductPrice, productToBasketItem } from '@/scripts/productScripts'; import { calcProductPrice, productToBasketItem } from '@/scripts/productScripts';
import ActionDialog from '@/components/actionDialog.vue' import ActionDialog from '@/components/actionDialog.vue'
import { ProductWithCategoryModel } from '@/data/models/productWithCategoryModel';
const showDialog: ModelRef<boolean> = defineModel() const showDialog: ModelRef<boolean> = defineModel()
const nrOfArticles = ref(1) const nrOfArticles = ref(1)
const basketStore = useBasketStore() const basketStore = useBasketStore()
const props = defineProps({ const props = defineProps({
product: ProductModel, product: ProductWithCategoryModel
// todo productCategory: CategoryModel
}) })
function addProductToBasket() { function addProductToBasket() {

View File

@@ -1,8 +1,9 @@
import { BasketItemModel } from "@/data/models/basketItemModel"; import { BasketItemModel } from "@/data/models/basketItemModel";
import { CategoryModel } from "@/data/models/categoryModel"; import { CategoryModel } from "@/data/models/categoryModel";
import { ProductModel } from "@/data/models/productModel"; import { ProductModel } from "@/data/models/productModel";
import { ProductWithCategoryModel } from "@/data/models/productWithCategoryModel";
export function calcProductPrice(product: ProductModel, quantity: number = 1): number { export function calcProductPrice(product: ProductWithCategoryModel, quantity: number = 1): number {
return calcPrice(product.price, product.discount, quantity) return calcPrice(product.price, product.discount, quantity)
} }