Przeglądaj źródła

feat: filter menu by category

bregsiaju 1 rok temu
rodzic
commit
6bfbb62d9f
3 zmienionych plików z 60 dodań i 16 usunięć
  1. 1
    0
      src/components/OrderList.vue
  2. 12
    5
      src/stores/menus.js
  3. 47
    11
      src/views/HomeView.vue

+ 1
- 0
src/components/OrderList.vue Wyświetl plik

@@ -170,6 +170,7 @@ const totalPayment = computed(() => {
170 170
     box-shadow: 0px 0px 2px 0px rgba(0, 0, 0, 0.25);
171 171
     padding: 10px 0;
172 172
     border: 1px solid transparent;
173
+    cursor: pointer;
173 174
 
174 175
     &.selected {
175 176
       color: white;

+ 12
- 5
src/stores/menus.js Wyświetl plik

@@ -6,11 +6,6 @@ export const useMenuStore = defineStore('menus', {
6 6
     loading: false,
7 7
     error: null
8 8
   }),
9
-  getters: {
10
-    getMenuPerCategory: (state) => {
11
-      return (categoryName) => state.menus.filter((item) => item.category === categoryName)
12
-    }
13
-  },
14 9
   actions: {
15 10
     async fetchMenus() {
16 11
       this.menus = []
@@ -22,6 +17,18 @@ export const useMenuStore = defineStore('menus', {
22 17
       } finally {
23 18
         this.loading = false
24 19
       }
20
+    },
21
+    async getMenuPerCategory(categoryName) {
22
+      await this.fetchMenus()
23
+      console.log('nama kategori ' + categoryName)
24
+
25
+      if (categoryName !== 'all') {
26
+        const filteredCategory = this.menus.filter((item) => item.category === categoryName)
27
+        console.log(filteredCategory)
28
+        this.menus = filteredCategory
29
+      } else {
30
+        await this.fetchMenus()
31
+      }
25 32
     }
26 33
   }
27 34
 })

+ 47
- 11
src/views/HomeView.vue Wyświetl plik

@@ -5,9 +5,9 @@
5 5
         <div class="category">
6 6
           <h5 class="mb-3">Categories</h5>
7 7
           <div class="d-flex gap-3 category-list">
8
-            <button v-for="item in categories" :key="item.id" class="btn btn-outline-dark"
9
-              :class="{ active: selectedCategories === item.id }" v-html="item.name"
10
-              @click="selectedCategories = item.id"></button>
8
+            <button v-for="category in categories" :key="category.id" class="btn btn-outline-dark"
9
+              :class="{ active: selectedCategories === category.id }" v-html="category.title"
10
+              @click="selectCategory(category)"></button>
11 11
           </div>
12 12
         </div>
13 13
         <MenuContainer />
@@ -20,8 +20,10 @@
20 20
 </template>
21 21
 
22 22
 <script>
23
+import { ref } from 'vue';
23 24
 import MenuContainer from '../components/MenuContainer.vue';
24 25
 import OrderList from '../components/OrderList.vue';
26
+import { useMenuStore } from '../stores/menus';
25 27
 
26 28
 export default {
27 29
   components: {
@@ -33,31 +35,51 @@ export default {
33 35
       categories: [
34 36
         {
35 37
           id: 1,
36
-          name: '&#129368; All Menu'
38
+          value: 'all',
39
+          title: '&#129368; All Menu'
37 40
         },
38 41
         {
39 42
           id: 2,
40
-          name: '&#9749; Coffee'
43
+          value: 'coffee',
44
+          title: '&#9749; Coffee'
41 45
         },
42 46
         {
43 47
           id: 3,
44
-          name: '&#127851; Chocolate'
48
+          value: 'tea',
49
+          title: '&#129483; Tea'
45 50
         },
46 51
         {
47 52
           id: 4,
48
-          name: '&#127839; Snacks'
53
+          value: 'chocolate',
54
+          title: '&#127851; Chocolate'
49 55
         },
50 56
         {
51 57
           id: 5,
52
-          name: '&#127848; Ice Cream'
58
+          value: 'snacks',
59
+          title: '&#127839; Snacks'
53 60
         },
54
-      ],
55
-      selectedCategories: 1
61
+        {
62
+          id: 6,
63
+          value: 'ice cream',
64
+          title: '&#127848; Ice Cream'
65
+        },
66
+      ]
56 67
     }
57 68
   }
58 69
 }
59 70
 </script>
60 71
 
72
+<script setup>
73
+const { getMenuPerCategory } = useMenuStore()
74
+
75
+let selectedCategories = ref(1)
76
+
77
+const selectCategory = (category) => {
78
+  selectedCategories.value = category.id;
79
+  getMenuPerCategory(category.value)
80
+}
81
+</script>
82
+
61 83
 <style lang="scss" scoped>
62 84
 .menu {
63 85
   padding: 28px 8px 0 32px;
@@ -75,15 +97,29 @@ export default {
75 97
 }
76 98
 
77 99
 .category {
78
-  margin-bottom: 30px;
100
+  margin-bottom: 20px;
79 101
 
80 102
   .category-list {
103
+    overflow-x: auto;
104
+    padding-bottom: 10px;
105
+
106
+    &::-webkit-scrollbar {
107
+      height: 5px;
108
+      // background-color: #f5f5f5;
109
+    }
110
+
111
+    &::-webkit-scrollbar-thumb {
112
+      border-radius: 10px;
113
+      background-color: transparent;
114
+    }
115
+
81 116
     .btn {
82 117
       border-radius: 50px;
83 118
       padding: 8px 16px;
84 119
       border-color: #5C5470;
85 120
       color: #5C5470;
86 121
       font-size: 14px;
122
+      white-space: nowrap;
87 123
 
88 124
       &:hover {
89 125
         background: linear-gradient(232deg, rgba(185, 138, 238, 0.80) 0%, rgba(251, 190, 222, 0.80) 100%);

Ładowanie…
Anuluj
Zapisz