summaryrefslogtreecommitdiff
path: root/src/clients/loaders.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/clients/loaders.ts')
-rw-r--r--src/clients/loaders.ts40
1 files changed, 34 insertions, 6 deletions
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