Skip to content

Commit

Permalink
sales page uses the api plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaprar committed Jul 1, 2024
1 parent 914bf3c commit 4fb136a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 29 deletions.
Binary file modified backend/.tmp/dev.data.db
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
},
"x-generation-date": "2024-06-26T15:36:38.832Z"
"x-generation-date": "2024-07-01T19:00:00.554Z"
},
"x-strapi-config": {
"path": "/documentation",
Expand Down
44 changes: 17 additions & 27 deletions frontend/pages/shop/sales/index.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<template>
<div
class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom"
>
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Finestre di vendita</h1>
</div>
<div class="table-responsive small">
Expand All @@ -13,9 +11,7 @@
<th>Data fine</th>
<th>Stato</th>
<th>
<NuxtLink :to="`/shop/sales/new`"
><i class="bi-plus-circle-fill"
/></NuxtLink>
<NuxtLink :to="`/shop/sales/new`"><i class="bi-plus-circle-fill" /></NuxtLink>
</th>
</tr>
</thead>
Expand All @@ -28,9 +24,7 @@
{{ isSaleActive(sale) ? "🟢 In corso" : "🔴 Conclusa" }}
</td>
<td>
<NuxtLink :to="`/shop/sales/${sale.id}`"
><i class="bi-pencil-fill"
/></NuxtLink>
<NuxtLink :to="`/shop/sales/${sale.id}`"><i class="bi-pencil-fill" /></NuxtLink>
</td>
</tr>
</tbody>
Expand All @@ -39,7 +33,6 @@
</template>

<script lang="ts">
const API_URL = `http://localhost:1337/api`;
export default {
setup() {
definePageMeta({ layout: "admin" });
Expand All @@ -52,31 +45,28 @@ export default {
};
},
async created() {
this.$log().debug("created");
this.$loader.startLoader();
let sales = await this.getSales(this.shopId);
await this.refreshSales();
this.$loader.stopLoader();
this.sales = sales;
},
methods: {
isSaleActive(sale: Sale): boolean {
const now = new Date();
return now >= sale.startDate && now <= sale.endDate
},
async getSales(shop: string): Promise<Sale[]> {
const url = `${API_URL}/shops/${shop}/sales`;
let response = await fetch(url);
let sales_response: SaleDto[] = await response.json();
this.$log().debug("sales_response", sales_response);
return sales_response.map((item) => {
return {
id: item.id?.toString(),
startDate: new Date(item.startDate),
endDate: new Date(item.endDate),
} as Sale;
});
async refreshSales() {
let result = await this.$backend.sales.getAll();
if (result.ok) {
this.sales = result.val.map((item) => {
return {
id: item.id?.toString(),
startDate: new Date(item.startDate),
endDate: new Date(item.endDate),
} as Sale;
});
} else {
this.$toast.error("Error nel recuperare le vendite. Riprovare.")
}
},
},
};
Expand Down
19 changes: 18 additions & 1 deletion frontend/plugins/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ class SalesClient {
return Err(ApiErrorVariant.NotFound)
}
let result: SaleDto = (await response.json());
//result.id = saleId
logger.debug("[ApiClient][Sales][get] result", result);
return Ok(result)
} catch (error) {
Expand All @@ -278,6 +277,24 @@ class SalesClient {
}
}

async getAll(): Promise<Result<SaleDto[], ApiErrorVariant>> {
logger.debug("[ApiClient][Sales][getAll] SHOP_ID", SHOP_ID)
const url = `${this.baseUrl}/shops/${SHOP_ID}/sales`;
try {
let response = await fetch(url);
if (response.status == 404) {
logger.warn("[ApiClient][Sales][getAll] not found");
return Err(ApiErrorVariant.NotFound)
}
let result: SaleDto[] = (await response.json());
logger.debug("[ApiClient][Sales][getAll] result", result);
return Ok(result)
} catch (error) {
logger.error("[ApiClient][Sales][getAll] error", error);
return Err(ApiErrorVariant.Generic)
}
}

async getOrders(saleId: number): Promise<Result<OrderDto[], ApiErrorVariant>> {
logger.debug("[ApiClient][Sales][getOrders] saleId", saleId)
const url = `${this.baseUrl}/sales/${saleId}/orders`;
Expand Down
5 changes: 5 additions & 0 deletions frontend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5943,6 +5943,11 @@ vue-router@^4.3.2, vue-router@^4.3.3:
dependencies:
"@vue/devtools-api" "^6.5.1"

vue-toastification@^2.0.0-rc.5:
version "2.0.0-rc.5"
resolved "https://registry.yarnpkg.com/vue-toastification/-/vue-toastification-2.0.0-rc.5.tgz#92798604d806ae473cfb76ed776fae294280f8f8"
integrity sha512-q73e5jy6gucEO/U+P48hqX+/qyXDozAGmaGgLFm5tXX4wJBcVsnGp4e/iJqlm9xzHETYOilUuwOUje2Qg1JdwA==

vue3-datepicker@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/vue3-datepicker/-/vue3-datepicker-0.4.0.tgz#d9493e801022062a89ba41e7927b391e5fe2e039"
Expand Down

0 comments on commit 4fb136a

Please sign in to comment.