feat: add add card to favorite
This commit is contained in:
parent
6e9947640b
commit
25d1fa25dd
18
src/App.vue
18
src/App.vue
@ -1,5 +1,5 @@
|
||||
<script setup>
|
||||
import { onMounted, reactive, ref, watch } from 'vue'
|
||||
import { onMounted, provide, reactive, ref, watch } from 'vue'
|
||||
import Header from '@/components/Header.vue'
|
||||
import CardList from '@/components/CardList.vue'
|
||||
import Drawer from '@/components/Drawer.vue'
|
||||
@ -34,9 +34,17 @@ import { onMounted, reactive, ref, watch } from 'vue'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
onMounted(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>
|
||||
<template>
|
||||
<Drawer v-if="showDrawer"/>
|
||||
@ -71,7 +79,11 @@ import { onMounted, reactive, ref, watch } from 'vue'
|
||||
</div>
|
||||
|
||||
<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>
|
||||
</div>
|
||||
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
|
||||
defineProps({
|
||||
imageUrl: String,
|
||||
id: Number,
|
||||
title: String,
|
||||
price: Number,
|
||||
isAdded: Boolean,
|
||||
|
||||
@ -5,13 +5,13 @@
|
||||
alert('Добавлено в корзину')
|
||||
}
|
||||
|
||||
const onClickFavorite = () => {
|
||||
alert('Добавлено в Избранное')
|
||||
}
|
||||
|
||||
defineProps({
|
||||
items: Array
|
||||
})
|
||||
|
||||
defineEmits([
|
||||
'addToFavorite',
|
||||
])
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@ -19,11 +19,14 @@
|
||||
<Card
|
||||
v-for="item in items"
|
||||
:key="item.id"
|
||||
:id="item.id"
|
||||
:image-url="item.imageUrl"
|
||||
:title="item.title"
|
||||
:price="item.price"
|
||||
:is-added="item.isAdded"
|
||||
:is-favorite="item.isFavorite"
|
||||
:on-click-add="onClickAdd"
|
||||
:on-click-favorite="onClickFavorite"
|
||||
:on-click-favorite="() => $emit('addToFavorite', item)"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user