Skip to content

Commit

Permalink
Add small changes to fetch using composable
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasgauvin committed Mar 20, 2024
1 parent 292c7d3 commit 04dad2d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 30 deletions.
3 changes: 2 additions & 1 deletion pages/Items/Create.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ const handleSubmit = async () => {
price: formData.value.price,
},
});
if (error.value) {
if(error.value) {
console.error("Error creating item:", error);
return;
}
navigateTo("/items");
};
Expand Down
1 change: 1 addition & 0 deletions pages/Items/Edit/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const handleSubmit = async () => {
});
if (error.value) {
console.error("Error updating item:", error);
return;
}
navigateTo("/items");
};
Expand Down
6 changes: 4 additions & 2 deletions pages/Items/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@
const { data: items, refresh: refreshItems } = await useFetch("/api/Items");
const handleDelete = async (itemId) => {
const {data, error} = await fetch(`/api/Items/${itemId}`, {
const {data, error} = await useFetch(`/api/Items/${itemId}`, {
method: "DELETE",
});
if (error.value) {
console.log(error)
if (error && error.value) {
console.error("Error deleting item:", error);
return;
}
// Trigger re-fetch after deletion
refreshItems();
Expand Down
10 changes: 5 additions & 5 deletions pages/Sales/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ if (items) {
}
const handleDelete = async (saleId) => {
try {
await fetch(`/api/Sales/${saleId}`, {
const { data, error } = await fetch(`/api/Sales/${saleId}`, {
method: "DELETE",
});
if(error && error.value) {
console.error("Error deleting sale:", error);
return;
}
// Trigger re-fetch after deletion
refreshSales();
} catch (error) {
console.error("Error deleting sale:", error);
}
};
const calculateRevenueForItem = (item, quantity) => {
Expand Down
24 changes: 2 additions & 22 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,8 @@

<script setup>
const sales = ref([]);
const items = ref([]);
onMounted(async () => {
await fetchData();
});
const fetchData = async () => {
try {
const salesResponse = await fetch("/api/Sales");
const itemsResponse = await fetch("/api/Items");
if (!salesResponse.ok || !itemsResponse.ok) {
throw new Error("Failed to fetch data");
}
sales.value = await salesResponse.json();
items.value = await itemsResponse.json();
} catch (error) {
console.error("Error fetching data:", error);
}
};
const { data: items, refresh: refreshItems } = await useFetch("/api/Items");
const { data: sales, refresh: refreshSales } = await useFetch("/api/Sales");
// Calculate revenue for the past n days
const calculateRevenueForDays = (days) => {
Expand Down

0 comments on commit 04dad2d

Please sign in to comment.