feat: add add card to favorite

This commit is contained in:
Sergey Krylov 2025-01-24 06:45:18 +03:00
parent 6e9947640b
commit 25d1fa25dd
3 changed files with 24 additions and 8 deletions

View File

@ -1,5 +1,5 @@
<script setup> <script setup>
import { onMounted, reactive, ref, watch } from 'vue' import { onMounted, provide, reactive, ref, watch } from 'vue'
import Header from '@/components/Header.vue' import Header from '@/components/Header.vue'
import CardList from '@/components/CardList.vue' import CardList from '@/components/CardList.vue'
import Drawer from '@/components/Drawer.vue' import Drawer from '@/components/Drawer.vue'
@ -34,9 +34,17 @@ import { onMounted, reactive, ref, watch } from 'vue'
} }
} }
onMounted(fetchItems) onMounted(fetchItems)
watch(filters, fetchItems) watch(filters, fetchItems)
const addItemToFavorite = async (item) => {
try {
item.isFavorite = !item.isFavorite;
await axios.patch(`${import.meta.env.VITE_API_URL}/items/${item.id}`, item);
} catch (e) {
console.log('Error', e)
}
}
</script> </script>
<template> <template>
<Drawer v-if="showDrawer"/> <Drawer v-if="showDrawer"/>
@ -71,7 +79,11 @@ import { onMounted, reactive, ref, watch } from 'vue'
</div> </div>
<div v-else> <div v-else>
<CardList v-if="items.length > 0" :items="items" /> <CardList
v-if="items.length > 0"
:items="items"
@addToFavorite="addItemToFavorite"
/>
<h2 v-else>Пусто</h2> <h2 v-else>Пусто</h2>
</div> </div>

View File

@ -3,6 +3,7 @@
defineProps({ defineProps({
imageUrl: String, imageUrl: String,
id: Number,
title: String, title: String,
price: Number, price: Number,
isAdded: Boolean, isAdded: Boolean,

View File

@ -5,13 +5,13 @@
alert('Добавлено в корзину') alert('Добавлено в корзину')
} }
const onClickFavorite = () => {
alert('Добавлено в Избранное')
}
defineProps({ defineProps({
items: Array items: Array
}) })
defineEmits([
'addToFavorite',
])
</script> </script>
<template> <template>
@ -19,11 +19,14 @@
<Card <Card
v-for="item in items" v-for="item in items"
:key="item.id" :key="item.id"
:id="item.id"
:image-url="item.imageUrl" :image-url="item.imageUrl"
:title="item.title" :title="item.title"
:price="item.price" :price="item.price"
:is-added="item.isAdded"
:is-favorite="item.isFavorite"
:on-click-add="onClickAdd" :on-click-add="onClickAdd"
:on-click-favorite="onClickFavorite" :on-click-favorite="() => $emit('addToFavorite', item)"
/> />
</div> </div>
</template> </template>