From f05130e52c9aedd21c07dbf3de8821b4e21c187c Mon Sep 17 00:00:00 2001 From: Sergey Krylov Date: Mon, 27 Jan 2025 06:52:47 +0300 Subject: [PATCH] feat: add creating order --- src/App.vue | 49 ++++++++++++++++++++++++++++++++++++--- src/components/Drawer.vue | 23 +++++++++++++++--- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/App.vue b/src/App.vue index 7b7eb5c..6c17432 100644 --- a/src/App.vue +++ b/src/App.vue @@ -14,6 +14,7 @@ searchQuery: '', sortBy: '' }); + const isCreatingOrder = ref(false); const fetchItems = async (filters = {}) => { const params = {}; @@ -48,7 +49,8 @@ "type": "success", "hideProgressBar": true, "transition": "slide", - "dangerouslyHTMLString": true + "dangerouslyHTMLString": true, + "position": "bottom-right", }) } } catch (e) { @@ -57,7 +59,8 @@ "type": "error", "hideProgressBar": true, "transition": "slide", - "dangerouslyHTMLString": true + "dangerouslyHTMLString": true, + "position": "bottom-right", }) } } @@ -72,7 +75,8 @@ "type": "success", "hideProgressBar": true, "transition": "slide", - "dangerouslyHTMLString": true + "dangerouslyHTMLString": true, + "position": "bottom-right", }) } } catch (e) { @@ -80,6 +84,43 @@ } } + const createOrder = async () => { + try { + isCreatingOrder.value = true; + const { data } = await axios.post(`${import.meta.env.VITE_API_URL}/orders`, { + items: cart.value, + totalPrice: totalPrice.value + }); + + items.value.forEach((item) => { + item.isAdded = false; + }); + + await axios.patch(`${import.meta.env.VITE_API_URL}/items`, items.value); + + toast("Заказ успешно оформлен", { + "type": "success", + "hideProgressBar": true, + "transition": "slide", + "dangerouslyHTMLString": true, + "position": "bottom-right", + }) + + return data; + } catch (e) { + toast("Ошибка при создании заказа", { + "type": "error", + "hideProgressBar": true, + "transition": "slide", + "dangerouslyHTMLString": true, + "position": "bottom-right", + }) + } finally { + isCreatingOrder.value = false; + showDrawer.value = false; + } + } + const cart = computed(() => items.value.filter(i => i.isAdded)); const totalPrice = computed(() => cart.value.reduce((res, cur) => { return res + cur.price @@ -91,6 +132,8 @@ totalPrice, openDrawer: () => showDrawer.value = true, closeDrawer: () => showDrawer.value = false, + createOrder, + isCreatingOrder, })