Skip to content

Commit 40a4d37

Browse files
committed
Fix edit product bug
1 parent 48fcf7a commit 40a4d37

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

backend/src/views/Products/Products.vue

+23-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
Add new Product
99
</button>
1010
</div>
11-
<ProductsTable />
12-
<ProductModal v-model="showProductModal" :product="product"/>
11+
<ProductsTable @clickEdit="editProduct"/>
12+
<ProductModal v-model="showProductModal" :product="productModel" @close="onModalClose"/>
1313
</template>
1414

1515
<script setup>
@@ -18,14 +18,34 @@ import store from "../../store";
1818
import ProductModal from "./ProductModal.vue";
1919
import ProductsTable from "./ProductsTable.vue";
2020
21+
const DEFAULT_PRODUCT = {
22+
id: '',
23+
title: '',
24+
description: '',
25+
image: '',
26+
price: ''
27+
}
28+
2129
const products = computed(() => store.state.products);
2230
23-
const product = ref({})
31+
const productModel = ref({...DEFAULT_PRODUCT})
2432
const showProductModal = ref(false);
2533
2634
function showAddNewModal() {
2735
showProductModal.value = true
2836
}
37+
38+
function editProduct(p) {
39+
store.dispatch('getProduct', p.id)
40+
.then(({data}) => {
41+
productModel.value = data
42+
showAddNewModal();
43+
})
44+
}
45+
46+
function onModalClose() {
47+
productModel.value = {...DEFAULT_PRODUCT}
48+
}
2949
</script>
3050

3151
<style scoped>

backend/src/views/Products/ProductsTable.vue

+3-5
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ const sortDirection = ref('desc')
189189
const product = ref({})
190190
const showProductModal = ref(false);
191191
192+
const emit = defineEmits(['clickEdit'])
193+
192194
onMounted(() => {
193195
getProducts();
194196
})
@@ -243,11 +245,7 @@ function deleteProduct(product) {
243245
}
244246
245247
function editProduct(p) {
246-
store.dispatch('getProduct', p.id)
247-
.then(({data}) => {
248-
product.value = data
249-
showAddNewModal();
250-
})
248+
emit('clickEdit', p)
251249
}
252250
</script>
253251

0 commit comments

Comments
 (0)