diff options
author | HombreLaser <sebastian-440@live.com> | 2023-05-22 21:18:20 -0600 |
---|---|---|
committer | HombreLaser <sebastian-440@live.com> | 2023-05-22 21:18:20 -0600 |
commit | 21508a3514500f8f38ddaa8bef7a9cd420d76628 (patch) | |
tree | e416d58752154f7cbe78a96c1fbe35dd34a8703f /src/clients | |
parent | b5885b23a8d3593428334683b8c03075ce071f3a (diff) |
Añade carrito
Diffstat (limited to 'src/clients')
-rw-r--r-- | src/clients/actions.ts | 6 | ||||
-rw-r--r-- | src/clients/loaders.ts | 21 |
2 files changed, 25 insertions, 2 deletions
diff --git a/src/clients/actions.ts b/src/clients/actions.ts index acc5ad3..cee9a85 100644 --- a/src/clients/actions.ts +++ b/src/clients/actions.ts @@ -56,13 +56,15 @@ export async function authenticatedEdit({ request }) { } } -export async function createAddress({ request }) { +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('/account/addresses', form_data, client.authorizationHeaders()); + await client.post(request_path, form_data, client.authorizationHeaders()); return redirect("/account"); } catch(error) { diff --git a/src/clients/loaders.ts b/src/clients/loaders.ts index 7e44f16..9f5a131 100644 --- a/src/clients/loaders.ts +++ b/src/clients/loaders.ts @@ -1,5 +1,7 @@ import { redirect } from "react-router-dom"; import { ApiClient } from "./api_client"; +import { Product, mapProduct } from "../models/product"; +import Token from "../lib/token"; export async function loader({ request }) { const client = new ApiClient(); @@ -9,6 +11,25 @@ export async function loader({ request }) { return response; } +export async function cartLoader() { + const data = new Array<Product>(); + const token = new Token(); + const client = new ApiClient(); + const request = await client.authenticatedGet("/account/cart"); + + if(request.response) { + token.logout(); + + return redirect("/products") + } + + request.data.data.attributes.products.data.map(response_data => { + data.push(mapProduct(response_data)); + }); + + return data; +} + export async function addressLoader({ params }) { const client = new ApiClient(); const path = `/account/addresses/${params.addressId}`; |