diff options
Diffstat (limited to 'src/clients/actions.ts')
-rw-r--r-- | src/clients/actions.ts | 34 |
1 files changed, 34 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 |