From da2631822f902094e691143302d6fc6b68e1cf56 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Wed, 24 May 2023 20:18:00 -0600 Subject: Añade lógica de carrito MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/clients/actions.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src/clients/actions.ts') diff --git a/src/clients/actions.ts b/src/clients/actions.ts index cee9a85..bd99519 100644 --- a/src/clients/actions.ts +++ b/src/clients/actions.ts @@ -77,4 +77,38 @@ export async function create({ request }) { 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; } \ No newline at end of file -- cgit v1.2.3