From e6fa7db87b5f879de71bfc9e4f8afea9d722ab95 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Fri, 26 May 2023 18:57:36 -0600 Subject: Añade creación de reseñas MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/clients/actions.ts | 64 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 21 deletions(-) (limited to 'src/clients') diff --git a/src/clients/actions.ts b/src/clients/actions.ts index 447d054..852e473 100644 --- a/src/clients/actions.ts +++ b/src/clients/actions.ts @@ -79,27 +79,6 @@ export async function create({ request }) { } } -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(); @@ -124,4 +103,47 @@ export async function placeOrder({ request }) { } catch(error) { return redirect('/account/cart') } +} + +export async function productAction({ params, request }) { + const client = new ApiClient(); + const product = await client.get(`/products/${ params.productId }`); + + if(!product) + redirect(`/products`); + + const product_id = product.data.data.id; + const form = await request.formData(); + + if(form.get('intent') == 'create_review') { + return await createReview(params.productId, form, client); + } + + return await addToCart(product_id, form, client); +} + +async function addToCart(product_id: string, form: FormData, client: ApiClient) { + const post_request_path = "/account/cart"; + form.append('product_id', product_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; +} + +async function createReview(product_id: string, form: FormData, client: ApiClient) { + try { + await client.post(`/products/${product_id}/reviews`, form, client.authorizationHeaders()); + + return null; + } catch(error) { + return error.response; + } } \ No newline at end of file -- cgit v1.2.3