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
121
122
123
124
125
126
127
|
import { redirect } from "react-router-dom";
import { ApiClient } from "./api_client";
import { clearSessionStorage, deleteEmptyFields, setFormErrorsInSessionStorage, FormError } from "../lib/form_utils";
import Token from "../lib/token";
function requestErrorsToArray(errors: any) {
const errors_array = new Array<FormError>();
for(const [field, error_message] of Object.entries(errors)) {
const e = {
field_name: field,
message: error_message.toString()
};
errors_array.push(e);
}
return errors_array;
}
export async function editAccount({ request }) {
const client = new ApiClient();
let form_data = await request.formData();
form_data = deleteEmptyFields(form_data);
const response = await client.put("/account", form_data);
if(response.status == 401)
return redirect("/products");
client.token.set(response.data.token);
client.token.setRefresh(response.data.refresh);
return redirect("/account");
}
export async function authenticatedEdit({ request }) {
clearSessionStorage();
const client = new ApiClient();
const paths = new URL(request.url).pathname.split('/');
const request_path = paths.slice(0, paths.length - 1).join('/');
let form_data = await request.formData();
form_data = deleteEmptyFields(form_data);
try {
await client.put(request_path, form_data);
return redirect("/account");
} catch(error) {
if(error.response.status == 401) {
new Token().logout();
return redirect("/products");
}
else {
return requestErrorsToArray(error.response.data.errors);
}
}
}
export async function create({ request }) {
clearSessionStorage();
const client = new ApiClient();
const paths = new URL(request.url).pathname.split('/');
const request_path = paths.slice(0, paths.length - 1).join('/');
const form_data = await request.formData();
try{
await client.post(request_path, form_data, client.authorizationHeaders());
return redirect("/account");
} catch(error) {
if(error.response.status == 401) {
new Token().logout();
return redirect("/products");
}
else {
return requestErrorsToArray(error.response.data.errors);
}
}
}
export async function addToCart({ params, request }) {
const client = new ApiClient();
const product = await client.get(`/products/${ params.productId }`);
const post_request_path = "/account/cart";
const id = product.data.data.id;
const form = await request.formData();
form.append('product_id', id);
if(form.get('quantity') == '')
form.set('quantity', 1);
try {
await client.post(post_request_path, form, client.authorizationHeaders());
} catch(error) {
return error.response.status;
}
return 200;
}
export async function deleteFromCart({ request }) {
const client = new ApiClient();
const form = await request.formData();
const to_delete = `/account/cart/${ form.get('product_id') }`;
const req = await client.del(to_delete);
if(req.response) { // 404
return redirect("/account/cart");
}
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')
}
}
|