From e8fa9bd7bba125a339f11876eb5ea99d0cd301b6 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Thu, 25 May 2023 19:11:31 -0600 Subject: Añade historial de órdenes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/clients/actions.ts | 13 +++++++++++++ src/clients/loaders.ts | 40 ++++++++++++++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 6 deletions(-) (limited to 'src/clients') 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(); + const product_data = new Array(); + const cards_data = new Array(); 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 + + 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 -- cgit v1.2.3