diff options
author | HombreLaser <sebastian-440@live.com> | 2023-05-24 20:18:00 -0600 |
---|---|---|
committer | HombreLaser <sebastian-440@live.com> | 2023-05-24 20:18:00 -0600 |
commit | da2631822f902094e691143302d6fc6b68e1cf56 (patch) | |
tree | 68b893c04233f779f5a65151ea21d673b6f7a8fc /src/clients | |
parent | 21508a3514500f8f38ddaa8bef7a9cd420d76628 (diff) |
Añade lógica de carrito
Diffstat (limited to 'src/clients')
-rw-r--r-- | src/clients/actions.ts | 34 | ||||
-rw-r--r-- | src/clients/api_client.ts | 8 |
2 files changed, 42 insertions, 0 deletions
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 diff --git a/src/clients/api_client.ts b/src/clients/api_client.ts index 81ae482..e544f16 100644 --- a/src/clients/api_client.ts +++ b/src/clients/api_client.ts @@ -44,6 +44,14 @@ export class ApiClient { return response } + async del(path: string) { + const request_url = `${ this.url }${ path }`; + const options = this.authorizationHeaders(); + + const response = await axios.delete(request_url, options); + return response; + } + async getProduct(id: string) { const request_url = `${ this.url }/products/${ id }`; const [product_response, product_reviews] = await Promise.all([ |