Skip to content

Commit 8cd1bf1

Browse files
committed
Test filter live
1 parent 15749fd commit 8cd1bf1

File tree

3 files changed

+116
-34
lines changed

3 files changed

+116
-34
lines changed

components/Products/ProductsFilters.vue

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
<div class="mb-8">
55
<h3 class="font-semibold mb-4">PRODUKT TYPE</h3>
66
<div class="space-y-2">
7-
<CommonCheckbox id="clothing" label="Clothing" :checked="false" />
8-
<CommonCheckbox id="tshirts" label="Tshirts" :checked="false" />
9-
<CommonCheckbox id="uncategorized" label="Uncategorized" :checked="false" />
7+
<CommonCheckbox
8+
v-for="type in productTypes"
9+
:key="type.id"
10+
:id="type.id"
11+
:label="type.name"
12+
:checked="type.checked"
13+
@change="() => toggleProductType(type.id)"
14+
/>
1015
</div>
1116
</div>
1217

@@ -17,32 +22,62 @@
1722
label="Pris"
1823
:min="0"
1924
:max="1000"
20-
:value="1000"
21-
:startValue="0"
22-
:formatValue="(v) => `kr ${v}`"
23-
:disabled="true"
25+
:value="priceRange[1]"
26+
:startValue="priceRange[0]"
27+
:disabled="false"
28+
@input="(value) => setPriceRange([priceRange[0], value])"
2429
/>
2530
</div>
2631

2732
<div class="mb-8">
2833
<h3 class="font-semibold mb-4">STØRRELSE</h3>
2934
<div class="grid grid-cols-3 gap-2">
30-
<CommonButton :selected="true" variant="filter" :disabled="true">
31-
Large
35+
<CommonButton
36+
v-for="size in sizes"
37+
:key="size"
38+
:selected="selectedSizes.includes(size)"
39+
variant="filter"
40+
@click="() => toggleSize(size)"
41+
>
42+
{{ size }}
3243
</CommonButton>
3344
</div>
3445
</div>
3546

3647
<div class="mb-8">
3748
<h3 class="font-semibold mb-4">FARGE</h3>
3849
<div class="grid grid-cols-3 gap-2">
39-
<CommonColorSwatch color="#3b82f6" title="Blue" />
50+
<CommonColorSwatch
51+
v-for="color in colors"
52+
:key="color.name"
53+
:color="color.hex"
54+
:title="color.name"
55+
:class="{ 'ring-2 ring-offset-2 ring-gray-900': selectedColors.includes(color.name) }"
56+
@click="() => toggleColor(color.name)"
57+
style="cursor:pointer"
58+
/>
4059
</div>
4160
</div>
4261

43-
<CommonButton variant="reset" class="mt-4 w-full" :disabled="true">
62+
<CommonButton variant="reset" class="mt-4 w-full" @click="resetFilters">
4463
Resett filter
4564
</CommonButton>
4665
</div>
4766
</div>
4867
</template>
68+
69+
<script setup>
70+
defineProps({
71+
productTypes: Array,
72+
toggleProductType: Function,
73+
priceRange: Array,
74+
setPriceRange: Function,
75+
sizes: Array,
76+
selectedSizes: Array,
77+
toggleSize: Function,
78+
colors: Array,
79+
selectedColors: Array,
80+
toggleColor: Function,
81+
resetFilters: Function,
82+
});
83+
</script>

components/Products/ProductsShowAll.vue

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ const props = defineProps({
3737
3838
const config = useRuntimeConfig();
3939
40-
const products = computed(() => {
41-
return (
42-
allCategoryProducts.value?.productCategory?.products?.nodes ||
43-
allProducts.value?.products?.nodes ||
44-
[]
45-
);
40+
const props = defineProps({
41+
products: {
42+
type: Array,
43+
required: true,
44+
default: () => [],
45+
},
4646
});
4747
48+
const products = computed(() => props.products);
49+
4850
/**
4951
* Returns the path and query parameters for a product link.
5052
*

pages/products.vue

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,79 @@
11
<template>
22
<div class="container mx-auto px-4 py-8">
33
<div class="flex flex-col md:flex-row gap-8">
4-
<ProductsFilters />
4+
<ProductsFilters
5+
:productTypes="productTypes"
6+
:toggleProductType="toggleProductType"
7+
:priceRange="priceRange"
8+
:setPriceRange="setPriceRange"
9+
:sizes="sizes"
10+
:selectedSizes="selectedSizes"
11+
:toggleSize="toggleSize"
12+
:colors="colors"
13+
:selectedColors="selectedColors"
14+
:toggleColor="toggleColor"
15+
:resetFilters="resetFilters"
16+
/>
517
<div class="flex-1">
618
<div
719
class="flex flex-col sm:flex-row sm:justify-between sm:items-center gap-4 mb-8"
820
>
921
<h1 class="text-xl sm:text-2xl font-medium text-center sm:text-left">
10-
Produkter <span class="text-gray-500">(0)</span>
22+
Produkter
1123
</h1>
12-
<ProductsSort />
24+
<ProductsSort v-model="sortBy" />
1325
</div>
14-
<ProductsShowAll />
26+
<ProductsShowAll
27+
:sortBy="sortBy"
28+
:selectedSizes="selectedSizes"
29+
:selectedColors="selectedColors"
30+
:priceRange="priceRange"
31+
:productTypes="productTypes"
32+
/>
1533
</div>
1634
</div>
1735
</div>
1836
</template>
1937

2038
<script setup>
21-
useHead({
22-
title: "Products",
23-
titleTemplate: "%s - Nuxt 3 Woocommerce",
24-
meta: [
25-
{ name: "viewport", content: "width=device-width, initial-scale=1" },
26-
{
27-
hid: "description",
28-
name: "description",
29-
content: "Nuxt 3 Woocommerce",
30-
},
31-
],
32-
link: [{ rel: "icon", type: "image/x-icon", href: "/favicon.ico" }],
33-
});
39+
const sortBy = ref("popular");
40+
const selectedSizes = ref([]);
41+
const selectedColors = ref([]);
42+
const priceRange = ref([0, 1000]);
43+
const productTypes = ref([
44+
{ id: "clothing", name: "Clothing", checked: false },
45+
{ id: "tshirts", name: "Tshirts", checked: false },
46+
{ id: "uncategorized", name: "Uncategorized", checked: false },
47+
]);
48+
const sizes = ref(["Large"]);
49+
const colors = ref([{ name: "Blue", hex: "#3b82f6" }]);
50+
51+
function toggleProductType(id) {
52+
productTypes.value = productTypes.value.map((type) =>
53+
type.id === id ? { ...type, checked: !type.checked } : type
54+
);
55+
}
56+
function setPriceRange(newRange) {
57+
priceRange.value = newRange;
58+
}
59+
function toggleSize(size) {
60+
if (selectedSizes.value.includes(size)) {
61+
selectedSizes.value = selectedSizes.value.filter((s) => s !== size);
62+
} else {
63+
selectedSizes.value = [...selectedSizes.value, size];
64+
}
65+
}
66+
function toggleColor(color) {
67+
if (selectedColors.value.includes(color)) {
68+
selectedColors.value = selectedColors.value.filter((c) => c !== color);
69+
} else {
70+
selectedColors.value = [...selectedColors.value, color];
71+
}
72+
}
73+
function resetFilters() {
74+
selectedSizes.value = [];
75+
selectedColors.value = [];
76+
priceRange.value = [0, 1000];
77+
productTypes.value = productTypes.value.map((type) => ({ ...type, checked: false }));
78+
}
3479
</script>

0 commit comments

Comments
 (0)