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(); 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 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') } } export async function productAction({ params, request }) { const client = new ApiClient(); const product = await client.get(`/products/${ params.productId }`); if(!product) redirect(`/products`); const product_id = product.data.data.id; const form = await request.formData(); if(form.get('intent') == 'create_review') { return await createReview(params.productId, form, client); } return await addToCart(product_id, form, client); } async function addToCart(product_id: string, form: FormData, client: ApiClient) { const post_request_path = "/account/cart"; form.append('product_id', product_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; } async function createReview(product_id: string, form: FormData, client: ApiClient) { try { await client.post(`/products/${product_id}/reviews`, form, client.authorizationHeaders()); return null; } catch(error) { return error.response; } }