blob: ffb44f3ceb680d502399ecba508fff5a71645df4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
import { redirect } from "react-router-dom";
import { ApiClient } from "./api_client";
import { Product, mapProduct } from "../models/product";
import { Card } from "../models/card";
import { refreshIfExpired, logout, presentSession } from "../lib/session";
import { deleteEmptyParams } from "../lib/form_utils";
import Token from "../lib/token";
import Order from "../models/order";
export async function loader({ request }) {
if(presentSession()) {
if(!refreshIfExpired()) {
logout();
return redirect('/products');
}
}
const client = new ApiClient();
const url = new URL(request.url)
const params = '?' + deleteEmptyParams(new URLSearchParams(url.search)).toString();
const response = await client.get(`${url.pathname}${params}`);
return response;
}
export async function cartLoader() {
const product_data = new Array<Product>();
const cards_data = new Array<Card>();
const token = new Token();
const client = new ApiClient();
const product_request = await client.authenticatedGet("/account/cart");
const cards_request = await client.authenticatedGet("/account/cards");
if(product_request.response || cards_request.response) {
token.logout();
return redirect("/products")
}
product_request.data.data.attributes.products.data.map(response_data => {
product_data.push(mapProduct(response_data));
});
cards_request.data.data.map(data => {
cards_data.push(data.attributes);
});
return [product_data, cards_data];
}
export async function addressLoader({ params }) {
const client = new ApiClient();
const path = `/account/addresses/${params.addressId}`;
const request = await client.authenticatedGet(path);
if(request.response)
return redirect("/account");
return request;
}
export async function cardLoader({ params }) {
const client = new ApiClient();
const path = `/account/cards/${ params.cardId }`;
const request = await client.authenticatedGet(path);
if(request.response)
return redirect("/account");
return request;
}
export async function accountLoader() {
const client = new ApiClient();
const account_response = await client.authenticatedGet("/account");
const addresses_response = await client.authenticatedGet("/account/addresses");
const cards_response = await client.authenticatedGet("/account/cards");
// Authentication error handling.
if(account_response.response || addresses_response.response || cards_response.response)
return redirect("/products");
return [account_response, addresses_response, cards_response];
}
export async function productLoader({ params }) {
const client = new ApiClient();
const response = await client.getProduct(params.productId);
if(response[0].response?.status == 404) {
throw new Response("", {
status: 404,
statusText: "El producto que desea buscar no existe",
});
}
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');
}
}
|