summaryrefslogtreecommitdiff
path: root/src/clients
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-05-25 19:11:31 -0600
committerHombreLaser <sebastian-440@live.com>2023-05-25 19:11:31 -0600
commite8fa9bd7bba125a339f11876eb5ea99d0cd301b6 (patch)
tree5d8211221680a3c214b3a16b5d179722a8add085 /src/clients
parentda2631822f902094e691143302d6fc6b68e1cf56 (diff)
Añade historial de órdenes
Diffstat (limited to 'src/clients')
-rw-r--r--src/clients/actions.ts13
-rw-r--r--src/clients/loaders.ts40
2 files changed, 47 insertions, 6 deletions
diff --git a/src/clients/actions.ts b/src/clients/actions.ts
index bd99519..447d054 100644
--- a/src/clients/actions.ts
+++ b/src/clients/actions.ts
@@ -111,4 +111,17 @@ export async function deleteFromCart({ request }) {
}
return req.status;
+}
+
+export async function placeOrder({ request }) {
+ const client = new ApiClient();
+ const form = await request.formData();
+
+ try {
+ const response = await client.post('/orders', form, client.authorizationHeaders());
+
+ return redirect('/products');
+ } catch(error) {
+ return redirect('/account/cart')
+ }
} \ No newline at end of file
diff --git a/src/clients/loaders.ts b/src/clients/loaders.ts
index 9f5a131..a114e8d 100644
--- a/src/clients/loaders.ts
+++ b/src/clients/loaders.ts
@@ -1,7 +1,9 @@
import { redirect } from "react-router-dom";
import { ApiClient } from "./api_client";
import { Product, mapProduct } from "../models/product";
+import { Card } from "../models/card";
import Token from "../lib/token";
+import Order from "../models/order";
export async function loader({ request }) {
const client = new ApiClient();
@@ -12,22 +14,29 @@ export async function loader({ request }) {
}
export async function cartLoader() {
- const data = new Array<Product>();
+ const product_data = new Array<Product>();
+ const cards_data = new Array<Card>();
const token = new Token();
const client = new ApiClient();
- const request = await client.authenticatedGet("/account/cart");
+ const product_request = await client.authenticatedGet("/account/cart");
+ const cards_request = await client.authenticatedGet("/account/cards");
- if(request.response) {
+ if(product_request.response || cards_request.response) {
token.logout();
return redirect("/products")
}
- request.data.data.attributes.products.data.map(response_data => {
- data.push(mapProduct(response_data));
+ product_request.data.data.attributes.products.data.map(response_data => {
+ product_data.push(mapProduct(response_data));
});
- return data;
+ cards_request.data.data.map(data => {
+ cards_data.push(data.attributes);
+ });
+
+
+ return [product_data, cards_data];
}
export async function addressLoader({ params }) {
@@ -71,4 +80,23 @@ export async function productLoader({ params }) {
const response = await client.getProduct(params.productId);
return [response[0], response[1]];
+}
+
+export async function ordersLoader() {
+ const client = new ApiClient();
+ const response = await client.authenticatedGet('/orders');
+ const orders = new Array<Order>
+
+ try {
+ response.data.data.map(order => {
+ delete order.attributes.products;
+ order.attributes['id'] = order.id;
+ orders.push(order.attributes)
+ });
+
+ return orders;
+ } catch(error) {
+
+ return redirect('/products');
+ }
} \ No newline at end of file